设计模式学习笔记七:策略模式
2015-02-15 11:32
393 查看
策略模式,它主要的作用是封装算法,是一种行为模式。
有三种角色:
1、具体算法角色:具体的算法的实现;
2、抽象策略角色:抽象类或接口,提供具体算法角色的抽象;
3、上下文角色:实现对具体算法角色的引用。
更详细定义参照:策略模式。
代码时间:
1、具体实现:
2、具体算法策略:
3、抽象接口:
3、策略引用:
有三种角色:
1、具体算法角色:具体的算法的实现;
2、抽象策略角色:抽象类或接口,提供具体算法角色的抽象;
3、上下文角色:实现对具体算法角色的引用。
更详细定义参照:策略模式。
代码时间:
1、具体实现:
package com.array7.strategy; public class Run { public static void main(String[] args) { new Context(new Strategy1()).execute(); new Context(new Strategy2()).execute(); new Context(new Strategy3()).execute(); } }
2、具体算法策略:
package com.array7.strategy; public class Strategy1 implements IStrategy { @Override public void execute() { System.out.println("stargtegy1.execute..."); } } package com.array7.strategy; public class Strategy2 implements IStrategy { @Override public void execute() { System.out.println("stargtegy2.execute..."); } } package com.array7.strategy; public class Strategy3 implements IStrategy { @Override public void execute() { System.out.println("stargtegy3.execute..."); } }
3、抽象接口:
package com.array7.strategy; public interface IStrategy { void execute(); }
3、策略引用:
package com.array7.strategy; public class Context { private IStrategy strategy; public Context(IStrategy strategy) { this.strategy = strategy; } public void execute() { this.strategy.execute(); } }
相关文章推荐
- java 设计模式 学习笔记(4) 策略模式
- HeadFirst 设计模式学习笔记1--策略模式
- 设计模式学习笔记(六)之策略模式(Strategy)
- 设计模式学习笔记二:Strategy,策略模式
- 设计模式学习笔记之策略模式
- 设计模式学习笔记--Strategy 策略模式
- 设计模式学习笔记(5) - 策略模式
- 《Head First 设计模式》学习笔记:策略模式与观察者模式
- Head First_设计模式_学习笔记_第01章_策略模式
- 设计模式学习笔记(六)之策略模式(Strategy)
- 设计模式学习笔记二【策略模式】
- HeadFirst 设计模式学习笔记1--策略模式
- 二 策略模式——设计模式学习笔记
- java 设计模式学习笔记十五 strategy 策略设计模式
- 步步为营 .NET 设计模式学习笔记 三、Strategy(策略模式)
- 设计模式学习笔记(二):策略模式
- 学习笔记 ---- 设计模式之策略模式
- 步步为营 .NET 设计模式学习笔记 三、Strategy(策略模式)
- 设计模式学习笔记一 简单工厂策略模式实现销售策略的变更(一)
- 设计模式学习笔记-策略模式