感受:将算法从方法级别,提升到类级别。之后利用java多态,来切换不同的算法实现不同的功能。

在阎宏博士的《JAVA与模式》一书中开头是这样描述策略(Strategy)模式的:

  策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。


策略模式的结构

  策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理。策略模式通常把一个系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。用一句话来说,就是:“准备一组算法,并将每一个算法封装起来,使得它们可以互换”。下面就以一个示意性的实现讲解策略模式实例的结构。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxsAAAD5CAIAAADA2L+KAAAgAElEQVR4nO3dMcjsWP3/8akebiNelGVXBPeKBgRBrthYpFgEwWILYyGWF9IINiIptNoqaCM2EbGQW6SwsFhSbbWktAks2Eiq7XK3s0hxy/yK7//5/r97kknyTCaTk8z7VSx358lkMpNvTj45OUlOHQAAAJY5bb0AAAAAu0eiAgAAWIpEBQAAsBSJCgAAYCkSFQAAwFIkKgAAgKVIVAAAAEuRqAAAAJYiUQEAACxFogIAAFiKRAUAALAUiQoAAGApEhUAAMBSJCoAAIClSFQAAABLkagAAACWmpWoXr58eQKWefny5drVDADAVmYlqtOJriwsRRUBAA6MRIUboYoAAAdGosKNUEUAgAMjUeFGqCIAwIGRqHAjVBEA4MBIVLgRqggAcGAkqq7rurqu67reeikO7vBVBAC4ZySqLoqiJEnSNA2CIMuyrRfnsI5dRQCAO3fviSqKIk1RZVnGcXzBTJIkWbIMZVlWVbVkDrtw4CoCAOCuE1Vd10EQ2FeKohiZvm3bwRfDMFyyGHEck6gAANi1u05UaZqeC0NJkoRhGEVRmqZd19V1HYZhHMdRFMnrMpm8cjqdoiiKokgHYxVFET3SKYMgCMMwy7IgCOT1PM+jKJLXoyjK83z177ydo1YRAAAdiWowUcmwKvl3HMcSdGRi6aYKw9D2KjkdXU3T6Gy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95Vyi0kbNHnfqi3IacbBtiqJo5vHonKbNzirP88HHfulSDcbB/vSDb5d4qn+15woHh+rLMy4ml39zI1X0+vXrFy9evHr1qizLV69evXjxglx1gVevXv3617/+5JNPZk5/4N1eFEVBENjDHhleKWft0zSVP8mLQRDI2ER5V9d1eZ7LZEmS6JYro0WDIJDhjNpoJEki/ethGOoWal8MwzBJEumclo1dZrv8amhvHbi0gBFXSFRhGOrD251IVJZl0zTacPR7kpqm0TwhyUCTinbG6JBt7eaRx2bpp8jo7zAMnQ75wXORfW3byiD68clk/LhM73yQDPCqqkpPLjhP7Oq6TgfIy2AyO+K+/3YZBdI9PqdMX9Rv2u+mmvyamxusIs1Sn3/+ub74+eefk6su8Pnnn//tb3/72c9+9vz581evXv3zn/98+/btyPTH3u3JfeOcTVvCkDOlhB75t2x3clmMvCKdUjqxc8q+LEt9rx0PoJPZT5SY1X358cmHdOzSAs65QqKS3uw8z/vxpSxLbYyappEpncnkjJXTHaVzdnJJ0zQ2i+goIhHHsW375g+YmPM40qZp5NMHO4rk8ai6VNKr73wdiUcymfMn+3adSZ7n+mKWZbYJtj1eNrP6zKmiwSxlkasu9r///e/169e/+tWvnj179vOf//z169dv3rzpT3YPu70sy06nk24g5xJV/8Usy6TX6nQ6jSQq6ffSK3P1r3raUXqn5MWmaWTLPfwtgu+htIC+3d8zXYed2svi9DYNmy7aNdnsKPeh6B57+Py/yk9oFU1mKYtctdDHH3/86tWr995778c//vFf/vIX+5v7vF0v4Yxqspe8zExUURRpYzLeRyV3M6kM/dAoiuzNU3TOchXOom/ovaOWFjBu94mqexxAant9pD9sw0VaiXxN25u1o+cMnk6nJ2Upi1y13L///e/f/va3L168+N73vvf73//+s88+83y7vphzTbGEGPm33tbEXgXsJKqmaWxsCoLA9gHb03nd47lF/ase9gRBkCRJv0tbbtS3i07lJY5aWsC4IyQq7MIJ/tm6KNYigab/FM6yLOV2vva6GRk5bqNPGIZyOys5kWevYpaeJ3t7FBnBKTFO+8VlnuEjZ9nW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ZeoLqvP/tgUncll9emslKcmqsH6dGpelvbchiBucxXVk+rTOevnSaKiFe1oRX1qRd15Xn2OUOMrTIa+dkMbc1mWTdM4Qxrte22RZVmWJIlub3rGXU8n6wFK0zTaNS0n4KMoCsPQ2SoGe9Gdwk2SxJ6utpuKLJgdFKkHgnme68ZgT3irtm2db6qblvR161hRXQAZiakTyxe0L+ovdvGdVIqiCIJgZnPg7Id0mI4dCSGcn1RGpOrHjR/53eB+VPPrs3+Mrvsq+dnzPNfVsbw+z53Quaw+bXeac+Kmv8bn16cOqrU1P7ghiCX1KaOLZk48vz6dKbWRkTG/47cnuNn9qGhFaUW3bUXdeV59jlDjK0z6VAdv+FaWpT32kinrurblKOfFnQMpnXN/S7DXrThnxJ1hH07J2gXQOVRVJW9p27aqKrt7sCMKVVVVdpFkhjLPwQ+y31Q2bPkIO738es6eqa7r/ovC7tf7HfLj5t8epn/IKIvUX0fyTfs/XdM0kxfD3yBRzazPpmn0uzhv1/c6v8nC+nRusXOV+izL0u56B9fOU+uzv0PqehuCdXF9Nk0z2FExaE59nvumuuJuU59zZkUrSiu6bSvqzvPqc4Ty+W6/2n9rL+hIHi+D33TRVsQTqa4+k5UM1mcURVmWrXHvJU9Qn6vO6upoRXeNRLUzPrcFXdc5B0zdY3/DhouEmQ6fqLqh+nQORuGtO0lUHa3onpGodsbztgD7dQ+JCvt1P4kK+0Wi2hnaAqyERAWfkajgPxLVztAWYCUkKviMRAX/kah2hrYAKyFRwWckKviPRLUztAVYCYkKPiNRwX8kqp15/vz5CVjB8+fPqU946yr1SZViVVesUkWiAgAAWIpEBQAAsBSJCgAAYCkSFQAAwFIkKgAAgKVIVAAAAEuRqAAAAJYiUQEAACxFogIAAFiKRAUAALAUiQoAAGApEhUAAMBSJCoAAICl/g9V2E9kgpba6AAAAABJRU5ErkJggg==" alt="" />

  这个模式涉及到三个角色:

  ●  环境(Context)角色:持有一个Strategy的引用。

  ●  抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。

  ●  具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。

