View RSS Feed

fatabass

Strategy Pattern Example

Rating: 1 votes, 1.00 average.
by , 03-03-2012 at 11:20 PM (1820 Views)
Java Code:
package strategyPattern;

public class Warrior {
	
	private AttackBehaviour attackBehaviour;
	public String name;

	void setAttackBehaviour(AttackBehaviour aB) {
		this.attackBehaviour = aB;
	}
	
	void attack() {
		attackBehaviour.attack();
	}
}

package strategyPattern;

public interface AttackBehaviour {
	
	void attack();

}

package strategyPattern;

public class Kick implements AttackBehaviour {

	@Override
	public void attack() {
		System.out.println("I am kicking!");
	}
}

package strategyPattern;

public class Punch implements AttackBehaviour {
	
	@Override
	public void attack() {
		System.out.println("I am punching!");
	}
}

package strategyPattern;

public class TestClass {
	
	public static void main(String args[])
	{
		Warrior fatabass = new Warrior();
		fatabass.name = "Fatabass";
		
		fatabass.setAttackBehaviour(new Kick());
		
		fatabass.attack();
		
		fatabass.setAttackBehaviour(new Punch());
		
		fatabass.attack();
	}
}

Submit "Strategy Pattern Example" to Facebook Submit "Strategy Pattern Example" to Digg Submit "Strategy Pattern Example" to del.icio.us Submit "Strategy Pattern Example" to StumbleUpon Submit "Strategy Pattern Example" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments