可信的品牌网站建设,设计网站printerest,阿里巴巴网站威海哪里做,中关村在线官网入口策略模式是一种行为设计模式#xff0c;就是定义一系列算法#xff0c;然后将每一个算法封装起来#xff0c;并使它们可相互替换。本模式通过定义一组可相互替换的算法#xff0c;实现将算法独立于使用它的用户而变化。
Strategy is a behavioral design pattern that def…策略模式是一种行为设计模式就是定义一系列算法然后将每一个算法封装起来并使它们可相互替换。本模式通过定义一组可相互替换的算法实现将算法独立于使用它的用户而变化。
Strategy is a behavioral design pattern that defines a series of algorithms, then encapsulates each algorithm and
makes them interchangeable. This pattern enables algorithms to vary independently of the users who use them by
defining a set of interchangeable algorithms. 结构设计
为实现一系列可相互替换的算法可定义一个公共接口然后定义一组实现该接口的具体策略这样就可在上下文中使用该接口调用具体策略上定义的算法。 Context上下文维护指向具体策略的引用且仅通过策略接口与该对象进行交流。上下文可以维护一个对策略对象的引用这符合组合设计原则。 上下文定义了一个接口以封装对策略对象的访问。如果策略对象直接暴露给外部使用会导致其内部实现细节的暴露从而增加接口使用难度。 Strategy策略基类或策略接口声明了一个上下文用于执行策略的方法。 ConcreteStrategy具体策略类实现了策略类声明的方法。 策略模式类图表示如下
伪代码实现
接下来将使用代码介绍下策略模式的实现。
// 1、抽象策略类声明执行策略的方法
public interface IStrategy {void operation(String paramStr);
}
// 2、具体策略类A实现策略接口声明的方法
public class ConcreteAStrategy implements IStrategy {Overridepublic void operation(String paramStr) {System.out.println(do some thing in the concrete A instance);}
}
// 2、具体策略类B实现策略接口声明的方法
public class ConcreteBStrategy implements IStrategy {Overridepublic void operation(String paramStr) {System.out.println(do some thing in the concrete B instance);}
}
// 3、策略上下文维护指向具体策略的引用且仅通过策略接口与该对象进行交流。这里提供两种使用策略类的方式。
public class StrategyContext {private static final MapString, IStrategy STRATEGY_MAP;private IStrategy strategy;static {STRATEGY_MAP new HashMap();STRATEGY_MAP.put(type A, new ConcreteAStrategy());STRATEGY_MAP.put(type B, new ConcreteBStrategy());}public StrategyContext() {}public StrategyContext(IStrategy strategy) {this.strategy strategy;}public void doSomething(String paramStr) {strategy.operation(paramStr);}public void doSomething(String strategyType, String paramStr) {IStrategy currentStrategy STRATEGY_MAP.get(strategyType);if (Objects.isNull(currentStrategy)) {throw new RuntimeException(strategy is null);}currentStrategy.operation(paramStr);}
}
// 4、策略模式客户端
public class StrategyClient {public void test() {StrategyContext strategyContextA new StrategyContext(new ConcreteAStrategy());strategyContextA.doSomething(TEST);StrategyContext strategyContextB new StrategyContext();strategyContextB.doSomething(type B, TEST);}
}适用场景
在以下情况下可以考虑使用策略模式 (1) 如果在一个系统里面有许多类它们之间的区别仅在于它们的行为那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。 (2) 一个系统需要动态地在几种算法中选择一种 可考虑使用策略模式。 策略模式能够将对象关联至可以不同方式执行特定子任务的不同子对象从而以间接方式在运行时更改对象行为。 (3) 当类中使用了复杂条件运算符(如多重的条件选择语句)以在同一算法的不同变体中切换时 可使用该模式。 策略模式可将所有继承自同样接口的算法抽取到独立类中因此可以不需要条件语句。原始对象并不实现所有算法的变体而是将执行工作委派给其中的一个独立算法对象。 (4) 不希望客户端知道复杂的、与算法相关的数据结构在具体策略类中封装算法和相关的数据结构提高算法的保密性与安全性。 策略模式让你能将各种算法的代码、 内部数据和依赖关系与其他代码隔离开来。 不同客户端可通过一个简单接口执行算法 并能在运行时进行切换。
优缺点
策略模式有以下优点 (1) 符合开闭原则。可以在不修改原有系统的基础上选择算法或行为也可以灵活地增加新的算法或行为。 (2) 定义一系列可重用的算法。策略模式提供了管理相关的算法族的办法。 (3) 使用组合来代替继承。实现支持多种算法或行为的方法。 (4) 避免使用多重条件语句。当不同的行为堆砌在一个类时很难避免使用条件语句来选择合适的行为。如果将行为封装在一个个独立的Strategy类中则可消除这些条件语句。 如使用字典的初始化从文件中读取的方式就可将策略配置移除到外部从而进一步减少不必要的代码修改。 但是策略模式也存在以下缺点 (1) 如果使用的算法极少发生改变那么没有任何理由引入新的类和接口。使用策略模式只会让程序过于复杂。 (2) 策略模式将造成产生很多策略类可以通过使用享元模式在一定程度上减少对象的数量。 (3) 许多现代编程语言支持函数类型功能允许在一组匿名函数中实现不同版本的算法。这样就可以使用这些函数来替换策略对象无需借助额外的类和接口来保持代码简洁。如在Java语言中是Lambda表达式在C语言中是函数指针。
参考
《设计模式 可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著, 李英军, 马晓星等译 https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/strategy.html 策略模式 https://refactoringguru.cn/design-patterns/strategy 策略模式 https://www.runoob.com/design-pattern/strategy-pattern.html 策略模式 https://www.cnblogs.com/adamjwh/p/11011095.html 简说设计模式——策略模式 https://blog.csdn.net/ShuSheng0007/article/details/88085445 秒懂设计模式之策略模式