源代码

  环境角色类

public class Context {
//持有一个具体策略的对象
private Strategy strategy;
/**
* 构造函数,传入一个具体策略对象
* @param strategy 具体策略对象
*/
public Context(Strategy strategy){
this.strategy = strategy;
}
/**
* 策略方法
*/
public void contextInterface(){ strategy.strategyInterface();
} }

  抽象策略类

public interface Strategy {
/**
* 策略方法
*/
public void strategyInterface();
}

  具体策略类

public class ConcreteStrategyA implements Strategy {

    @Override
public void strategyInterface() {
//相关的业务
} }
public class ConcreteStrategyB implements Strategy {

    @Override
public void strategyInterface() {
//相关的业务
} }
public class ConcreteStrategyC implements Strategy {

    @Override
public void strategyInterface() {
//相关的业务
} }

使用场景

  假设现在要设计一个贩卖各类书籍的电子商务网站的购物车系统。一个最简单的情况就是把所有货品的单价乘上数量,但是实际情况肯定比这要复杂。比如,本网站可能对所有的高级会员提供每本20%的促销折扣;对中级会员提供每本10%的促销折扣;对初级会员没有折扣。

  根据描述,折扣是根据以下的几个算法中的一个进行的:

  算法一:对初级会员没有折扣。

  算法二:对中级会员提供10%的促销折扣。

  算法三:对高级会员提供20%的促销折扣。

  使用策略模式来实现的结构图如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3YAAADuCAIAAAD6EgJpAAAgAElEQVR4nO3dIajs2P3A8VGXNYVnlm0pdJ8J/GGhXFpTEbFUVaxoTKl8EFOoKSWiVatCZU1KqShPRFRUlKhVJbImsFBTotZl11WMWJm/+HF//c05mcyZyUzmTO73Ix5z8zLJmeSXk1/OOUl2IwAAAHBVu3sXAAAAAFtDigkAAIArI8UEAADAlZFiAgAA4MpIMQEAAHBlpJgAAAC4MlJMAAAAXBkpJgAAAK7sRIr5/Py8A5Z5fn5eJ5oBAEAkTqSYux3NnFiKKAIA4LUhxcTNEUUAALw2pJi4OaIIAIDXhhQTN0cUAQDw2pBi4uaIIgAAXhtSTNwcUQQAwGtDirnIMAz3LsI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SffcMNMrGRnNVte13Sy3CDNnmcvD7KwbZWILM21R0z3yECKJ9vFUSSajSyNW7sWx9arfxKgjC+u6luU0TTMZQs5YZDvdSUn9ZY6zkbDkuZhN0+jI4Bl6fpH78yS8NVm0l08ycKvrOrl1T76lv3G/3zvpr39amTm0w5FiAgdOHhJyN6JzzV3Xtd6+pxOli6HvezuzjC6ys8k8dhXyLFyZ7lQ6k8uUAvhpYtu2x3JHrV/ObVvqui6wS71pGr/8Xdc5t5VIGuQ3QMqGcjbd5GYZhkHWFVKqSE66F4SZjRw9GdwizI5NXxJmk1+c//mxhZk/Z/wiifYxoCST0SVXBeNLu+Z4GJzO3nRyvq7r/B5e+a5/LBybPnlFMRMJ9trv3IcPBF669C+vnpo53rXwklnqnJqy2+08v0kXvpWDFBM4EE+lfGtSd9zi3YMxi2T/RlKMFfCO8juKJ8ziKcmt8Y5yixQTOPB6qsLXKZL9G0kxsG3xhFk8JcGaSDGBA1SF2xbJ/o2kGNi2eMIsnpJgTaSYwAGqwm2LZP9GUgxsWzxhFk9JsCZSTOAAVeG2RbJ/IykGti2eMIunJFgTKSZwgKpw2yLZv5EUA9sWT5jFUxKsiRQTOEBVuG2R7N9IioFtiyfM4ikJ1kSKCRx48+bNDtv15s2be4fYOBJmWEUk0T4S8K/VHSOQFBMAAABXRooJAACAKyPFBAAAwJWRYgIAAODKSDEBAABwZaSYAAAAuDJSTAAAAFwZKSYAAACujBQTAAAAV0aKCQAAgCv7f+DtekUdpz97AAAAAElFTkSuQmCC" alt="" />

