设计模式–策略模式
策略模式
定义:该模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响使用算法的客户。策略模式属于对象行为模式,它通过对算法进行封装,把使用算法的责任和算法的实现分割开来,并委派给不同的对象对这些算法进行管理。
主要结构
策略模式的主要角色:
- 抽象策略类(strategy):这是一个抽象角色,通常由一个接口或者抽象类实现。此角色给出所有的具体策略类所需的接口。
- 具体策略类(concrete strategy):实现了抽象策略定义的接口,提供具体的算法实现或行为。
- 环境类(context):持有一个策略类的引用,最终给客户端调用。
案例实现
【例】临近年终了,需要给不同的员工计算其年假天数。针对不同类型的员工,其年假规则不一致,由人事将计算出来的年假展示出来。
代码实现:
年假天数计算(抽象策略类)AnnualLeave
接口:
/**
* 年假接口
* @author zlt
*/
public interface AnnualLeave {
/**
* 通过工作年限计算年假天数
* @param yearsOfJob 员工工作年限
* @return 年假天数
*/
Integer countAnnualLeave(Integer yearsOfJob);
}
一线开发年假天数(具体策略类)CoderAnnualLeave
类:
/**
* 一线开发程序员年假
* @author zlt
*/
public class CoderAnnualLeave implements AnnualLeave {
@Override
public Integer countAnnualLeave(Integer yearsOfJob) {
int annualLeave = 0;
if (yearsOfJob >= 1) {
annualLeave = 5;
}
annualLeave += (yearsOfJob - 1) * 2;
return annualLeave;
}
}
技术经理年假天数(具体策略类)ManagerAnnualLeave
类:
/**
* 总经理年假
*
* @author zlt
*/
public class ManagerAnnualLeave implements AnnualLeave {
@Override
public Integer countAnnualLeave(Integer yearsOfJob) {
int annualLeave = 0;
if (yearsOfJob > 1) {
annualLeave = 5;
}
annualLeave += (yearsOfJob - 1) * 3;
return annualLeave;
}
}
技术总监年假天数(具体策略类)DirectorAnnualLeave
类:
/**
* 总监年假
* @author zlt
*/
public class DirectorAnnualLeave implements AnnualLeave {
@Override
public Integer countAnnualLeave(Integer yearsOfJob) {
int annualLeave = 0;
if (yearsOfJob > 1) {
annualLeave = 5;
}
annualLeave += (yearsOfJob - 1) * 4;
return annualLeave;
}
}
hr核算年假天数(环境类)HumanResource
类:
/**
* 人事核算年假天数
* @author zlt
*/
public class HumanResource {
/**
* 年假计算
*/
private AnnualLeave annualLeave;
public HumanResource(AnnualLeave annualLeave) {
this.annualLeave = annualLeave;
}
/**
* 人事核算年假天数
*
* @param yearsOfJob 工作年限
* @return 年假天数
*/
public Integer countAnnualLeave4HR(Integer yearsOfJob) {
Integer i = annualLeave.countAnnualLeave(yearsOfJob);
System.out.println("人事核算年假天数:" + i + "天");
return i;
}
}
客户端使用代码:
public static void main(String[] args) {
// 计算3年工作年限一线开发的年假
HumanResource humanResource = new HumanResource(new CoderAnnualLeave());
humanResource.countAnnualLeave4HR(3);
System.out.println("==========================================");
// 计算3年工作年限技术经理的年假天数
HumanResource humanResource1 = new HumanResource(new ManagerAnnualLeave());
humanResource1.countAnnualLeave4HR(3);
System.out.println("==========================================");
// 计算3年工作年限技术总监的年假天数
HumanResource humanResource2 = new HumanResource(new DirectorAnnualLeave());
humanResource2.countAnnualLeave4HR(3);
}
代码结果:
总结
优缺点:
优点:
- 策略类之间可以自由切换,由于策略类都实现同一个接口,所以使它们之间可以自由切换。
- 易于扩展,增加一个新的策略只需要添加一个具体的策略类即可,基本不需要改变原有的代码,符合“开闭原则”。
- 避免使用多重条件选择语句(if-else),充分体面面对对象设计思想。
缺点:
- 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
- 策略模式将造成产生很多的策略类,可以通过使用享元模式在一定程度上减少对象的数量。
使用场景
- 一个系统需要动态的在几种算法中选择一种时,可将每个算法封装到策略类中。
- 一个类定义了多种行为,并且这些行为在这个类的操作中以多个条件语句的形式出现,可将每个条件分支移入它们各自的策略类中以代替这些条件语句。
- 系统中各算法彼此完全独立,且要求对客户隐藏具体算法的实现细节时。
- 系统要求使用算法的客户不应该知道其操作的数据时,可使用策略模式来隐藏算法相关的数据结构。
- 多个类只区别在表现行为不同,可以使用策略模式,在运行时动态选择具体要执行的行为。