????????????????y????.

?????????????????仯?????????????????????????????????????????????????????????????????????????

?????????Duck?е?fly??????quack?????????

public interface Flyable{
public void fly();
}
public interface Quackable{
public void quack();
}
??Duck?????????
public class Duck{
public void swim(){   //???
System.out.println(" ???");
}
public  abstract void display(); /*???????????????????? ??????????*/
}
??MallardDuck??RedheadDuck??DisabledDuck ????д?????
//??
public class MallardDuck extends Duck  implements Flyable??Quackable{
public void display(){
System.out.println("???????...");
}
public void fly(){
//???÷???
}
public void quack(){
//???÷???
}
}
//????
public class RedheadDuck extends Duck implements Flyable??Quackable{
public void display(){
System.out.println("?????????...");
}
public void fly(){
//???÷???
}
public void quack(){
//???÷???
}
}
//?з?? ????Quackable????в?????
public class DisabledDuck extends Duck implements Quackable{
public void display(){
System.out.println("?з???????...");
}
public void quack(){
//???÷???
}
}

????>>>>>>????:

???????:

??????????????????????????????????????

????????:

????Flyable?? Quackable??????????????????????????????л?????????? Flyable????????Java???????????????????????????????????á?

??????????????????????????????:

??????е???:?ù??????????????.??????????.

??????е????:??????.??????????????·???????????У???д??????

????????????????????????????????д????????.

??????????????????????????.

??????????:??????????????????.

????????????????л?????????????.

???????????:??????????????????.???????μ???????????.

????------------------------- strategy(??????) -------------------------

???????????????????

???????????????????????????????仯????????????????????????У????????????????

???????????п???????仯???????????????????????????Щ??????仯??????????? -->important.

????????????????“?仯????仯?????”?????????????????????????Duck?????????"fly"??????????

??????“quack”??????????????????????????????????????????????“?????”????????????“??

??????”??????????????“????”??

????????д????????FlyBehavior(???????)??QuackBehavior???е??????.

public interface FlyBehavior{
public void fly();
}
public interface QuackBehavior{
public void quack();
}
??????????Щ???FlyBehavior?????????
public class FlyWithWings implements FlyBehavior{
public void  fly(){
//??????????г??????????????
}
}
public class FlyNoWay implements FlyBehavior{
public void  fly(){
//???????????????
}
}
???QuackBehavior????????????
public class Quack implements QuackBehavior{
public void quack(){
//???????е????
}
}
public class Squeak implements QuackBehavior{
public void quack(){
//??????е????
}
}
public class MuteQuack implements QuackBehavior{
public void quack(){
//???????????????
}
}

?????????:

???????????????????÷??к?????е???????????????????????Щ??????????????????????????????Щ?μ????????????????е??????????????“???”????????????????

?????????????Duck ???????

public class Duck{        --------->????????У??????????????????????????.
FlyBehavior flyBehavior;//???
QuackBehavior quackBehavior;//???
public Duck(){}
public abstract void display();
public void swim(){
//???????????
}
public void performFly(){
flyBehavior.fly();  -->????????????????????????????????????????.
}
public void performQuack(){
quackBehavior.quack();();
}
}

????????MallardDuck???????

????----->???????????????'??'??'??'?????????????????????'??'??'??'?????????

public class MallardDuck extends Duck{
public MallardDuck {
flyBehavior = new FlyWithWings ();
quackBehavior = new Quack();
//???MallardDuck ?????Duck?????о???flyBehavior ??quackBehavior ???????}
public void display(){
//???
}
}

??????????????????????????У?????????????????

??????????????????????????flyBehavior ??quackBehavior ???????д??????????????????????????????

?????????????Duck?м?????????????