<二>读<<大话设计模式>>之策略模式

llguanli llguanli     2022-08-28     700

关键词:

       又和大家见面了。可以坚持写出第二篇文章真不错,好好加油。

       <<大话设计模式>>解说策略模式是以商场收银软件程序开头的,那么问题来了。哪家商场收银软件强,开玩笑了。

读过上篇文章《《简单工厂模式》》的知道,它有两个缺点:1、client依赖两个类。耦合性高;2、假设算法过多则须要写非常多类。解决上面问题的就是策略模式了。

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

商场收银软件:单位价格*打折算法=售价。对于这种软件来说,须要常常变化的是打折算法。其有用简单工厂模式全然能够的。直接添加打折算法,然后工厂生产实例对象就能够了。

       可是这个模式知识解决对象创建问题,并且因为工厂本身包含了全部收费方式,商场是可能常常更改打折算法的,每次维护或扩展收费方式都要修改这个工厂。这样是很麻烦也不遵循易扩展、易复用原则。事实上分析一下该软件的变化点就是打折算法,我们仅仅要将这个变化点封装起来就行了,而策略模式可以做到这一点。

好了。上代码了。依据样例学习进步快

1、策略类,定义了全部算法实现的公共方法接口

/*

 * 策略类

 */

publicabstractclass Strategy {

 

    /*

     * 定义全部算法的公共接口

     */

    publicabstractvoid algorithmlnterface();

}

2、维护一个对Strategy类的引用。想调用那个类就调用那个类

/*

 * 维护对strategy对象的引用

 */

publicclass Context {

 

    Strategystrategy;

    public Context(Strategy strategy){

       this.strategy = strategy;

    }

   

    publicvoid contextInterface(){

       strategy.algorithmlnterface();

    }

}

3、算法a

publicclass ConcreteStrategyAextends Strategy {

 

    @Override

    publicvoid algorithmlnterface() {

       //TODO Auto-generated method stub

 

       System.out.println("this is algorithm a ;");

    }

 

}

4、算法b

publicclass ConcreteStrategyBextends Strategy {

 

    @Override

    publicvoid algorithmlnterface() {

       //TODO Auto-generated method stub

 

       System.out.println("this is algorithm b ;");

    }

 

}

5、client调用

publicclass StrategyClient {

 

    /**

     * @param args

     */

    publicstaticvoid main(String[] args) {

       //TODO Auto-generated method stub

 

       /*

        * 对照与工厂模式,该client只依赖于Context类,解耦更加彻底

        * 添加一个算法只须要添加一个算法类就可以

        */

       Contextcontext = new Context(new ConcreteStrategyA());

       context.contextInterface();

       //看到了吧。想要B算法就直接实例一下就能够调用它的方法了

       Contextcontext2 = new Context(new ConcreteStrategyB());

       context2.contextInterface();

    }

 

}

总结:策略模式是一种定义一系列算法的方法,从概念上来看,全部这些算法完毕的都是同样的工作,仅仅是实现不同。它能够以同样的方式调用全部的算法,降低了各种算法类与使用算法类之间的耦合。

附上代码地址:http://download.csdn.net/detail/jzhf2012/8084405

&lt;四&gt;读&lt;&lt;大话设计模式&gt;&gt;之代理模式

        代理模式我想大家即便不熟悉也都听过吧,从字面意思上看就是替别人干活的,比方代理商。在项目的实际应用中也有非常多地方用到。比方spring通过代理模式生成对象等。代理模式的书面定... 查看详情

作业二

#include<stdio.h>intmain(){intgrade;printf("请输入你的成绩");scanf("%d",&grade);if(grade>=90&&grade<=100){printf("A");}elseif(grade>=80&&grade<90){printf("B");}elseif(gr 查看详情

vue根据数组中某一项的值进行排序(代码片段)

...码表格大概是这个样子,样式和图片在代码中简化了。&lt;tableclass="recommend_table"cellspacing="0"&gt;&lt;tr&gt;&lt;th&gt;股票&lt;/th&gt;&lt;th@click="sort(‘in_price‘)"&gt;入选价&lt;/th&gt;&lt;th@click="sort(‘now_pri... 查看详情

html中&lt;li&gt;&lt;/li&gt;中使用&lt;br&gt;和&lt;li&gt;&lt;/li&gt

</pre><pre><!DOCTYPEhtml><html><head> <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"> <title>欢迎到訪</title> <style 查看详情

apachehttpcomponents工具类[httputil](代码片段)

