定义
它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。
适用场景
- 系统有很多类,而他们的区别仅仅在于他们的行为不同
- 一个系统需要动态地在几种算法中选择一种
优点
- 开闭原则 (面向扩展开发,面向修改关闭)
- 避免使用多重条件转移语句(if...else)
- 提高算法的保密性和安全性(客户端只知道策略的作用,不用知道策略的实现)
缺点
- 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
- 产生很多的策略实现类。
代码演示
促销活动:
1)满减优惠
2)返现优惠
3)立减优惠
3)无优惠
/**
* 类名:PromotionStrategy.java
* 类描述:策略模式抽象策略类
* -msi-
* 2019年12月7日下午5:08:55
*/
public interface PromotionStrategy {
public void doPromotion();
}
/**
*
* 类名:FanxianPromotionStategy.java
* 类描述:具体实现策略类:返现优惠
* @author -msi-
* 2019年12月7日下午6:13:41
*/
public class FanxianPromotionStategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("返现促销,返回的金额存放到余额中");
}
}
/**
* 类名:LijianPromotionStrategy.java
* 类描述:具体策略类:立减
* -msi-
* 2019年12月7日下午6:14:18
*/
public class LijianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("立减促销,直接减去配置的价格");
}
}
/**
*
* 类名:ManjianPromotionStrategy.java
* 类描述:具体策略类:满减
* @author -msi-
* 2019年12月7日下午6:24:01
*/
public class ManjianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("满200-20");
}
}
/**
* 类名:EmptyPromotionStrategy.java
* 类描述:具体策略类:无优惠
* @author -msi-
* 2019年12月7日下午6:13:09
*/
public class EmptyPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("无促销活动");
}
}
/**
*
* 类名:PromotionActivity.java
* 类描述:上下文管理类
* @author -msi-
* 2019年12月7日下午6:24:48
*/
public class PromotionActivity {
private PromotionStrategy promotionStrategy;
public PromotionActivity(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
}
public void executePromotionStrategy() {
promotionStrategy.doPromotion();
}
}
/**
*
* 类名:PromotionStrategyFactory.java
* 类描述:工厂类
* @author -msi-
* 2019年12月7日下午6:25:25
*/
public class PromotionStrategyFactory {
private static Map<String,PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String,PromotionStrategy>();
static {
PROMOTION_STRATEGY_MAP.put(PromotionKey.FAXIAN, new FanxianPromotionStategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN, new LijianPromotionStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJINA, new ManjianPromotionStrategy());
}
private PromotionStrategyFactory() {}
private static final EmptyPromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();
public static PromotionStrategy getPromotionStategy(String promotionKey) {
PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);
return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
}
private interface PromotionKey{
String LIJIAN = "LIJIAN";
String MANJINA = "MANJIAN";
String FAXIAN = "FAXIAN";
}
}
/**
*
* 类名:Test.java
* 类描述:客户端测试类
* @author -msi-
* 2019年12月7日下午6:26:25
*/
public class Test {
public static void main(String[] args) {
String promotionKey = "LIJIAN";
PromotionActivity promotionActivity = new PromotionActivity
(PromotionStrategyFactory.getPromotionStategy(promotionKey));
promotionActivity.executePromotionStrategy();
}
}