源代码

  抽象折扣类

public interface MemberStrategy {
/**
* 计算图书的价格
* @param booksPrice 图书的原价
* @return 计算出打折后的价格
*/
public double calcPrice(double booksPrice);
}

  初级会员折扣类

public class PrimaryMemberStrategy implements MemberStrategy {

    @Override
public double calcPrice(double booksPrice) { System.out.println("对于初级会员的没有折扣");
return booksPrice;
} }

  中级会员折扣类

public class IntermediateMemberStrategy implements MemberStrategy {

    @Override
public double calcPrice(double booksPrice) { System.out.println("对于中级会员的折扣为10%");
return booksPrice * 0.9;
} }

  高级会员折扣类

public class AdvancedMemberStrategy implements MemberStrategy {

    @Override
public double calcPrice(double booksPrice) { System.out.println("对于高级会员的折扣为20%");
return booksPrice * 0.8;
}
}

  价格类

public class Price {
//持有一个具体的策略对象
private MemberStrategy strategy;
/**
* 构造函数,传入一个具体的策略对象
* @param strategy 具体的策略对象
*/
public Price(MemberStrategy strategy){
this.strategy = strategy;
} /**
* 计算图书的价格
* @param booksPrice 图书的原价
* @return 计算出打折后的价格
*/
public double quote(double booksPrice){
return this.strategy.calcPrice(booksPrice);
}
}

  客户端

