码迷,mamicode.com
首页 > 其他好文 > 详细

23种设计模式之二(策略模式)

时间:2017-10-13 23:41:13      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:代码   结构   new   com   功能   close   独立   one   策略   

策略模式:(分别封装行为接口,实现算法族,超类里放行为接口对象,在子类里具体设定行为对象)

原则:

  分离变化部分,封装接口,基于接口编程各种功能。此模式让行为算法的变化独立于算法的使用者。

  技术分享

 

举例说明:

  分别封装行为接口:FlyBehavior、QuackBehavior;

  实现算法族:BadFlyBehavior、GoodFlyBehavior、NoFlyBehavior;GaGaQuackBehavior、GeGeQuackBehavior、NoQuackBehavior;

  超类里放行为接口对象:

    技术分享

 

  在子类里具体设定行为对象:

    技术分享

    技术分享

  在使用的时候:

    技术分享

 

 

例子结构:

技术分享

技术分享
1 package com.java.hexter.stimulateduck.flybehavior;
2 
3 public interface FlyBehavior {
4     void fly();
5 }
FlyBehavior
技术分享
1 package com.java.hexter.stimulateduck.quackbehavior;
2 
3 public interface QuackBehavior {
4     void quack();
5 };
QuackBehavior
技术分享
 1 package com.java.hexter.stimulateduck.duck;
 2 
 3 import com.java.hexter.stimulateduck.flybehavior.FlyBehavior;
 4 import com.java.hexter.stimulateduck.quackbehavior.QuackBehavior;
 5 
 6 public abstract class Duck {
 7 
 8     FlyBehavior mFlyBehavior;
 9     QuackBehavior mQuackBehavior;
10 
11     public Duck() {
12 
13     }
14 
15     public void Fly() {
16         mFlyBehavior.fly();
17     }
18 
19     public void Quack() {
20         mQuackBehavior.quack();
21     }
22 
23     public abstract void display();
24 
25     //为了增加代码的灵活性,可以自己设定鸭子叫的方式
26     public void SetQuackBehavoir(QuackBehavior qb) {
27         mQuackBehavior = qb;
28     }
29     public void SetFlyBehavoir(FlyBehavior fb) {
30         mFlyBehavior = fb;
31     }
32 
33     public void swim() {
34         System.out.println("~~im swim~~");
35     }
36 }
Duck
技术分享
 1 package com.java.hexter.stimulateduck.flybehavior;
 2 
 3 
 4 
 5 public class    BadFlyBehavior implements FlyBehavior
 6 {
 7     @Override
 8     public void fly() {
 9         // TODO Auto-generated method stub
10         System.out.println("--BadFly--");
11     }
12 }
BadFlyBehavior
技术分享
 1 package com.java.hexter.stimulateduck.flybehavior;
 2 
 3 
 4 public class    GoodFlyBehavior implements FlyBehavior
 5 {
 6 
 7     @Override
 8     public void fly() {
 9         // TODO Auto-generated method stub
10         System.out.println("--GoodFly--");
11     }
12     
13 }
GoodFlyBehavior
技术分享
 1 package com.java.hexter.stimulateduck.flybehavior;
 2 
 3 
 4 public class    NoFlyBehavior implements FlyBehavior
 5 {
 6     @Override
 7     public void fly() {
 8         // TODO Auto-generated method stub
 9         System.out.println("--NoFly--");
10     }
11 }
NoFlyBehavior
技术分享
 1 package com.java.hexter.stimulateduck.quackbehavior;
 2 
 3 
 4 public    class GaGaQuackBehavior implements QuackBehavior
 5 {
 6 
 7     @Override
 8     public void quack() {
 9         // TODO Auto-generated method stub
10         System.out.println("__GaGa__");
11     }
12     
13 }
GaGaQuackBehavior
技术分享
 1 package com.java.hexter.stimulateduck.quackbehavior;
 2 
 3 
 4 public    class GeGeQuackBehavior implements QuackBehavior
 5 {
 6 
 7     @Override
 8     public void quack() {
 9         // TODO Auto-generated method stub
10         System.out.println("__GeGe__");
11     }
12     
13 }
GeGeQuackBehavior
技术分享
 1 package com.java.hexter.stimulateduck.quackbehavior;
 2 
 3 
 4 public    class NoQuackBehavior implements QuackBehavior
 5 {
 6 
 7     @Override
 8     public void quack() {
 9         // TODO Auto-generated method stub
10         System.out.println("__NoQuack__");
11     }
12     
13 }
NoQuackBehavior
技术分享
 1 package com.java.hexter.stimulateduck.duck;
 2 
 3 import com.java.hexter.stimulateduck.flybehavior.GoodFlyBehavior;
 4 import com.java.hexter.stimulateduck.quackbehavior.GaGaQuackBehavior;
 5 
 6 public class GreenHeadDuck extends Duck {
 7 
 8     public GreenHeadDuck() {
 9         mFlyBehavior = new GoodFlyBehavior();
10         mQuackBehavior = new GaGaQuackBehavior();
11     }
12 
13     @Override
14     public void display() {
15         // TODO Auto-generated method stub
16         System.out.println("**GreenHead**");
17     }
18 
19 }
GreenHeadDuck
技术分享
 1 package com.java.hexter.stimulateduck.duck;
 2 
 3 import com.java.hexter.stimulateduck.flybehavior.BadFlyBehavior;
 4 
 5 import com.java.hexter.stimulateduck.quackbehavior.GeGeQuackBehavior;
 6 
 7 public class RedHeadDuck extends Duck {
 8 
 9     public RedHeadDuck() {
10         mFlyBehavior = new BadFlyBehavior();
11         mQuackBehavior = new GeGeQuackBehavior();
12     }
13 
14     @Override
15     public void display() {
16         // TODO Auto-generated method stub
17         System.out.println("**RedHead**");
18     }
19 
20 }
RedHeadDuck
技术分享
 1 package com.java.hexter.stimulateduck;
 2 
 3 import com.java.hexter.stimulateduck.duck.Duck;
 4 import com.java.hexter.stimulateduck.duck.GreenHeadDuck;
 5 import com.java.hexter.stimulateduck.duck.RedHeadDuck;
 6 import com.java.hexter.stimulateduck.flybehavior.NoFlyBehavior;
 7 import com.java.hexter.stimulateduck.quackbehavior.NoQuackBehavior;
 8 
 9 
