1 概述
策略模式定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户。
2 实现模板
//抽象算法类
abstract class Strategy {
//算法方法
public abstract void algorithmInterface();
}
//具体算法A
class ConcreteStrategyA extends Strategy {
@Override
public void algorithmInterface() {
System.out.println("算法A实现");
}
}
//具体算法B
class ConcreteStrategyB extends Strategy {
@Override
public void algorithmInterface() {
System.out.println("算法A实现");
}
}
//具体算法C
class ConcreteStrategyC extends Strategy {
@Override
public void algorithmInterface() {
System.out.println("算法A实现");
}
}
//上下文
class Context {
Strategy strategy;
//初始化时,传入具体的策略对象
public Context(Strategy strategy) {
this.strategy = strategy;
}
//上下文接口
public void contextInterface() {
//根据具体的策略对象,调用其算法的方法
strategy.algorithmInterface();
}
}
3 策略模式具体实现
考虑商场收银策略,有正常收银,有打折促销,有满减返利。由于打折促销和满减返利对应的都是不同的算法,所以考虑使用策略模式。
public abstract class CashSuper {
public abstract double acceptCash(double price, int num);
}
public class CashNormal extends CashSuper {
@Override
public double acceptCash(double price, int num) {
return price * num;
}
}
public class CashRebate extends CashSuper {
private double moneyRebate = 1d;
public CashRebate(double moneyRebate) {
this.moneyRebate = moneyRebate;
}
@Override
public double acceptCash(double price, int num) {
return price * num * moneyRebate;
}
}
public class CashReturn extends CashSuper {
private double moneyCondition = 0d; //返利条件数额
private double moneyReturn = 0d; //返利值
public CashReturn(double moneyCondition, double moneyReturn) {
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double price, int num) {
double result = price * num;
if (moneyCondition > 0 && result >= moneyCondition) {
result = result - Math.floor(result / moneyCondition) * moneyReturn;
}
return result;
}
}
策略抽象类及三种具体策略。对应抽象方法,及三种具体的收钱方法。
public class CashContext {
private CashSuper cs;
public CashContext(CashSuper cs) {
this.cs = cs;
}
public double getResult(double price, int num) {
return cs.acceptCash(price, num);
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int discount = sc.nextInt();
CashContext context = null;
switch (discount) {
case 1:
context = new CashContext(new CashNormal());
break;
case 2:
context = new CashContext(new CashRebate(0.8));
break;
case 3:
context = new CashContext(new CashReturn(300, 100));
break;
default:
break;
}
double totalPrice = context.getResult(230, 2);
}
}
上下文类,及客户端实现。客户端根据选择的收钱方式,创建对应的策略类对象,并都依托给上下文进行管理。这里客户端的代码还是不够精简,可以考虑使用简单工厂模式进行简化。
4 简单工厂模式结合策略模式
修改Context类和客户端代码
public class CashContext {
private CashSuper cs;
public CashContext(int cashType) {
switch (cashType) {
case 1:
cs = new CashNormal();
break;
case 2:
cs = new CashRebate(0.8);
break;
case 3:
cs = new CashReturn(300, 100);
break;
default:
break;
}
}
public double getResult(double price, int num) {
return cs.acceptCash(price, num);
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int discount = sc.nextInt();
CashContext context = new CashContext(discount);
double totalPrice = context.getResult(230, 2);
}
}
此时客户端只和CashContext一个类打交道,减少了耦合
5 总结
- 策略模式用于封装不同算法,这些算法做相同的工作,只是实现不同
- 策略模式通过Strategy抽象类为Context类提供了一系列可供重用的算法或行为,通过继承可以析取出算法中的公共方法。
- 策略模式简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。
- 策略模式封装了变化,就是用来封装算法的。