策略模式(Strategy)
来源:互联网 发布:梦龙网络计划软件图解 编辑:程序博客网 时间:2024/06/09 16:30
* 策略模式:对象的行为模式
* 针对一组算法,将每一组算法封装到具有共同接口的独立的类中,从而使得他们可以相互替换
* 特点:
* 抽象策略角色:通常用一个抽象类或者接口来实现,主要是定义这个算法所完成的功能
* 具体策略角色:包装了相关算法和行为
* 环境角色:持有策略类的引用
* 优点:提供管理相关算法族的办法,提供可替代继承关系的办法
* 避免了使用多重条件判断语句
* 缺点: 客户端必须知道所有的策略类,自己去决定使用那一个 造成很策略类
*/
/**
* 抽象策略类 ---待实现的方法
* @author Rain
*/
public interface Calculate {
int calculate(int a,int b);
}
/**
* 加法策略类 ---具体策略类
* @author Rain
*/
public class AddCaculate implements Calculate{
@Override
public int calculate(int a, int b) {
return a+b;
}
}
/**
* 除法策略类 ---具体策略类
* @author Rain
*/
public class DivideCaculate implements Calculate{
@Override
public int calculate(int a, int b) {
return a/b;
}
}
/**
* 环境
* @author Rain
*/
public class CalculateContext {
private Calculate calculate;
/**
* 构造方法赋值
* @param calculate
*/
public CalculateContext(Calculate calculate) {
this.calculate=calculate;
}
/**
* 获得策略对象
*/
public Calculate getCaculate(){
return calculate;
}
/**
* 环境角色的接口方法,具体结果依赖于策略引用指向具体的策略对象
*/
public int calcuate(int a,int b){
return calculate.calculate(a, b);
}
}
- 策略模式 Strategy模式
- Strategy模式 策略模式
- 策略模式(Strategy模式)
- 策略模式(Strategy Pattern)
- Strategy策略模式
- 策略(Strategy)模式
- Strategy策略模式
- 策略(Strategy)模式
- 策略模式(Strategy Pattern)
- 策略模式 Strategy Pattern
- 策略模式(Strategy Pattern)
- 策略模式(Strategy)
- HeadFirst Strategy策略模式
- 策略模式(Strategy Pattern)
- Strategy Pattern (策略模式)
- 泛化策略模式(Strategy)
- Strategy 策略模式
- Strategy 策略模式
- linux本端对端都需要安装scp
- Oracle Database Support Timeline
- velocity与jsp的区别
- Struts 2 实例--Hello World
- OpenCV 2.4+ C++ SVM文字识别
- 策略模式(Strategy)
- ibatis学习笔记(一)>>>>>>>sqlMapConfig.xml文件详解
- 关于linux mmc/sd驱动程序架构
- 拷贝构造函数的参数类型必须是引用
- Choosing between Critical Sections and Mutex
- 你知道HTML标签设计的本意吗?
- 监控Linux
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
- hdu 1014 数学