策略模式
interface FlyBehavior{
public void fly();
}
class FlyNoWay implements FlyBehavior{
public void fly(){
System.out.println("fly no way");
}
}
class FlyWithWings implements FlyBehavior{
public void fly(){
System.out.println("fly with wings");
}
}
class Duck {
FlyBehavior flyBehavior;
public void setFlyBehavior(FlyBehavior flyBehavior){
this.flyBehavior=flyBehavior;
}
public void performFly(){
flyBehavior.fly();
}
}
class MallerdDuck extends Duck{
public MallerdDuck(){
this.setFlyBehavior(new FlyWithWings());
}
}
class ModelDuck extends Duck{
public ModelDuck(){
this.setFlyBehavior(new FlyNoWay());
}
}
public class Stategy{
public static void main(String[] args){
MallerdDuck maller=new MallerdDuck();
maller.performFly();
ModelDuck model=new ModelDuck();
model.performFly();
}
}