public class Client {

    public static void main(String[] args) {
//选择并创建需要使用的策略对象
MemberStrategy strategy = new AdvancedMemberStrategy();
//创建环境
Price price = new Price(strategy);
//计算价格
double quote = price.quote(300);
System.out.println("图书的最终价格为:" + quote);
} }

  从上面的示例可以看出,策略模式仅仅封装算法,提供新的算法插入到已有系统中,以及老算法从系统中“退休”的方法,策略模式并不决定在何时使用何种算法。在什么情况下使用什么算法是由客户端决定的。

认识策略模式

  策略模式的重心

  策略模式的重心不是如何实现算法,而是如何组织、调用这些算法,从而让程序结构更灵活,具有更好的维护性和扩展性。

  算法的平等性

  策略模式一个很大的特点就是各个策略算法的平等性。对于一系列具体的策略算法,大家的地位是完全一样的,正因为这个平等性,才能实现算法之间可以相互替换。所有的策略算法在实现上也是相互独立的,相互之间是没有依赖的。

  所以可以这样描述这一系列策略算法:策略算法是相同行为的不同实现。

  运行时策略的唯一性

  运行期间,策略模式在每一个时刻只能使用一个具体的策略实现对象,虽然可以动态地在不同的策略实现中切换,但是同时只能使用一个。

  公有的行为

  经常见到的是,所有的具体策略类都有一些公有的行为。这时候,就应当把这些公有的行为放到共同的抽象策略角色Strategy类里面。当然这时候抽象策略角色必须要用Java抽象类实现,而不能使用接口。

  这其实也是典型的将代码向继承等级结构的上方集中的标准做法。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj0AAAF5CAIAAAD70gyxAAAgAElE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Z23cU2/cqBdakFw4a+OeeuNGvdCC5MJZG/fUGzfqhRYkF+7s7Cyx5Ozs7Ngv+UGRb9wS+pZaJBfO2rin3rhRL7QguXDWxj31xo16oQXJhbM27qk3btQLLUgunLVxT71xo15oQXLhrI176o0b9UILkgtnbdxTb9yoF1qQXDhr455640a90ILkwlkb99QbN+qFFiQXztq4p964US+0ILlw1sY99caNeqEFyYWzNu6pN27UCy1ILpy1cU+9caNeaEFy4ayNe+qNG/VCC5ILZ23cU2/cqBdakFw4a+OeeuNGvdCC5MJZG/fUGzfqhRYkF87auKfeuFEvtCC5cHwebtzIN24JfUstkgtnbdxTb9yoF1qQXDhr455640a90ILkwlkb99QbN+qFFiQXztq4p964US+0ILlw1sY99caNeqEFyYWzNu6pN27UCy1ILpy1cU+9caNeaEFy4ayNe+qNG/VCC5ILZ23cU2/cqBdakFw4a+OeeuNGvdCC5MJZG/fUGzfqhRYkF87auKfeuFEvtCC5cNbGPfXGjXqhBcmFszbuqTdu1AstSC6ctXFPvXGjXmhBcuGsjXvqjRv1QguSC2dt3FNv3KgXWpBcuPPz810+TnY2N27cOMr/e35+fuyX/KCOle+xWMs3oW+pRXLKfP311x9++OEnn3xy7B0BdKNv6UVymvz999/vvvvuer3+6KOPfv3112PvDqAYfUsvktPk9u3bT548GYbh2bNnb7/99rF3B1CMvqUXyanx8OHDr776yv/zxx9//Oabb464P4Bq9C29SE6HV69enZ+fv3z50j+yXq9v3rz5/PnzI+4VoBd9Sy+S0+Gzzz77+eefJw/++eefH3zwwVH2B9COvqUXySnwxx9/3Llz58Jvff75548ePTrw/gARoG/pRXIKvPPOO0+fPr3wWy9evLh58+arV68OvEuAdvQtvUju1H3//ff379+/YoNHjx598cUXh9odIBL0Lb1I7qQ9f/781q1bVx9Ordfr999//6+//jrYXgERoG/pRXIn7eOPP/7tt9/euFnTNO+9994B9geIBn1LL5I7XY8fP/7000+33Pjbb7/94Ycf9ro/QEzoW3qR3Ona9e+63rhx49i7DKhB39KL5PRhvg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ArSURBVKAJfQsAoAl9CwCgCX0LAKAJfQsAoAl9CwCgCX0LAKAJfQsAoMn/AMf9A0W3O98gAAAAAElFTkSuQmCC" alt="" />

