美文网首页
装饰器模式(Decorator Pattern):电商平台商品价

装饰器模式(Decorator Pattern):电商平台商品价

作者: 肖哥弹架构 | 来源:发表于2024-07-17 16:47 被阅读0次
image.png

在电商平台中,商品价格可能会受到各种因素的影响,如会员折扣、优惠券、限时促销等。这些价格策略需要动态地应用到商品上,同时保持价格计算的灵活性和可扩展性。

肖哥弹架构 跟大家“弹弹” 设计模式,需要代码关注

欢迎 点赞,点赞,点赞。

关注公号Solomon肖哥弹架构获取更多精彩内容

2. 为什么要使用装饰器设计模式

装饰器模式允许我们动态地给商品添加多种价格策略,而不需要修改商品本身的代码。这样可以在运行时根据需要透明地添加或修改价格策略。

3. 标准装饰器设计模式图

image.png

4. 业务装饰器设计模式图

image.png

5. 业务代码参考


    // 商品价格组件接口
    interface ProductPrice {
        double apply();
    }

    // 简单商品价格实现
    class SimpleProductPrice implements ProductPrice {
        private double basePrice;

        public SimpleProductPrice(double basePrice) {
            this.basePrice = basePrice;
        }

        public double getBasePrice() {
            return basePrice;
        }

        @Override
        public double apply() {
            // 应用基础价格,实际项目中可能包含更多逻辑
            return basePrice;
        }
    }

    // 装饰器抽象类
    abstract class PriceDecorator implements ProductPrice {
        protected ProductPrice decoratedPrice;

        public PriceDecorator(ProductPrice decoratedPrice) {
            this.decoratedPrice = decoratedPrice;
        }

        @Override
        public double apply() {
            // 调用被装饰的组件的价格应用方法
            return decoratedPrice.apply();
        }
    }

    // 会员折扣装饰器
    class DiscountDecorator extends PriceDecorator {
        private double discount;

        public DiscountDecorator(ProductPrice productPrice, double discount) {
            super(productPrice);
            this.discount = discount;
        }

        @Override
        public double apply() {
            // 应用折扣
            return super.apply() * (1 - discount);
        }
    }

    // 优惠券装饰器
    class CouponDecorator extends PriceDecorator {
        private double couponValue;

        public CouponDecorator(ProductPrice productPrice, double couponValue) {
            super(productPrice);
            this.couponValue = couponValue;
        }

        @Override
        public double apply() {
            // 应用优惠券
            return super.apply() - couponValue;
        }
    }

    // 促销活动装饰器
    class PromotionDecorator extends PriceDecorator {
        private double promotionDiscount;

        public PromotionDecorator(ProductPrice productPrice, double promotionDiscount) {
            super(productPrice);
            this.promotionDiscount = promotionDiscount;
        }

        @Override
        public double apply() {
            // 应用促销折扣
            return super.apply() * (1 - promotionDiscount);
        }
    }

    // 客户端使用装饰器模式计算商品价格
    class ECommercePriceCalculator {
        public static void main(String[] args) {
            ProductPrice basePrice = new SimpleProductPrice(100.0);
            System.out.println("Original Price: " + basePrice.apply());

            ProductPrice withDiscount = new DiscountDecorator(basePrice, 0.1); // 10% 会员折扣
            System.out.println("Price with Discount: " + withDiscount.apply());

            ProductPrice withCoupon = new CouponDecorator(withDiscount, 5.0); // 减5元优惠券
            System.out.println("Price with Coupon: " + withCoupon.apply());

            ProductPrice withPromotion = new PromotionDecorator(withCoupon, 0.05); // 5% 促销活动
            System.out.println("Price with Promotion: " + withPromotion.apply());
        }
    }

6. 使用装饰器设计模式的好处

装饰器模式解决了以下问题并带来了以下好处:

  • 动态添加职责:可以在运行时根据需要动态地添加额外的价格策略。
  • 保持对象的透明性:装饰后的对象与原始对象对客户端来说看起来是一致的。
  • 扩展性:新增价格策略时无需修改现有代码,符合开闭原则。

7. 其他使用单例场景参考

装饰器模式适用于需要动态扩展对象行为的场景,例如:

  • 权限校验:动态为用户添加不同的权限校验。
  • 日志记录:为方法调用添加日志记录功能。

8. 可以参考的开源框架

  • Spring Framework:在Spring AOP中,装饰器模式被用来实现方法的前置、后置处理。

总结

装饰器模式是一种强大的设计模式,它允许我们在不修改对象本身的情况下,动态地扩展对象的功能。这使得代码更加灵活和可扩展。

历史热点文章

相关文章

网友评论

      本文标题:装饰器模式(Decorator Pattern):电商平台商品价

      本文链接:https://www.haomeiwen.com/subject/lvochjtx.html