• 二十三种设计模式:解密职责链模式-购物优惠活动的设计艺术



    在购物领域,为了吸引和激励消费者,商家常常会推出各种优惠活动,比如满减、打折、赠品等。然而,这些优惠活动的处理逻辑通常较为复杂,需要根据购物订单的条件进行判断和处理。本文将深入探讨职责链模式的实现方式,帮助你设计和实现购物优惠活动的灵活而可扩展的系统。


    1、创造优惠的链条
    职责链模式是一种行为设计模式,它通过将请求发送者和接收者解耦,将请求沿着一个处理者链条进行传递和处理。在购物优惠活动中,我们可以将不同类型的优惠券视为处理者对象,每个处理者对象负责处理特定类型的优惠逻辑。请求将依次经过处理者对象,根据购物订单的条件进行优惠处理,直到找到能够处理请求的处理者对象或者达到职责链的末尾。


    2、详细案例代码

    假设我们正在设计一个购物优惠活动系统,系统中有三种优惠券类型:满减、打折和赠品。我们可以使用职责链模式来处理这些优惠券的逻辑。


    首先,我们定义一个抽象处理者类和具体处理者类:

    // 抽象处理者
    abstract class CouponHandler {
        protected CouponHandler nextHandler;
    
        public void setNextHandler(CouponHandler nextHandler) {
            this.nextHandler = nextHandler;
        }
    
        public abstract void handleCoupon(Order order);
    }
    
    // 具体处理者
    class DiscountCouponHandler extends CouponHandler {
        public void handleCoupon(Order order) {
            if (order.getTotalAmount() >= 100) {
                double discount = order.getTotalAmount() * 0.1;
                order.setTotalAmount(order.getTotalAmount() - discount);
                System.out.println("Discount coupon applied. Total amount after discount: " + order.getTotalAmount());
            } else if (nextHandler != null) {
                nextHandler.handleCoupon(order);
            }
        }
    }
    
    class FreeGiftCouponHandler extends CouponHandler {
        public void handleCoupon(Order order) {
            if (order.getTotalAmount() >= 200) {
                order.addGift("Free T-shirt");
                System.out.println("Free gift coupon applied. Gift added: Free T-shirt");
            } else if (nextHandler != null) {
                nextHandler.handleCoupon(order);
            }
        }
    }
    
    class FullReductionCouponHandler extends CouponHandler {
        public void handleCoupon(Order order) {
            if (order.getTotalAmount() >= 300) {
                double reduction = 50;
                order.setTotalAmount(order.getTotalAmount() - reduction);
                System.out.println("Full reduction coupon applied. Total amount after reduction: " + order.getTotalAmount());
            } else if (nextHandler != null) {
                nextHandler.handleCoupon(order);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    然后,我们定义一个订单类和职责链构建器类:

    // 订单类
    class Order {
        private double totalAmount;
        private List gifts;
    
        public Order(double totalAmount) {
            this.totalAmount = totalAmount;
               }
    
        public double getTotalAmount() {
            return totalAmount;
        }
    
        public void setTotalAmount(double totalAmount) {
            this.totalAmount = totalAmount;
        }
    
        public void addGift(String gift) {
            if (gifts == null) {
                gifts = new ArrayList<>();
            }
            gifts.add(gift);
        }
    
        public void showGifts() {
            if (gifts != null && !gifts.isEmpty()) {
                System.out.println("Gifts:");
                for (String gift : gifts) {
                    System.out.println("- " + gift);
                }
            }
        }
    }
    
    // 职责链构建器
    class CouponChainBuilder {
        public CouponHandler build() {
            CouponHandler discountHandler = new DiscountCouponHandler();
            CouponHandler freeGiftHandler = new FreeGiftCouponHandler();
            CouponHandler fullReductionHandler = new FullReductionCouponHandler();
    
            discountHandler.setNextHandler(freeGiftHandler);
            freeGiftHandler.setNextHandler(fullReductionHandler);
    
            return discountHandler;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    最后,我们在客户端中使用职责链模式处理购物优惠券:

    public class Client {
        public static void main(String[] args) {
            Order order = new Order(250);
            CouponChainBuilder builder = new CouponChainBuilder();
            CouponHandler handler = builder.build();
    
            handler.handleCoupon(order);
    
            order.showGifts();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行上述代码,输出如下:

    Discount coupon applied. Total amount after discount: 225.0
    Free gift coupon applied. Gift added: Free T-shirt
    Gifts:
    - Free T-shirt
    
    • 1
    • 2
    • 3
    • 4

    这个例子中,订单的总金额为250元,首先满足打折优惠券的条件,总金额减少了10%,然后满足赠品优惠券的条件,赠品"Free T-shirt"被添加到订单中。


    本文详细介绍了职责链模式在购物优惠活动中的实现方式。通过职责链模式,我们可以将不同类型的优惠券逻辑解耦并灵活组合,实现购物优惠活动的多级处理。


    职责链模式的设计艺术不仅仅适用于购物优惠活动,还可以应用于许多其他场景,比如请求处理等。通过合理构建职责链,我们可以实现灵活、可扩展的系统。在下一篇博文中,我们将探索职责链模式在请求处理中的应用,敬请期待!


    好了,今天的分享到此结束。如果觉得我的博文帮到了您,您的点赞和关注是对我最大的支持。如遇到什么问题,可评论区留言。


  • 相关阅读:
    【word格式】mathtype公式插入 | 段落嵌入后格式对齐 | 字体大小调整 |空心字体
    python高级内置函数介绍及应用举例
    【校招VIP】测试方案之测试需求分析
    Docker 日志管理 - ELK
    Elasticsearch全文搜索技术之二kibana的简介和使用
    RabbitMQ中CorrelationData 与DeliveryTag的区别
    (论文阅读34-39)理解CNN
    vue3手写一个轮播图
    【汇编】栈及栈操作的实现
    ffmpeg 之ffmpeg 整理流程分析
  • 原文地址:https://blog.csdn.net/lizhong2008/article/details/134503100