pom.xml&lt;dependency&gt;&lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;&lt;artifactId&gt;httpclient&lt;/artifactId&gt;&lt;version&gt;4.5.5&lt;/version&gt;&lt;/dependency&gt;&lt;dependency&gt;&lt;groupId&gt;org.... 查看详情

bootstrap&bxslider

Bootstrap规则一、响应式布局  @media二、图标、字体  @font-face三、基本使用实例:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><linkrel="stylesheet"href="boot 查看详情

滑动div函数

ThisassumesHTMLthatlookslikethis:<pre><code>&lt;divid=&quot;containing_div&quot;&gt;&lt;divid=&quot;div_1&quot;style=&quot;background-color:red&quot;class=&quot;visible_div&quot;&gt;&lt;p&gt;TestContent1&lt;/p&gt;&lt;/di... 查看详情

升级spring&集成redis二:spring4集成redis

1添加Redis依赖引入需要集成的redis版本<!--springredis2.8.2start--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</ 查看详情

xsspayload(大集合)

<script>alert(/xss/)</script><script>alert(&#38;XSS&#38;)</script><script>alert(&quot;XSS&quot;)</script>(代替被转义的“”)<INPUTtype="text"value='\\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRI... 查看详情

&lt;sub&gt;&lt;sup&gt;标签

今天无意中看到一个标签认为挺有意思的。平时用得好少。<sup><sub>上标签和下标签。感觉这个命名就是以bp来决定是上还是下。b是下。p是上哈哈,是不是非常有意思贴一段代码看看<html><body><p>Thistextcontains... 查看详情

vue插槽(代码片段)

...规范草案,将<slot>元素作为承载分发内容的出口。&lt;navigation-linkurl="/profile"&gt;YourProfile&lt;/navigation-link&gt;然后在<navigation-link>的模板中可能会写为:&lt;a:href="url"class="nav-link"&gt;&lt;slot&gt;&lt;/slot&... 查看详情

利用|,&amp;,^,~,&lt;&lt;,&gt;&gt;&gt;写出高效艺术的代码

简单介绍:大家在阅读源代码的时候常常会看到一些比方以下这样特别难理解的代码。cancelEvent.setAction(MotionEvent.ACTION_CANCEL|(motionEvent.getActionIndex()<<MotionEvent.ACTION_POINTER_INDEX_SHIFT));order=((order)>>(INDEX_OFFSET- 查看详情

vue中解析转义字符

...一下返回的富文本内容,发现里边包含了展示转义字符&lt;pstyle=&quot;line-height:150%&quot;&gt;&lt;spanstyle=&quot;;font-family:宋体;line-height:150%;font-size:16px&quot;&gt;&lt;spanstyle=&quot;font-family:宋体&quot;&gt;口腔... 查看详情

vue基础指令集锦(代码片段)

v-model双向绑定数据&lt;inputtype="text"v-model="msg"&gt;msg###v-on事件&lt;divid="box"&gt;&lt;buttonv-on:click="say"&gt;按钮&lt;/button&gt;&lt;button@click="say"&gt;按钮&lt;/button&gt;&lt... 查看详情

实验二

...tdio.h>intmain() doublex,y; charc1,c2,c3; inta1,a2,a3; scanf("%d%d%d",&a1,&a2,&a3); printf("a1=%d,a2=%d,a3=%d\\n",a1,a2,a3); getchar(); scanf("%c%c%c",&c1,&c2,&c3); getchar(); printf("c1=%c,c2=%c,c3=%c\\n",c1,c2,c3); scanf("%lf,%lf",&x,&y); printf("x=%lf,y=%lf\\... 查看详情

表格细边框的css样式怎么设置?

...blewidth="500"border="1"> <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr> &l... 查看详情

vue条件渲染(代码片段)

v-if字符串模板中,我们可以像这样写一个条件块:#ifok&lt;h1&gt;&lt;/h1&gt;/if在Vue中,我们使用v-if指令实现同样的功能:&lt;h1v-if="ok"&gt;&lt;/h1&gt;也可以使用v-else添加一个else块:&lt;h1v-if="ok"&gt;yes&lt;/h1... 查看详情

pagehelper分页工具(代码片段)

<a>共$page.total件商品</a>&ensp;&ensp;&ensp;<a>共$page.pages页</a>&ensp;&ensp;&ensp;<a>当前第$page.pageNum页</a>&ensp;&ensp;&ensp;<a>每页$page.pageSize条</a>&ensp;&ensp;&ensp;<ahref="showC... 查看详情