策略模式
策略模式:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的用户。 策略模式的关键在于:将行为及其运作方式抽象为策略和策略实现。
一、案例分析
此处以商场支付为例,可以用微信支付也可以用支付宝支付。
此处,支付是一种行为,可以将其视为一种策略,即:支付策略
1、不采用策略模式的商场支付
public class Mall {
private Double price;
public Mall setPrice(Double price) {
this.price = price;
return this;
}
/**
* 通过支付宝支付
*/
public void payWithAlipay() {
System.out.println("通过支付宝支付了" + price + "元.");
}
/**
* 通过微信支付
*/
public void payWithWeChat() {
System.out.println("通过微信支付了" + price + "元.");
}
}
public class Demo {
public static void main(String[] args) {
double price = 100d;
PayStrategy wxPayStrategy = new WxPayStrategy();
new Mall()
.setPrice(price)
.payWithWeChat();
PayStrategy zhiFuBaoStrategy = new ZhiFuBaoStrategy();
new Mall()
.setPrice(price)
.payWithAlipay();
}
}
没有采用策略模式,每次新增支付方式都要去修改Mall
中的代码。
2、采用策略模式的商场支付
//支付策略
public interface PayStrategy {
//price 支付的金额
void pay(double price);
}
//支付策略一:微信支付
public class WxPayStrategy implements PayStrategy {
@Override
public void pay(double price) {
System.out.println("通过微信支付了"+price+"元");
}
}
//支付策略二:支付宝支付
public class ZhiFuBaoStrategy implements PayStrategy {
@Override
public void pay(double price) {
System.out.println("通过支付宝支付了"+ price +"元");
}
}
//商场
public class Mall{
private PayStrategy payStrategy;
private Double price;
public Mall setPayStrategy(PayStrategy payStrategy){
this.payStrategy = payStrategy;
return this;
}
public Mall setPrice(Double price) {
this.price = price;
return this;
}
public void pay(){
payStrategy.pay(price);
}
}
- 将策略(抽象类/接口)PayStrategy作为客户端Mall的成员变量,才能承载不同对象的实现。
- 策略实现的方法:setPayStrategy(PayStrategy payStrategy),为策略实现的切换提供入口。
public class Demo {
public static void main(String[] args) {
double price = 100d;
PayStrategy wxPayStrategy = new WxPayStrategy();
new PayContext()
.setPayStrategy(wxPayStrategy)
.setPrice(price)
.pay();
PayStrategy zhiFuBaoStrategy = new ZhiFuBaoStrategy();
new PayContext()
.setPayStrategy(zhiFuBaoStrategy)
.setPrice(price)
.pay();
}
}
运行结果: 通过微信支付了100.0元 通过支付宝支付了100.0元
新的需求:增加支持新的支付方式:云闪付
此时,只需要新增一种支付策略实现YunShanFuPayStrategy
,而不需修改原有的代码。
新的支付策略三:云闪付支付
public class YunShanFuPayStrategy implements PayStrategy{
@Override
public void pay(double price) {
System.out.println("通过云闪付支付了"+price+"元");
}
}
二、分析
1、优点
- 扩展性好,每增减一种支付方式,只需增减一种支付策略实现即可,无需修改Mall,遵从了设计模式的开放-封闭原则(OCP,对扩展开放,对修改关闭)。
- 可复用高,每种支付方式都有相同的父类接口,可以用多态的方式使父类指向子类对象,每次调用都是父类,传入的参数是子类。
2、缺点
- 实现复杂:如果有n种策略,则需要n个策略实现类,再加一个策略类。