10 public class StimulateDuck {
11 
12     public static void main(String[] args) {
13 
14         Duck mGreenHeadDuck = new GreenHeadDuck();
15         Duck mRedHeadDuck = new RedHeadDuck();
16 
17         mGreenHeadDuck.display();
18         mGreenHeadDuck.Fly();
19         mGreenHeadDuck.Quack();
20         mGreenHeadDuck.swim();
21 
22         mRedHeadDuck.display();
23         mRedHeadDuck.Fly();
24         mRedHeadDuck.Quack();
25         mRedHeadDuck.swim();
26         mRedHeadDuck.display();
27         mRedHeadDuck.SetFlyBehavoir(new NoFlyBehavior());
28         mRedHeadDuck.Fly();
29         mRedHeadDuck.SetQuackBehavoir(new NoQuackBehavior());
30         mRedHeadDuck.Quack();
31     }
32 
33 }
StimulateDuck

输出结果:

**GreenHead**
--GoodFly--
__GaGa__
~~im swim~~
**RedHead**
--BadFly--
__GeGe__
~~im swim~~
**RedHead**
--NoFly--
__NoQuack__

例子该种方式实现的好处:新增行为简单,行为类更好的复用,组合更方便。既有继承带来的复用好处,没有挖坑

 

注意:

  1.认真分析策略中变化部分与不变部分;

  2.多用组合,少用继承;用行为类组合,而不是行为的继承;这样做更有弹性。

23种设计模式之二(策略模式)

标签:代码   结构   new   com   功能   close   独立   one   策略   

原文地址:http://www.cnblogs.com/mmzs/p/7663661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!