1,目标
通过学习策略模式来消除代码中的大量的if...else...和switch语句
深刻理解策略模式的应用场景,提高算法的保密性和安全性。
2,定位
希望通过对策略模式的学习,来消除程序中的大量的冗余代码和多重转移语句
3,策略模式的定义
策略模式(Strategy pattern)是指定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化不会影响到使用算法的用户。可以避免多重分支的if...else...和switch语句。
4,策略模式的适用场景
1,假如系统中有很多类,而他们的区别仅仅在于他们的行为不同。
2,一个系统需要动态地在几种算法中选择一种。
5,策略模式代码实现方法
我们按照培训机构卖课做为例子,首先创建一个培训课程的促销接口PromotionStrategy.java类
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public interface PromotionStrategy {
void doPromotion();
}
再分别创建各种促销实现类,CashbackStrategy.java促销返现类
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class CashbackStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("返现促销,返现后的价格转到支付宝");
}
}
培训机构发放优惠卷类CouponStrategy.java
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class CouponStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("领优惠券,课程后的价格");
}
}
正常的无促销的类EmptyStrategy.java
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class EmptyStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("无促销活动");
}
}
团购的类GroupbuyStrategy.java
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class GroupbuyStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("组团,满20人成团,全团享受团购价");
}
}
再创建一个活动发布类PromotionAvtivity.java,通过发布的活动中的构造方法得到不同的优惠活动,通过execute方法执行具体的活动优惠力度。
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class PromotionAvtivity {
PromotionStrategy promotionStrategy;
public PromotionAvtivity(PromotionStrategy promotionStrategy){
this.promotionStrategy = promotionStrategy;
}
public void execute(){
this.promotionStrategy.doPromotion();
}
}
最后再做一个测试类PromotionAvtivityTest.java
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class PromotionAvtivityTest {
public static void main(String[] args) {
PromotionAvtivity avtivity618 = new PromotionAvtivity(new CouponStrategy());
PromotionAvtivity avtivityGroup = new PromotionAvtivity(new GroupbuyStrategy());
avtivity618.execute();
avtivityGroup.execute();
}
}
我们看一下测试的结果。
领优惠券,课程后的价格
组团,满20人成团,全团享受团购价
Process finished with exit code 0
按照正常我们代码中使用写的话,应该使用下面的方式进行调用。?
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class PromotionAvtivityTest {
public static void main(String[] args) {
PromotionAvtivity promotionAvtivity = null;
String promotionKey = "COUPON";
if(promotionKey.equals("COUPON")){
promotionAvtivity = new PromotionAvtivity(new CouponStrategy());
}else if(promotionKey.equals("CASHBACK")){
promotionAvtivity = new PromotionAvtivity(new CashbackStrategy());
}//.....
promotionAvtivity.execute();
}
}
?测试结果如下:
组团,满20人成团,全团享受团购价
Process finished with exit code 0
上面的代码还是有冗余,我们结合之前的工厂模式进行改造一下。
新建一个PromotionStrategyFactory.java类
package com.packer.partten.strategy.promotion;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lijianfang on 2021/10/3.
*/
public class PromotionStrategyFactory {
private static Map<String,PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionStrategy>();
static {
PROMOTION_STRATEGY_MAP.put(PromotionKey.CASHBACK,new CashbackStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.COUPON,new CouponStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.GROUPBUY,new GroupbuyStrategy());
}
private static final PromotionStrategy NON_PROMOTION = new EmptyStrategy();
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 COUPON = "COUPON";
String CASHBACK = "CASHBACK";
String GROUPBUY = "GROUPBUY";
String EMPTY = "EMPTY";
}
}
测试方法如下:
package com.packer.partten.strategy.promotion;
/**
* Created by lijianfang on 2021/10/3.
*/
public class PromotionAvtivityTest {
public static void main(String[] args) {
String promotionKey = "GROUPBUY";
PromotionAvtivity promotionAvtivity = new PromotionAvtivity(PromotionStrategyFactory.getPromotionStrategy(promotionKey));
promotionAvtivity.execute();
}
}
测试结果如下:
组团,满20人成团,全团享受团购价
Process finished with exit code 0
|