23种设计模式 第三部分 关系模式(1)策略模式
来源:互联网 发布:containsall java 编辑:程序博客网 时间:2024/06/05 15:37
理解
策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。需要设计一个接口,为一系列实现类提供统一的方法,多个实现类实现该接口,设计一个抽象类(可有可无,属于辅助类),提供辅助函数,关系图如下
图中ICalculator提供统一的方法,
AbstractCalculator是辅助类,提供辅助方法,接下来,依次实现下每个类:
首先统一接口:
public interface ICalculator { public int calculate(String exp); }
辅助类:
public abstract class AbstractCalculator { public int[] split(String exp,String opt){ String array[] = exp.split(opt); int arrayInt[] = new int[2]; arrayInt[0] = Integer.parseInt(array[0]); arrayInt[1] = Integer.parseInt(array[1]); return arrayInt; } }三个实现类:
public class Plus extends AbstractCalculator implements ICalculator { @Override public int calculate(String exp) { int arrayInt[] = split(exp,"\\+"); return arrayInt[0]+arrayInt[1]; } }
public class Minus extends AbstractCalculator implements ICalculator { @Override public int calculate(String exp) { int arrayInt[] = split(exp,"-"); return arrayInt[0]-arrayInt[1]; } }
public class Multiply extends AbstractCalculator implements ICalculator { @Override public int calculate(String exp) { int arrayInt[] = split(exp,"\\*"); return arrayInt[0]*arrayInt[1]; } }测试类
public class StrategyTest { public static void main(String[] args) { String exp = "2+8"; ICalculator cal = new Plus(); int result = cal.calculate(exp); System.out.println(result); } }输出:10
策略模式的决定权在用户,系统本身提供不同算法的实现,新增或者删除算法,对各种算法做封装。因此,策略模式多用在算法决策系统中,外部用户只需要决定用哪个算法即可。
其他资料
Intent
- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
- Capture the abstraction in an interface, bury implementation details in derived classes.
Example
A Strategy defines a set of algorithms that can be used interchangeably. Modes of transportation to an airport is an example of a Strategy. Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service. For some airports, subways and helicopters are also available as a mode of transportation to the airport. Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably. The traveler must chose the Strategy based on trade-offs between cost, convenience, and time.
- Define the interface of an interchangeable family of algorithms
- Bury algorithm implementation details in derived classes
- Derived classes could be implemented using the Template Method pattern
- Clients of the algorithm couple themselves strictly to the interface
// 1. Define the interface of the algorithminterface Strategy { public void solve(); } // 2. Bury implementationabstract class TemplateMethod1 implements Strategy { // 3. Template Method public void solve() { start(); while (nextTry() && ! isSolution()) ; stop(); } protected abstract void start(); protected abstract boolean nextTry(); protected abstract boolean isSolution(); protected abstract void stop();}class Impl1 extends TemplateMethod1 { private int state = 1; protected void start() { System.out.print( "start " ); } protected void stop() { System.out.println( "stop" ); } protected boolean nextTry() { System.out.print( "nextTry-" + state++ + " " ); return true; } protected boolean isSolution() { System.out.print( "isSolution-" + (state == 3) + " " ); return (state == 3); }}// 2. Bury implementationabstract class TemplateMethod2 implements Strategy { // 3. Template Method public void solve() { while (true) { preProcess(); if (search()) break; postProcess(); } } protected abstract void preProcess(); protected abstract boolean search(); protected abstract void postProcess();}class Impl2 extends TemplateMethod2 { private int state = 1; protected void preProcess() { System.out.print( "preProcess " ); } protected void postProcess() { System.out.print( "postProcess " ); } protected boolean search() { System.out.print( "search-" + state++ + " " ); return state == 3 ? true : false; }}// 4. Clients couple strictly to the interfacepublic class StrategyDemo { public static void clientCode( Strategy strat ) { strat.solve(); } public static void main( String[] args ) { Strategy[] algorithms = { new Impl1(), new Impl2() }; for (int i=0; i < algorithms.length; i++) { clientCode( algorithms[i] ); } }}
Output:
start nextTry-1 isSolution-false nextTry-2 isSolution-true stoppreProcess search-1 postProcess preProcess search-2
https://sourcemaking.com/design_patterns/strategy/java/1
- 23种设计模式 第三部分 关系模式(1)策略模式
- 23种设计模式 第三部分 关系模式(2)模板方法模式
- 23种设计模式 第三部分 关系模式(3)观察者模式
- 23种设计模式 第三部分 关系模式(4)迭代子模式
- 23种设计模式 第三部分 关系模式(5)责任链模式
- 23种设计模式 第三部分 关系模式(6)命令模式
- 《设计模式解析》第三部分 设计模式
- 《设计模式解析》第三部分 设计模式
- 第三个设计模式:策略模式
- 第三个设计模式:策略模式
- 设计模式之策略模式(第三篇,转载)
- 23种设计模式02---策略模式
- 23种设计模式-12-策略模式
- 23种设计模式之策略模式
- 23种设计模式之策略模式
- 23种设计模式之策略模式
- 23种设计模式之策略模式
- 23种设计模式-策略模式
- android 6.0 activitymanagerservice keyword
- List.Sort 排序
- 2016.08.24 之感
- NIO中的缓冲区
- 三元运算符
- 23种设计模式 第三部分 关系模式(1)策略模式
- 通过SpannableString为TextView设置丰富的显示效果
- Spark 分布式集群环境搭建
- POJ 3436 ACM Computer Factory(网络流 拆点)
- 是走是留,大三的一个交待
- 简单理解设计模式之工厂模式(简单工厂方法)
- TCP协议的滑动窗口具体是怎样控制流量的?
- AngularJS Error: [ng:areq] loginController is not a function
- 29. Divide Two Integers