Head First_デザインパターン_ポリシー・モード


package Strategy_Pattern;

/*
 * head first             
 * 
 * 1.    (    ,           。)
 * 
 * 
 * */

public abstract class Strategy_Pattern_Duck {

	FlyBehaver flyBehaver;
	EatDifference eatDifference;

	//          
	public Strategy_Pattern_Duck() {
	};

	//            
	public void flyPerform() {
		flyBehaver.fly();
	}

	public void eatPerform() {
		eatDifference.eat();
	}

}
package Strategy_Pattern;

interface FlyBehaver {
	//      
	void fly();
}

class FlywhitB implements FlyBehaver {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("FlywhitB");
	}

}

class FlywhitA implements FlyBehaver {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("FlywhitA ");
	}

}

package Strategy_Pattern;

public interface EatDifference {

	void eat();
}

class EatApple implements EatDifference {

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("this is duck eatting Apple!");
	}

}

class EatBanana implements EatDifference {

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("this is duck eatting banana!");
	}

}

package Strategy_Pattern;

public class WhiteDuck extends Strategy_Pattern_Duck {
	public WhiteDuck() {

		//    whiteDuck    eat fly   
		flyBehaver = new FlywhitB();
		eatDifference = new EatApple();
	}

	public void sayHello() {
		System.out.println("  WhiteDuck!");
	}
}

package Strategy_Pattern;

public class Test {

	public static void main(String arg[]) {
		Strategy_Pattern_Duck d = new WhiteDuck();
		d.eatPerform();
		d.flyPerform();
		((WhiteDuck) d).sayHello();

	}
}