策略设计模式:
Dock超类:
package com.hycz.design.pattern.strategy;
import com.hycz.design.pattern.strategy.behavioral.FlyBehavior;
import com.hycz.design.pattern.strategy.behavioral.QuackBehavior;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:13 AM
* To change this template use File | Settings | File Templates.
*/
public abstract class Duck {
public FlyBehavior flyBehavior;
public QuackBehavior quackBehavior;
public void setFlyBehavior(FlyBehavior flyBehavior) {
this.flyBehavior = flyBehavior;
}
public void setQuackBehavior(QuackBehavior quackBehavior) {
this.quackBehavior = quackBehavior;
}
public abstract void dispaly();
public void swim(){
System.out.printf("All duck float, even decoys!");
}
public void performFly(){
flyBehavior.fly();
}
public void performQuack(){
quackBehavior.quack();
}
}
package com.hycz.design.pattern.strategy.behavioral;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:16 AM
* To change this template use File | Settings | File Templates.
*/
public interface FlyBehavior {
public void fly();
}
package com.hycz.design.pattern.strategy.behavioral;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:49 AM
* To change this template use File | Settings | File Templates.
*/
public class FlyNoWings implements FlyBehavior {
@Override
public void fly() {
System.out.println("I'm can't fly !");
}
}
package com.hycz.design.pattern.strategy.behavioral;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:47 AM
* To change this template use File | Settings | File Templates.
*/
public class FlyWithWings implements FlyBehavior {
@Override
public void fly() {
System.out.println("I'm flying !");
}
}
package com.hycz.design.pattern.strategy.behavioral;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:16 AM
* To change this template use File | Settings | File Templates.
*/
public interface QuackBehavior {
public void quack();
}
package com.hycz.design.pattern.strategy.behavioral;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:50 AM
* To change this template use File | Settings | File Templates.
*/
public class Quack implements QuackBehavior {
@Override
public void quack() {
System.out.println("Quack !");
}
}
package com.hycz.design.pattern.strategy;
import com.hycz.design.pattern.strategy.behavioral.FlyNoWings;
import com.hycz.design.pattern.strategy.behavioral.Quack;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:55 AM
* To change this template use File | Settings | File Templates.
*/
public class ModelDuck extends Duck {
public ModelDuck() {
flyBehavior = new FlyNoWings();
quackBehavior = new Quack();
}
@Override
public void dispaly() {
System.out.println("I'm a model duck !");
}
}
package com.hycz.design.pattern.strategy;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/13/13
* Time: 10:57 AM
* To change this template use File | Settings | File Templates.
*/
public class MiniDuckSimulator {
public static void main(String[] args) {
Duck model = new ModelDuck();
model.dispaly();
model.performFly();
model.performQuack();
}
}