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

news/2024/5/19 13:45:02 标签: 设计模式, 职责链模式, 责任链模式, java

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


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);
        }
    }
}

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

// 订单类
class Order {
    private double totalAmount;
    private List<String> 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;
    }
}

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

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();
    }
}

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

Discount coupon applied. Total amount after discount: 225.0
Free gift coupon applied. Gift added: Free T-shirt
Gifts:
- Free T-shirt

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


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


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


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



http://www.niftyadmin.cn/n/5196446.html

相关文章

leetcode:914. 卡牌分组(python3解法)

难度&#xff1a;简单 给定一副牌&#xff0c;每张牌上都写着一个整数。 此时&#xff0c;你需要选定一个数字 X&#xff0c;使我们可以将整副牌按下述规则分成 1 组或更多组&#xff1a; 每组都有 X 张牌。组内所有的牌上都写着相同的整数。 仅当你可选的 X > 2 时返回 tru…

关于ASO优化的分步入门指南2

1、分析元数据。 分析我们收集的当前元数据和关键词&#xff0c;单独跟踪关键字词&#xff0c;然后跟踪组合。例如如果应用程序的标题是关于音乐的应用&#xff0c;则需要跟踪“音乐”、“听”、“听音乐”等关键词。填充元数据分析选项卡&#xff0c;使用搜索分数、下载影响和…

MES管理系统与ERP系统的实施顺序与决策

在现今的数字化时代&#xff0c;制造企业纷纷寻求通过先进的系统来提升运营效率。其中&#xff0c;ERP管理系统与MES管理系统被誉为是数字化转型的两大利器。然而&#xff0c;在推进这两个系统时&#xff0c;企业常常面临一个关键问题&#xff1a;究竟应该先实施哪一个系统&…

ASUS华硕ROG幻13笔记本电脑GV301QE原厂Windows10系统

链接&#xff1a;https://pan.baidu.com/s/1aPW0ctRXRNAhE75mzVPdTg?pwdds78 提取码&#xff1a;ds78 华硕玩家国度幻13笔记本电脑锐龙版Ryzen 7 5800HS,显卡3050 3050Ti,3060,3060Ti,3070,3070Ti 原厂W10系统自带所有驱动、出厂主题壁纸、系统属性专属LOGO标志、Office办…

8.6 矢量图层点要素基于规则(Rule-based)渲染使用

文章目录 前言基于规则&#xff08;Rule-based&#xff09;QGis代码实现 总结 前言 前面介绍了矢量-点要素-单一符号、矢量-点要素-分类符号以及矢量-点要素-分级符号的使用本章介绍如何使用基于规则的渲染说明&#xff1a;文章中的示例代码均来自开源项目qgis_cpp_api_apps …

新一代车载以太网传输技术研讨会(AEM)顺利圆满举行

时间定格在2023年11月17日&#xff0c;新一代车载以太网传输技术研讨会在东莞国际会展中心举行。来自相关的的企业几百家。当然&#xff0c;深圳维信仪器作为主办方&#xff08;AEM线束测试仪中国区总平台&#xff09;举优质的线束测试设备&#xff0c;不论是手持线束测试&…

【代码随想录刷题】栈与队列总结

文章目录 1.栈与队列的理论基础2.栈的经典题目2.1 栈在系统中的应用2.2 括号匹配问题2.3 字符串去重问题2.4 逆波兰表达式问题 3. 队列的经典题目3.1 滑动窗口最大值问题3.2 求K个高频元素 1.栈与队列的理论基础 java中的栈Stack java中的队列Queue 了解了栈与队列基础之后&a…

VivadoAndTcl: namespace

命名空间&#xff0c;其实际是一系列变量和过程的合集&#xff0c;从而让TCL 解释器能够对这些变量和过程进行分类管理。 # 声明如下 namespace eval ns0 {proc print {} {puts "Tcl Proc 0" }proc add {a b} {return [expr {$a $b}] } }namespace eval ns1 …