zoukankan      html  css  js  c++  java
  • 大话设计模式之策略模式读后感

    策略模式:定义了算法家族,分别封装起来,让他们呢之间可以互相的替换,此模式让算法的变化不会影响到使用算法的客户。

    UML类图:

    解读:策略模式是定义一系列的算法,从概念上来讲,这些算法完成的工作都是一样的,只是他们的实现的不同,可以以相同的方式调用所有的算法,减少了各种算法类和算法调用类之间的耦合。

    策略模式中的supperStrategy为context类提供了一系列的可供重用的算法或者行为。继承有助于提取出这些算法的公共功能。

    在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给了策略模式中的对象context对象。

    代码示例:

     1 package com.dzf.designtest;
     2 
     3 /**
     4  * <desc>
     5  *   策略模式:定义了一系列的算法,让这些算法之间可以互相的替换
     6  * <p>
     7  * </desc>
     8  *
     9  * @author dingzf
    10  * @date 2018/4/1
    11  * @time 11:23
    12  */
    13 public class StrategyDemo {
    14     //客户端
    15     public static void main(String[] args) {
    16         Context context1 = new Context(new StrategyOne());
    17         context1.operate();
          //当我们需要使用哪种实现的时候,就选择哪种
    18 Context context2 = new Context(new StrategyTwo()); 19 context2.operate(); 20 Context context3 = new Context(new StrategyThree()); 21 context3.operate(); 22 } 23 } 24 25 26 /** 27 * 策略模式中的上下文对象,引用策略模式中的策略类 28 */ 29 class Context{ 30 private SupperStrategy supperStrategy; 31 public Context(SupperStrategy supperStrategy) { 32 this.supperStrategy = supperStrategy; 33 } 34 public void operate(){ 35 if(supperStrategy!=null){ 36 supperStrategy.getResult(); 37 } 38 } 39 } 40 41 /** 42 * 策略类 43 */ 44 abstract class SupperStrategy { 45 public abstract void getResult(); 46 } 47 48 /** 49 * 策略子类1 50 */ 51 class StrategyOne extends SupperStrategy { 52 @Override 53 public void getResult() { 54 System.out.println("我会策略1号"); 55 } 56 } 57 /** 58 * 策略子类2 59 */ 60 class StrategyTwo extends SupperStrategy { 61 @Override 62 public void getResult() { 63 System.out.println("我会策略2号"); 64 } 65 } 66 /** 67 * 策略子类3 68 */ 69 class StrategyThree extends SupperStrategy { 70 @Override 71 public void getResult() { 72 System.out.println("我会策略3号"); 73 } 74 }

    上面这种写法还是不太好,这样我们调用端需要知道两个类,我们可以改造下:

     1 public class StrategyDemo {
     2     //客户端
     3     public static void main(String[] args) {
     4       Context context1 = new Context("strategyOne");
     5       context1.operate();
     6       Context context2 = new Context("strategyTwo");
     7       context2.operate();
     8       Context context3 = new Context("strategyThree");
     9       context3.operate();
    10     }
    11 }
    12 
    13 
    14 /**
    15  * 策略模式中的上下文对象,引用策略模式中的策略类
    16  */
    17 class Context{
    18     private SupperStrategy supperStrategy;
    19     public Context(String supperStrategy) {
    20         switch (supperStrategy){
    21             case "strategyOne" :
    22                 this.supperStrategy = new StrategyOne();
    23                 break;
    24             case "strategyTwo" :
    25                 this.supperStrategy = new StrategyTwo();
    26                 break;
    27             case "strategyThree" :
    28                 this.supperStrategy = new StrategyThree();
    29                 break;
    30         }
    31     }
    32     public void operate(){
    33         if(supperStrategy!=null){
    34             supperStrategy.getResult();
    35         }
    36     }
    37 }

    我们来使用简单工厂的方式来判断需要生成的对象,这样调用端只需要知道一个context类就可以啦。

    选择所用具体实现的职责由客户端对象承担,并转给了策略模式中的对象context对象。

    大家有没有发现代码上和简单工厂很类似,要说和简单工厂的区别,在代码上的体现就是调用端只需要知道context这个类,而使用简单工厂的时候,至少得知道两个类。进一步解耦和了吧,个人感觉策略模式是简单工厂的升级版,把策略模式和简单工厂合在一起使用更佳,当然大话设计模式里面也是推荐这样使用的。

    最后:以上可以说是自己读大话涉及模式的一些读后感,如果有不对的地方,希望能够指正出来。

  • 相关阅读:
    UVALive 7141 BombX
    CodeForces 722D Generating Sets
    CodeForces 722C Destroying Array
    CodeForces 721D Maxim and Array
    CodeForces 721C Journey
    CodeForces 415D Mashmokh and ACM
    CodeForces 718C Sasha and Array
    CodeForces 635C XOR Equation
    CodeForces 631D Messenger
    田忌赛马问题
  • 原文地址:https://www.cnblogs.com/zfding/p/8686201.html
Copyright © 2011-2022 走看看