策略模式和责任链模式(1)

简介: 策略模式和责任链模式(1)

一、策略模式

策略模式( Strategy Pattern )又叫也叫政策模式( Policy Pattern) ,它是将定义的算法家族、分别封装起来,让它们之间可以互相替换,从而让算法的变化不会影响到使用算法的用户。属于行为型模式。


原文: Define a family of algor ithms, encapsulate each one, and make them inter changeable.


**策略模式在生活场景中应用也非常多。**比如一个人的交税比率与他的工资有关,不同的工资水平对应不同的税率。再比如我们在互联网移动支付的大背景下,每次下单后付款前,需要选择支付方式。


image.png


策略模式可以解决在有多种算法相似的情况下,使用if…else 或swith…case所带来的复杂性和臃

肿性。在日常业务开发中,策略模式适用于以下场景:

1、针对同-类型问题,有多种处理方式,每- -种都能独立解决问题;

2、算法需要自由切换的场景;

3、需要屏蔽算法规则的场景。

首先来看下策略模式的通用UML类图:


image.png


从类图中我们可以看到, 委派模式有三个参与角色:


抽象任务角色( Task ) : 定义一个抽象接口, 它有若干实现类。


委派者角色( Delegate ) : 负责在各个具体角色实例之间做出决策并判断并调用具体实现的方法。


具体任务角色( Concrete ) 真正执行任务的角色。


注意: 策略模式中的上下文环境 (Context) , 其职责本来是隔离客户端与策略类的耦合, 让客户端完全与上下文环境 沟通, 无需关系具体策略。


二、策略模式实例


优惠策略会有很多种可能如 : 领取优惠券抵扣、返现促销、拼团优惠。下面我们用代码来模拟,


首先我们创建—个促销策略的抽象


PromotionStrategy :


/**
 * 促销策略抽象 
 */
public interface IPromotionStrategy {
    void doPromotion();
}


然后分别创建优惠券抵扣策略 CouponStrategy 类、返现促销策略 CashbackStrategy 类、拼团优惠策略 GroupbuyStrategy 类和无优惠策略 EmptyStrategy 类 :


public class CouponStrategy implements IPromotionStrategy {
    public void doPromotion() {
        System.out.println("使用优惠券抵扣");
    }
}


CashbackStrategy 类 :


public class CashbackStrategy implements IPromotionStrategy {
    public void doPromotion() {
        System.out.println("返现,直接打款到支付宝账号");
    }
}


GroupbuyStrategy 类 :


public class GroupbuyStrategy implements IPromotionStrategy {
    public void doPromotion() {
        System.out.println("5人成团,可以优惠");
    }
}


EmptyStrategy 类 :


public class EmptyStrategy implements IPromotionStrategy {
    public void doPromotion() {
        System.out.println("无优惠");
    }
}


然后创建促销活动方案 PromotionActivity 类 :


public class PromotionActivity {
    private IPromotionStrategy strategy;
    public PromotionActivity(IPromotionStrategy strategy) {
        this.strategy = strategy;
    }
    public void execute(){
        strategy.doPromotion();
    }
}


编写客户端测试类 :


public class Test {
    public static void main(String[] args) {
        PromotionActivity activity618 = new PromotionActivity(new CouponStrategy());
        PromotionActivity activityllll = new PromotionActivity(new CashbackStrategy());
        activity618.execute();
        activityllll.execute();
    }
}


运行效果如下:


使用优惠券抵扣
返现,直接打款到支付宝账号


此时,小伙伴们会发现,如果把上面这段则试代码放到实际的业务场景其实并不实用。因为我们做活动时候往往是要根据不同的需求对促销策略进行动态选择的,并不会一次性执行多种优惠。所以,我

们的代码通常会这样写 :


public class Test {
    public static void main(String[] args) {
        PromotionActivity promotionActivity = null;
        String promotionKey = "COUPON";
        if (StringUtils.equals(promotionKey, "COUPON")) {
            promotionActivity = new PromotionActivity(new CouponStrategy());
        } else if (StringUtils.equals(promotionKey, "CASHBACK")) {
            promotionActivity = new PromotionActivity(new CashbackStrategy());
        }
        promotionActivity.execute();
    }
}


这样改造之后,满足了业务需求,客户可根据自己的需求选择不同的优惠策略了。但是,经过—段

时间的业务积累,我们的促销活动会越来越多。千是,我们的程序猿小哥哥就忙不来了,每次上活动之前都要通宵改代码,而且要做重复测试,判断逻辑可能也变得越来越复杂。这时候,我们是不需要思考

代码是不是应该重构了?回顾我们之前学过的设计模式应该如何来优化这段代码呢?其实,我们可以结

合单例模式和工厂摆式。创建 PromotionStrategyFactory 类 :


public class PromotionStrategyFacory {
    private static final IPromotionStrategy EMPTY = new EmptyStrategy();
    private static Map<String, IPromotionStrategy> PROMOTIONS = new HashMap<String, IPromotionStrategy>();
    static {
        PROMOTIONS.put(PromotionKey.COUPON, new CouponStrategy());
        PROMOTIONS.put(PromotionKey.CASHBACK, new CashbackStrategy());
        PROMOTIONS.put(PromotionKey.GROUPBUY, new GroupbuyStrategy());
    }
    private PromotionStrategyFacory() {
    }
    public static IPromotionStrategy getPromotionStrategy(String promotionKey) {
        IPromotionStrategy strategy = PROMOTIONS.get(promotionKey);
        return strategy == null ? EMPTY : strategy;
    }
    public static Set<String> getPromotionKeys() {
        return PROMOTIONS.keySet();
    }
    private interface PromotionKey {
        String COUPON = "COUPON";
        String CASHBACK = "CASHBACK";
        String GROUPBUY = "GROUPBUY";
    }
}
目录
相关文章
|
7天前
|
设计模式 算法 搜索推荐
策略模式的小记
本文介绍了策略模式的概念、结构和使用场景,并通过支付系统的例子展示了如何使用策略模式来动态选择不同的支付策略,包括定义支付策略接口、实现具体的支付策略类、创建上下文类以及在客户端动态选择支付策略。
策略模式的小记
|
2月前
|
设计模式
|
4月前
|
设计模式 JavaScript 前端开发
如何优雅的使用责任链模式?
【6月更文挑战第7天】如何优雅的使用责任链模式?
|
5月前
|
算法 数据安全/隐私保护
行为型 策略模式
行为型 策略模式
28 1
|
设计模式 算法 Java
什么场景要使用策略模式,什么场景不能使用?
如果,让我来设计,我最先想到的就是策略模式。另外,我把往期面试题解析的配套文档我已经准备好,想获得的可以在我的煮叶简介中找到。那么什么场景要使用策略模式,什么场景又不应该使用策略模式呢?我们可以先来看官方对策略模式的定义。
152 0
|
JavaScript 测试技术
关于责任链模式我所知道的
关于责任链模式我所知道的
78 0
责任链模式简单介绍
责任链模式简单介绍
128 0
|
Java Nacos Sentinel
责任链模式详解(上)
责任链模式详解
225 0
责任链模式详解(上)
|
设计模式 XML 数据格式