策略模式的优点

  (1)策略模式提供了管理相关的算法族的办法。策略类的等级结构定义了一个算法或行为族。恰当使用继承可以把公共的代码移到父类里面,从而避免代码重复。

  (2)使用策略模式可以避免使用多重条件(if-else)语句。多重条件语句不易维护,它把采取哪一种算法或采取哪一种行为的逻辑与算法或行为的逻辑混合在一起,统统列在一个多重条件语句里面,比使用继承的办法还要原始和落后。

策略模式的缺点

  (1)客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道算法或行为的情况。

  (2)由于策略模式把每个具体的策略实现都单独封装成为类,如果备选的策略很多的话,那么对象的数目就会很可观。

转自:http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html

java_设计模式_策略模式_Strategy pattern(2016-07-15)的更多相关文章

  1. java_设计模式_状态模式_State Pattern(2016-08-16)

    定义: 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 类图: 状态模式所涉及到的角色有: ● 环境(Context)角色,也成上下文:定义客户端所感兴趣的接口,同时维护一个 ...

  2. java_设计模式_组合模式_Composite Pattern(2016-08-12)

    概念: 组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部分-整体模式,它使我们树 ...

  3. java_设计模式_迭代器模式_Iterator Pattern(2016-08-12)

    迭代子(Iterator)模式又叫游标(Cursor)模式,是对象的行为模式. 定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节. 类型:行为类模式 类图: 如果要问java中 ...

  4. java_设计模式_命令模式_Command Pattern(2016-08-09)

    理解还不到位,先窜出来.等过一阵子再看,再理解. 定义:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能. 类型:行为类模式 类 ...

  5. java_设计模式_工厂模式_Factory Pattern(2016-08-04)

    工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...

  6. java_设计模式_外观模式_Facade Pattern(2016-08-09)

    外观模式/门面模式 1.概念 为子系统中的一组接口提供一个统一接口.Facade模式定义了一个高层接口,这个接口使得这子系统更容易使用. 2.UML 由于外观模式的结构图过于抽象,因此把它稍稍具体点. ...

  7. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  8. 反馈法学习设计模式(一)——策略模式Strategy Pattern

    简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...

  9. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

随机推荐

  1. HDU 5933 ArcSoft's Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))

    ArcSoft's Office Rearrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  2. Connection 和Dispose的学习日志

  3. Selenium 处理模态对话框

    模态对话框的原理 模态窗口 点击下一步的动作为,聚焦到“下一步”,然后直接回车 driver.FindElement(By.CssSelector("div.rg_btn a")) ...

  4. java---金额中文大小写实时转换小程序

    本例演示一个String图形用户界面的完整应用程序,包括输入数据, 显示结果,响应事件以及异常处理. 输入一个实数表示金额,每次输入时显示该金额的中文大小写形式,运行窗口见最后. 在窗口中," ...

  5. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. reloadData should be in main thread

    reloadData should be called in main thread, so if you call it in work thread, you should call it as ...

  7. C# 模拟用户登录

    , data.Length);            newStream.Close();                               request.CookieContainer  ...

  8. IE8下提示&#39;console&#39;没有定义错误

    在开发的过程中因为调试的原因,在代码中增加console.info("xxxx"),而未进行删除 在IE8下測试该代码所在的页面报错,例如以下: 须要注意的是,使用console对 ...

  9. hadoop小文件合并

    1.背景 在实际项目中,输入数据往往是由许多小文件组成,这里的小文件是指小于HDFS系统Block大小的文件(默认128M), 然而每一个存储在HDFS中的文件.目录和块都映射为一个对象,存储在Nam ...

  10. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...