public Basket(EDiscountType discountType) { _products = new List <Product>(); // Here the discount strategy is defined, according to a desired kind of discount type. _discountStrategy = DiscountStrategyFactory.DiscountStrategyFor(discountType); }
static void Main(string[] args) { IBasketDiscountStrategy basketDiscountStrategy = BasketDiscountFactory.Create(DiscountType.MoneyOff); Basket basket = new Basket(basketDiscountStrategy) { TotalCost = 100 }; decimal total = basket.GetTotalCostAfterDiscount(); Console.WriteLine("原价100,打折后{0}", total); Console.Read(); }
public void BasketDiscountFactory_Should_Return_BasketDiscountPercentageOff_When_Passed_DiscountType_Of_PercentageOff() { IBasketDiscountStrategy basketDiscountStrategy = BasketDiscountFactory.GetDiscount(DiscountType.PercentageOff); Assert.That(basketDiscountStrategy, Is.TypeOf(typeof(BasketDiscountPercentageOff))); }
public Basket(IBasketDiscountStrategy basketDiscountStrategy) { this._basketDiscountStrategy = basketDiscountStrategy; }
public Basket(DiscountType type) { this._strategy = BasketDiscountFactory.GetDiscount(type); }
/// <summary> /// 通过构造函数实现创建具体的打折策略对象 /// </summary> /// <param name="discountType"></param> public Basket(DiscountType discountType) { //根据枚举类型使用工厂方法创建指定的打折策略(都实现了IBasketDiscountStrategy接口) //实现了该接口的具体实现类中都实现了GetTotalCostAfterApplyingDiscountTo方法实现打折 _basketDiscount = BasketDiscountFactory.GetDiscount(discountType); }
public Basket(DiscountType discountType) { _basketDiscount = BasketDiscountFactory.GetDiscount(discountType); }
/// <summary> /// Defines a discount type to be applied over all products of basket. /// </summary> /// <param name="discountType">Discount type which will be applied over the products list.</param> public void SetDiscountType(EDiscountType discountType) { _discountStrategy = DiscountStrategyFactory.DiscountStrategyFor(discountType); }
public Basket(DiscountTypeEnum discountType) { _basketDiscountStrategy = BasketDiscountFactory.GetDiscount(discountType); }