定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化不会影响到使用算法的用户
类型:行为型
使用场景:
系统有很多类,而他们的区别仅仅在于他们的行为不同。
一个系统需要动态地在集中算法中选择一种
优点:
1.开闭原则
2.避免使用多重条件转义语句
3.提高算法的保密性和安全性
缺点:
客户端必须知道所有的策略类,并自行决定使用哪一个策略类
产生了很多策略类
接口:
public interface PromotionsStrategy { void doPromotion(); }
执行动作:
private PromotionsStrategy promotionsStrategy; public PromotionActivity(PromotionsStrategy promotionsStrategy) { this.promotionsStrategy = promotionsStrategy; } public void executePromotionsStrategy(){ promotionsStrategy.doPromotion(); } }
为空的情况:
public class EmptyPromotionStrategy implements PromotionsStrategy{ @Override public void doPromotion() { System.out.println("无处销活动"); } }
返现:
public class FanXianPromotionsStrategy implements PromotionsStrategy{ @Override public void doPromotion() { System.out.println("返现促销,返回的金额存放到慕课网用户的余额中"); } }
立减:
public class LiJianPromotionsStrategy implements PromotionsStrategy { @Override public void doPromotion() { System.out.println("立减促销,返回的金额存放到慕课网用户的余额中"); } }
满减:
public class ManJianPromotionsStrategy implements PromotionsStrategy{ @Override public void doPromotion() { System.out.println("满减促销,返回的金额存放到慕课网用户的余额中"); } }
工厂:
import java.util.HashMap; import java.util.Map; public class PromotionsStrategyFactory { private static Map<String, PromotionsStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionsStrategy>(); static { PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN, new LiJianPromotionsStrategy()); PROMOTION_STRATEGY_MAP.put(PromotionKey.FANXIAN, new FanXianPromotionsStrategy()); PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJIAN, new ManJianPromotionsStrategy()); } private static final PromotionsStrategy NON_PROMOTION = new EmptyPromotionStrategy(); private PromotionsStrategyFactory() { } public static PromotionsStrategy getPromotionStrategy(String promotionsKey) { PromotionsStrategy promotionsStrategy = PROMOTION_STRATEGY_MAP.get(promotionsKey); return promotionsStrategy == null ? NON_PROMOTION : promotionsStrategy; } private interface PromotionKey { String LIJIAN = "LIJIAN"; String FANXIAN = "FANXIAN"; String MANJIAN = "MANJIAN"; } }
测试:
public class main { public static void main(String[] args) { // PromotionActivity promotionActivity618=new PromotionActivity(new LiJianPromotionsStrategy()); // PromotionActivity promotionActivity1111=new PromotionActivity(new FanXianPromotionsStrategy()); // // promotionActivity618.executePromotionsStrategy(); // promotionActivity1111.executePromotionsStrategy(); // PromotionActivity promotionActivity=null; // String promotionKey="LIJIAN"; // if(StringUtils.equals(promotionKey,"LIJIAN")){ // promotionActivity=new PromotionActivity(new LiJianPromotionsStrategy()); // }else if(StringUtils.equals(promotionKey,"FANXIAN")){ // promotionActivity=new PromotionActivity(new FanXianPromotionsStrategy()); // } // promotionActivity.executePromotionsStrategy(); String promotionKey="LIJIAN"; PromotionActivity promotionActivity=new PromotionActivity(PromotionsStrategyFactory.getPromotionStrategy(promotionKey)); promotionActivity.executePromotionsStrategy(); } }