策略模式
定义:定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化不会影响到使用算法的用户。
生活举例,常见的促销活动,用户可以选择不同的促销
代码中,如果有大量的 if...else... 的语句,可以通过 策略模式 消除掉。
类型:行为型
适用场景:(1)系统又很多类,而他们的区别仅仅在于他们的行为不同,(2)一个系统需要动态地在几种算法中选择一种,
优点:(1)符合开闭原则,(2)避免使用多重条件转移语句,(3)提高算法的保密性和安全性
缺点:(1)客户端必须知道所有的策略类,并自行决定使用哪一个策略,(2)会产生很多策略类
不同点 | ||
策略模式 | (1)属于行为型, (2)策略模式接受已经创建好的对象,从而实现不同的行为 | |
工厂模式 (工厂方法,抽象工厂) | (1)属于创建型, (2)工厂模式接受指令,创建符合要求的具体对象 |
不同点 | ||
策略模式 | (1)在使用策略模式的时候,客户端(test)需要知道选择使用哪个策略 (2)如果系统中,某个类的行为存在多种实现方式(比如618促销和双11促销,对于促销(行为)有多种实现方式)这种情况下,使用策略模式。 | |
状态模式 | (1)在使用状态模式的时候,客户端是不需要关心具体的状态的,这些状态会自动转换, (2)如果系统中,某个类的对象存在多个状态,在不同状态下,行为又有所差异的话,而且这些状态可以发生转化,我们可以使用状态模式, |
代码
// 每个 class 都是 .java 文件
public interface PromotionStrategy {
void doPromotion();
}
public class EmptyPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("无促销活动");
}
}
public class FanXianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("返现促销,返回的金额存放到慕课网用户的余额中");
}
}
public class LiJianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("立减促销,课程的价格直接减去配置的价格");
}
}
public class ManJianPromotionStrategy implements PromotionStrategy{
@Override
public void doPromotion() {
System.out.println("满减促销,满200-20元");
}
}
// 促销活动
public class PromotionActivity {
private PromotionStrategy promotionStrategy;
public PromotionActivity(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
}
public void executePromotionStrategy(){
promotionStrategy.doPromotion();
}
}
// 工厂模式
public class PromotionStrategyFactory {
private static Map<String,PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionStrategy>();
static {
PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN,new LiJianPromotionStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.FANXIAN,new FanXianPromotionStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJIAN,new ManJianPromotionStrategy());
}
private static final PromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();
private PromotionStrategyFactory(){ }
public static PromotionStrategy getPromotionStrategy(String promotionKey){
PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);
return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
}
private interface PromotionKey{
String LIJIAN = "LIJIAN";
String FANXIAN = "FANXIAN";
String MANJIAN = "MANJIAN";
}
}
// 测试代码
public class Test {
// 策略模式
public static void main(String[] args) {
PromotionActivity promotionActivity618 = new PromotionActivity(new LiJianPromotionStrategy());
PromotionActivity promotionActivity1111 = new PromotionActivity(new FanXianPromotionStrategy());
promotionActivity618.executePromotionStrategy();
promotionActivity1111.executePromotionStrategy();
}
// 策略模式 + 工厂模式
// public static void main(String[] args) {
// String promotionKey = "MANJIANxxx";
// PromotionActivity promotionActivity = new PromotionActivity(PromotionStrategyFactory.getPromotionStrategy(promotionKey));
// promotionActivity.executePromotionStrategy();
// }
}
UML类图
源码示例
在 java.util 包中的 Comparator 接口,或者是 TreeMap 类,
Spring 中的 Resource 接口,InstantiationStrategy 接口,
参考:Java设计模式透析之 —— 策略(Strategy),举的例子是,用不同的实现去实现相同的SQL语句的查询。