代码文件目录结构:
FlyBehavior.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly;
public interface FlyBehavior {
void fly();
}
FlyNoWay.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly;
public class FlyNoWay implements FlyBehavior{
@Override
public void fly() {
System.out.println("I can't fly!");
}
}
FlyRocketPowered.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly;
public class FlyRocketPowered implements FlyBehavior{
@Override
public void fly() {
System.out.println("I'm flying with a rocket!");
}
}
FlyWithWings.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly;
public class FlyWithWings implements FlyBehavior{
public void fly(){
System.out.println("I'm flying!");
}
}
QuackBehavior.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack;
public interface QuackBehavior {
public void quack();
}
MuteQuack.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack;
public class MuteQuack implements QuackBehavior{
@Override
public void quack() {
System.out.println("<< Silence >>");
}
}
Quack.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack;
public class Quack implements QuackBehavior{
@Override
public void quack() {
System.out.println("Quack!");
}
}
Squeak.java
package Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack;
public class Squeak implements QuackBehavior{
@Override
public void quack() {
System.out.println("Squeak!");
}
}
Duck.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyBehavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack.QuackBehavior;
public abstract class Duck {
FlyBehavior flyBehavior;
QuackBehavior quackBehavior;
public Duck(){}
public void setFlyBehavior(FlyBehavior flyBehavior) {
this.flyBehavior = flyBehavior;
}
public void setQuackBehavior(QuackBehavior quackBehavior) {
this.quackBehavior = quackBehavior;
}
public void performFly(){
this.flyBehavior.fly();
}
public void performQuack(){
this.quackBehavior.quack();
}
public void swim(){
System.out.println("the duck is swimming!");
}
public abstract void display();
}
MallardDuck.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyWithWings;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack.Quack;
//绿头鸭
public class MallardDuck extends Duck {
public MallardDuck(){
this.flyBehavior = new FlyWithWings();
this.quackBehavior = new Quack();
}
@Override
public void display() {
System.out.println("this is a MallardDuck!");
}
}
RedHeadDuck.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyWithWings;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack.Quack;
public class RedHeadDuck extends Duck {
public RedHeadDuck(){
this.flyBehavior = new FlyWithWings();
this.quackBehavior = new Quack();
}
@Override
public void display() {
System.out.println("this is a redHeadDuck!");
}
}
RubberDuck.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyNoWay;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack.Squeak;
public class RubberDuck extends Duck{
public RubberDuck(){
this.flyBehavior = new FlyNoWay();
this.quackBehavior = new Squeak();
}
@Override
public void display() {
System.out.println("this is a rubberDuck!");
}
}
DecoyDuck.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyNoWay;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack.MuteQuack;
public class DecoyDuck extends Duck {
public DecoyDuck(){
this.flyBehavior = new FlyNoWay();
this.quackBehavior = new MuteQuack();
}
@Override
public void display() {
System.out.println("this is a DecoyDuck!");
}
}
ModelDuck.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyNoWay;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.quack.MuteQuack;
public class ModelDuck extends Duck{
public ModelDuck(){
this.quackBehavior = new MuteQuack();
this.flyBehavior = new FlyNoWay();
}
@Override
public void display() {
System.out.println("I'm a model duck!");
}
}
MiniDuckSimulator.java
package Chapter1_StrategyPattern.ch1_3_behavior;
import Chapter1_StrategyPattern.ch1_3_behavior.behaviors.fly.FlyRocketPowered;
public class MiniDuckSimulator {
public static void main(String[] args) {
Duck mallard = new MallardDuck();
mallard.performFly();
mallard.performQuack();
Duck model = new ModelDuck();
model.performFly();
model.setFlyBehavior(new FlyRocketPowered());
model.performFly();
}
}
notes.txt
ch1_1
直接使用继承来应付新增子类的问题
问题:
1.当子类鸭子的数量越来越多的时候,继承就难以保证整个系统的可维护性,因为每次添加一个子类,
都要对对应的属性是否要实现进行考虑
2.代码在多个子类重复
3.代码在运行时难以改变
ch1_2
将其中某些非共通的行为抽取出来作为接口
问题:
1.对于具有同一个行为的每个子类都会重复代码,可维护性差
ch1_3
1.将某一类行为抽取出来作为一系列行为类,从is-a转为has-a ==> 将继承转成组合
2.使用setter实现动态设置行为
设计模式:
定义一个算法族,分别封装起来,是的他们之间可以相互变换,使得算法的变化独立于使用他的客户(鸭子)
运行结果: