forked from phonism/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
策略模式 | ||
======== | ||
|
||
**设计原则一:**找出应用中可能需要变化指出,把他们独立出来,不要和不需要变化的代码混在一起。 | ||
|
||
**设计原则二:**针对接口编程,而不是针对实现编程。(利用多态) | ||
|
||
**设计原则三:**多用组合,少用继承。 | ||
|
||
|
||
鸭子类继承Duck,飞行行为实现FlyBehavior接口,呱呱叫行为实现QuackBehavior接口。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package StrategyPattern; | ||
|
||
public interface FlyBehavior { | ||
|
||
public void fly(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 >>"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package StrategyPattern; | ||
|
||
public interface QuackBehavior { | ||
|
||
public void quack(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |