行为型模式-策略
来源:互联网 发布:nginx 隐藏源ip 编辑:程序博客网 时间:2024/05/18 10:48
结构图
模式说明
类设计
模式说明
- 扩展点在每一个具体的Strategy实现类。
- 一个Context上下文持有一个具体Strategy实现类的引用。
- 客户端可以替换Strategy的实现类,不改变算法的变化。
- 定义一系列的算法,把它们一个个封装起来,并使它们可以相互替换,该模式使得算法可独立于使用它的客户而变化。
public static void main(String[] args) {Context context;// 由于实例化不同的策略,所以最终调用context.contextInterface()// 时,所获得的结果就不相同context = new Context(new ConcreteStrategyA());context.contextInterface();// use B algorithmcontext = new Context(new ConcreteStrategyB());context.contextInterface();// use C algorithmcontext = new Context(new ConcreteStrategyC());context.contextInterface();}
类设计
public interface Strategy {public void algorithmInterface();}public class Context {private Strategy strategy;public Context(Strategy strategy) {this.strategy = strategy;}public void contextInterface() {strategy.algorithmInterface();}}public class ConcreteStrategyA implements Strategy {@Overridepublic void algorithmInterface() {System.out.println("algorithm A");}}public class ConcreteStrategyB implements Strategy {@Overridepublic void algorithmInterface() {System.out.println("algorithm B");}}public class ConcreteStrategyC implements Strategy {@Overridepublic void algorithmInterface() {System.out.println("algorithm C");}}
0 0
- 行为型-策略模式
- 行为型模式-策略
- 行为型模式--策略模式
- 行为型模式-策略模式
- 行为型模式--策略模式
- 策略模式(行为型)
- 策略模式(行为型)
- 策略模式(行为型)
- 13-行为型-策略模式
- 行为型之策略模式
- 行为型模式-策略(strategy)
- 设计模式 - 行为型模式 - 策略模式
- 设计模式->行为型模式->策略模式
- 行为型模式--策略模式(Strategy)
- 设计模式--行为型--策略模式
- 设计模式14:策略模式【行为型】
- 行为型模式-----策略模式(Strategy)
- 策略模式(行为型模式)
- ScheduledThreadPool解读
- 二叉树的非递归遍历
- runtime
- C 可变参数
- 【LeetCode从零单排】No15 3Sum
- 行为型模式-策略
- ThreadPoolExecutor解读
- 浅析同义词--Synonym
- MarkDown编辑器
- CyclicBarrier解读
- MVC入门教程八[MvcPager分页控件的使用]
- CountDownLatch解读
- Android 随机数的生成
- 简单模拟对大数据进行排序的整个过程