java-策略模式

 
  1. 策略模式,又叫算法簇模式,就是定义了不同的算法族,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户。 
  2. 策略模式的好处在于你可以动态的改变对象的行为。 
  3. 设计原则是把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口(c++z中可以用虚类),然后在类中包含这个对象的实例,这样类的实例在运行时就可以随意调用实现了这个接口的类的行为。下面是一个例子。 
  4. 策略模式属于对象行为型模式,主要针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。通常,策略模式适用于当一个应用程序需要实现一种特定的服务或者功能,而且该程序有多种实现方式时使用。

        

                                  (策略模式静态图)

       策略模式中有三个对象:
    (1)       环境对象:该类中实现了对抽象策略中定义的接口或者抽象类的引用。
    (2)       抽象策略对象:它可由接口或抽象类来实现。
    (3)       具体策略对象:它封装了实现同不功能的不同算法。
    利用策略模式构建应用程序,可以根据用户配置等内容,选择不同有算法来实现应用程序的功能。具体的选择有环境对象来完成。采用这种方式可以避免由于使用条件语句而带来的代码混乱,提高应用程序的灵活性与条理性。

  5. /* 
  6. 这是一个表现僧人和道士的程序,僧人光头,使用棍子做武器,道士长小胡子,使用拂尘作武器 
  7. */ 
  8. public interface DisplayInte {
  9.     public void display();
  10. }
  11. public interface WeaponInte {
  12.     public void wuqi();
  13. }
  14. public class daoshiDisplayImpl implements DisplayInte {
  15.     public void display() {
  16.         // TODO Auto-generated method stub
  17.         System.out.println("道士是有头发的!!!");
  18.     }
  19. }
  20. public class heshangDisplayImpl implements DisplayInte {
  21.     public void display() {
  22.         // TODO Auto-generated method stub
  23.         System.out.println("和尚没有头发的");
  24.     }
  25. }
  26. public class daoshiWeaponImpl implements WeaponInte {
  27.     public void wuqi() {
  28.         // TODO Auto-generated method stub
  29.             System.out.println("道士使用的武器是拂尘!!!");
  30.     }
  31. }
  32. public class heshangWeaponImpl implements WeaponInte {
  33.     public void wuqi() {
  34.         // TODO Auto-generated method stub
  35.         System.out.println("和尚拿的武器是法仗!!!");
  36.     }
  37. }
  38. public class Daoshi extends Role {
  39.     public Daoshi() {
  40.         this.display=new daoshiDisplayImpl();
  41.         this.weapon=new daoshiWeaponImpl();
  42.         // TODO Auto-generated constructor stub
  43.     }
  44. }
  45. public class Heshang extends Role {
  46.     public Heshang() {
  47.         this.display=new heshangDisplayImpl();
  48.         this.weapon=new heshangWeaponImpl();
  49.         // TODO Auto-generated constructor stub
  50.     }
  51. }
  52. public class Role {
  53.     public String name;
  54.     public int age;
  55.     public DisplayInte display;
  56.     public WeaponInte weapon;
  57.     public void display()
  58.     {       
  59.         display.display();
  60.     }
  61.     public void weapon()
  62.     {
  63.         weapon.wuqi();
  64.     }
  65.     public void changeWeapon()
  66.     {
  67.         if(this instanceof Daoshi)
  68.         {
  69.             weapon=new heshangWeaponImpl();
  70.         }
  71.         else
  72.         {
  73.             weapon=new daoshiWeaponImpl();
  74.         }
  75.     }
  76.     
  77.     
  78. }
  79. public class Test {
  80.     /**
  81.      * @param args
  82.      */
  83.     public static void main(String[] args) {
  84.         // TODO Auto-generated method stub
  85.         Role oDaoshi=new Daoshi();
  86.         Role oHeshang=new Heshang();
  87.         oDaoshi.display();
  88.         oDaoshi.weapon();
  89.         oHeshang.display();
  90.         oHeshang.weapon();
  91.         oDaoshi.changeWeapon();
  92.         oDaoshi.display();
  93.         oHeshang.changeWeapon();
  94.         oHeshang.display();
  95.     }
  96. }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
策略模式是一种行为设计模式,它允许在运行时选择算法的行为。在策略模式中,我们创建表示各种策略的对象和一个上下文对象,该对象可以根据其策略对象的不同行为而更改其执行算法。 以下是Java策略模式的示例代码: 首先,我们定义一个策略接口,该接口定义了一个方法calculate(),该方法将由具体策略类实现: ```java public interface Strategy { public int calculate(int num1, int num2); } ``` 然后,我们实现两个具体策略类,它们实现了策略接口并提供了自己的实现: ```java public class AddStrategy implements Strategy { public int calculate(int num1, int num2) { return num1 + num2; } } public class SubtractStrategy implements Strategy { public int calculate(int num1, int num2) { return num1 - num2; } } ``` 接下来,我们定义一个上下文类,该类将使用策略接口来执行算法: ```java public class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int num1, int num2) { return strategy.calculate(num1, num2); } } ``` 最后,我们可以在客户端代码中使用上下文对象来执行算法: ```java public class Client { public static void main(String[] args) { Context context = new Context(new AddStrategy()); System.out.println("10 + 5 = " + context.executeStrategy(10, 5)); context = new Context(new SubtractStrategy()); System.out.println("10 - 5 = " + context.executeStrategy(10, 5)); } } ``` 输出结果为: ``` 10 + 5 = 15 10 - 5 = 5 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值