Skip to content

Commit

Permalink
add Strategy Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
phonism committed Nov 20, 2014
1 parent 24186e8 commit 84a5d97
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions DesignPattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Design pattern samples from [Head First Design Patterns](http://www.amazon.com/Head-First-Design-Patterns-Freeman/dp/0596007124)
11 changes: 11 additions & 0 deletions DesignPattern/StartegyPattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
策略模式
========

**设计原则一:**找出应用中可能需要变化指出,把他们独立出来,不要和不需要变化的代码混在一起。

**设计原则二:**针对接口编程,而不是针对实现编程。(利用多态)

**设计原则三:**多用组合,少用继承。


鸭子类继承Duck,飞行行为实现FlyBehavior接口,呱呱叫行为实现QuackBehavior接口。
34 changes: 34 additions & 0 deletions DesignPattern/StartegyPattern/src/Duck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package StrategyPattern;

public abstract class Duck {

QuackBehavior quackBehavior;
FlyBehavior flyBehavior;

public Duck() {

}

public abstract void display();

public void performQuack() {
quackBehavior.quack();
}

public void performmFly() {
flyBehavior.fly();
}

public void setFlyBehavior(FlyBehavior flyBehavior) {
this.flyBehavior = flyBehavior;
}

public void setQuackBehavior(QuackBehavior quackBehavior) {
this.quackBehavior = quackBehavior;
}

public void swim() {
System.out.println("All ducks float, even decoys!");
}

}
7 changes: 7 additions & 0 deletions DesignPattern/StartegyPattern/src/FlyBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package StrategyPattern;

public interface FlyBehavior {

public void fly();

}
9 changes: 9 additions & 0 deletions DesignPattern/StartegyPattern/src/FlyNoWay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package StrategyPattern;

public class FlyNoWay implements FlyBehavior {

public void fly() {
System.out.println("I can't fly!!!");
}

}
9 changes: 9 additions & 0 deletions DesignPattern/StartegyPattern/src/FlyRocketPowered.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package StrategyPattern;

public class FlyRocketPowered implements FlyBehavior {

public void fly() {
System.out.println("I'm flying with a rocket!!!");
}

}
9 changes: 9 additions & 0 deletions DesignPattern/StartegyPattern/src/FlyWithWings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package StrategyPattern;

public class FlyWithWings implements FlyBehavior {

public void fly() {
System.out.println("I'm flying!!!");
}

}
14 changes: 14 additions & 0 deletions DesignPattern/StartegyPattern/src/MallardDuck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package StrategyPattern;

public class MallardDuck extends Duck {

public MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}

public void display() {
System.out.println("I'm a real Mallard duck");
}

}
16 changes: 16 additions & 0 deletions DesignPattern/StartegyPattern/src/MiniDuckSimulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package StrategyPattern;

public class MiniDuckSimulator {

public static void main(String[] args) {
Duck mallard = new MallardDuck();
mallard.performQuack();
mallard.performmFly();

Duck model = new ModelDuck();
model.performmFly();
model.setFlyBehavior(new FlyRocketPowered());
model.performmFly();
}

}
14 changes: 14 additions & 0 deletions DesignPattern/StartegyPattern/src/ModelDuck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package StrategyPattern;

public class ModelDuck extends Duck {

public ModelDuck() {
flyBehavior = new FlyNoWay();
quackBehavior = new Quack();
}

public void display() {
System.out.println("I'm a model duck!!!");
}

}
9 changes: 9 additions & 0 deletions DesignPattern/StartegyPattern/src/MuteQuack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package StrategyPattern;

public class MuteQuack implements QuackBehavior {

public void quack() {
System.out.println("<< Silence >>");
}

}
9 changes: 9 additions & 0 deletions DesignPattern/StartegyPattern/src/Quack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package StrategyPattern;

public class Quack implements QuackBehavior {

public void quack() {
System.out.println("Quack!!!");
}

}
7 changes: 7 additions & 0 deletions DesignPattern/StartegyPattern/src/QuackBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package StrategyPattern;

public interface QuackBehavior {

public void quack();

}
9 changes: 9 additions & 0 deletions DesignPattern/StartegyPattern/src/Squeak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package StrategyPattern;

public class Squeak implements QuackBehavior {

public void quack() {
System.out.println("Squeak!!!");
}

}

0 comments on commit 84a5d97

Please sign in to comment.