标签:style text enc 参考资料 配置 and nali cat repr
策略模式(Strategy Pattern)
The intent of the Strategy Pattern is to define a family of algorithms, encapsulate each algorithm, and make them interchangeable. The Strategy Pattern lets the algorithm vary independently from clients that use it. In addition the pattern, defines a group of classes that represent a set of possible behaviors. These behaviors can then be used in an application to change its functionality.
1、Strategy
-定义所有支持的算法的公共接口。Context使用这个接口来调用某ConcreteStrategy定义的算法。
2、ConcreteStrategy
-以Strategy接口实现某具体算法。
3、ConText
-用一个ConcreteStrategy对象来配置。
-维护一个对Strategy对象的引用。
-可定义一个接口来让Strategy访问它的数据。
interface Strategy { public void method(); }
class ConcreteStrategyA implements Strategy { public void method() { System.out.println("A method!"); } }
class ConcreteStrategyB implements Strategy { public void method() { System.out.println("B method"); } }
class Context { private Strategy strategy; public Strategy getStrategy() { return strategy; } public void setStrategy(Strategy strategy) { this.strategy=strategy; } public void method() { strategy.method(); } }
《设计模式:可复用面向对象软件的基础》
标签:style text enc 参考资料 配置 and nali cat repr
原文地址:https://www.cnblogs.com/diameter/p/13204500.html