在领域驱动设计中,当工厂模式与其他模式(如策略模式)结合使用时,如何确保设计的灵活性和可扩展性?请提供一个场景和实现思路。

在领域驱动设计(DDD)中,工厂模式通常用于创建复杂对象,而策略模式则用于在运行时选择具体的算法实现。当这两个模式结合使用时,可以极大地提高系统的灵活性和可扩展性。下面通过一个具体的场景来说明如何实现这一点。

场景描述

假设我们正在开发一个电子商务系统,需要处理不同类型的促销活动,例如满减、折扣和赠品等。每种促销活动的计算逻辑不同,因此可以使用策略模式来实现这些不同的计算逻辑。同时,为了确保促销活动对象的正确创建,可以使用工厂模式来创建这些对象。

实现思路

  1. 定义策略接口 首先,定义一个策略接口 PromotionStrategy,该接口包含一个或多个算法方法,例如 applyPromotion,用于计算具体的促销结果。

    public interface PromotionStrategy {
        BigDecimal applyPromotion(BigDecimal originalPrice);
    }
    
  2. 实现具体策略 然后,为每种促销活动实现一个具体的策略类。例如,DiscountStrategy 用于处理折扣,CouponStrategy 用于处理优惠券,FreebieStrategy 用于处理赠品。

    public class DiscountStrategy implements PromotionStrategy {
        private final BigDecimal discountRate;
        
        public DiscountStrategy(BigDecimal discountRate) {
            this.discountRate = discountRate;
        }
        
        @Override
        public BigDecimal applyPromotion(BigDecimal originalPrice) {
            return originalPrice.multiply(discountRate);
        }
    }
    
    public class CouponStrategy implements PromotionStrategy {
        private final BigDecimal couponAmount;
        
        public CouponStrategy(BigDecimal couponAmount) {
            this.couponAmount = couponAmount;
        }
        
        @Override
        public BigDecimal applyPromotion(BigDecimal originalPrice) {
            return originalPrice.subtract(couponAmount);
        }
    }
    
    public class FreebieStrategy implements PromotionStrategy {
        @Override
        public BigDecimal applyPromotion(BigDecimal originalPrice) {
            // 赠品不改变价格
            return originalPrice;
        }
    }
    
  3. 定义促销活动工厂 接下来,定义一个工厂类 PromotionFactory,用于创建具体的促销活动对象。工厂方法可以根据传入的参数(例如促销类型)创建相应的策略对象。

    public class PromotionFactory {
        public static Promotion createPromotion(PromotionType type, BigDecimal parameter) {
            switch (type) {
                case DISCOUNT:
                    return new DiscountPromotion(new DiscountStrategy(parameter));
                case COUPON:
                    return new CouponPromotion(new CouponStrategy(parameter));
                case FREEBIE:
                    return new FreebiePromotion(new FreebieStrategy());
                default:
                    throw new IllegalArgumentException("Unknown promotion type");
            }
        }
    }
    
  4. 定义促销活动抽象类 定义一个抽象类 Promotion,包含策略对象和应用促销的方法。

    public abstract class Promotion {
        protected PromotionStrategy strategy;
        
        public Promotion(PromotionStrategy strategy) {
            this.strategy = strategy;
        }
        
        public BigDecimal applyPromotion(BigDecimal originalPrice) {
            return strategy.applyPromotion(originalPrice);
        }
    }
    
  5. 实现具体的促销活动类 实现具体的促销活动类,例如 DiscountPromotion、CouponPromotion 和 FreebiePromotion,这些类继承自 Promotion 抽象类。

    public class DiscountPromotion extends Promotion {
        public DiscountPromotion(DiscountStrategy strategy) {
            super(strategy);
        }
    }
    
    public class CouponPromotion extends Promotion {
        public CouponPromotion(CouponStrategy strategy) {
            super(strategy);
        }
    }
    
    public class FreebiePromotion extends Promotion {
        public FreebiePromotion(FreebieStrategy strategy) {
            super(strategy);
        }
    }
    

总结

通过结合工厂模式和策略模式,我们可以实现一个高度灵活和可扩展的系统。工厂模式确保了对象的正确创建,而策略模式则提供了多种算法实现的灵活性。当需要添加新的促销活动时,只需要实现新的策略类并扩展工厂类即可,而不需要修改现有代码。