关于订单的多件折扣及优惠券实现设计模式(组合+策略模式)

首次用的策略+组合模式:

现实中,我们要实现一个M件N折的功能

意思是,满几件,打几折的电商优惠活动。

后面考虑来,优惠券,于是统一使用这个模式去实现该功能:

先上设计图:

其中

order类,与打折类(接口)是组合的关系。

即,优惠券或M件N折,均是Order的一部份。

先说,M件N折:

实现步骤:

首先设计类及接口。

Order类的设计:

1.给Order类,添加一个私有成员,iCalCount接口。ICalDiscountPromotion iCalDiscount。

2.给Order类,添加一个折扣,AddCalDiscount(iCalDiscount)

ICalDiscountPromotion的设计:

1.声明接口

2.实现这个接口的两个类

3.每个类,分别实现各自的CalDiscount()计算优惠价格。

最后,调用的实现:

Order order=new Order()

Order.AddCalDisCount(new CalDiscountActivity(价格,数量,规则)

至此,简单的模式,实现折扣优惠。

后面再有优惠,就可以添加一个新类去计算

代码:

public class Order {

private ICalDiscountPromotion _calDiscount;

/// <summary>

/// 加一个折扣优惠

/// </summary>

/// <param name=”calDiscount“></param>

public void AddCalDiscount(ICalDiscountPromotion calDiscount) {

this._calDiscount = calDiscount;

}

/// <summary>

/// 设置实际订单总额(ICalDiscountPromotion内部计算并设置各种价格,用于优惠折扣)

/// </summary>

/// <returns></returns>

public void SetOrderSum() {

OrderSum = _calDiscount.CalDiscount();

}

}

public interface ICalDiscountPromotion {

int CalDiscount();

}

/// <summary>

/// M件N折

/// </summary>

public class CalDiscountActvitity : ICalDiscountPromotion {

public decimal CalDiscount(int price)

// {

// return 0.1M;

// }

private int activityQty;

private int activityOrdinaryPrice;

private List<ActivityRule> activityRules;

private int activityReduceMoney;

private int activityPrice;

CalDiscountActvitity(int ordinaryPrice, int qty, List<ActivityRule> rules) {

activityOrdinaryPrice = ordinaryPrice;

activityQty = qty;

activityRules = rules;

}

/// <summary>

/// 订单ID

/// </summary>

public int OrderId { get; set; }

/// <summary>

/// 活动ID

/// </summary>

public int ActivityId { get; set; }

/// <summary>

/// 优惠前原始价格(当进行CalTotalPrice后,会同时更新此价格)

/// </summary>

public int ActivityOrdinaryPrice { get { return activityOrdinaryPrice; } }

/// <summary>

/// 满足条件后的少了多少金额

/// </summary>

public int ActivityReduceMoney {

get { return activityReduceMoney; }

}

/// <summary>

/// 优惠后价格(当进行CalTotalPrice后,会同时更新此价格)

/// </summary>

public int ActivityPrice { get { return activityPrice; } }

/// <summary>

/// 计算订单的活动折扣信息(如订单有2件打8折总价100元,通过此计算,会把OrderSum的重新计算比如80元)

/// 1.设置优惠了多少金额

/// 2.设置优惠总价

/// </summary>

public int CalDiscount() {

if (activityRules.Count > 0) {

//0 <= x < 2;

//2 <= x < 3;

//3 <= x < 4;

//4 <= x < 999;

for (int i = 0; i < activityRules.Count; i++) {

if (activityQty >= activityRules[i].FromQty && activityQty < activityRules[i].ToQty) {

activityPrice = Convert.ToInt16(Convert.ToDecimal(activityOrdinaryPrice) * activityRules[i].Discount);

break;

}

}

//foreach (var rule in activityRules) {

// if (qty >= rule.FromQty && qty < rule.ToQty) //2件,8折

// {

// activityReduceMoney = Convert.ToInt16(Convert.ToDecimal(ordinaryPrice) * rule.Discount);

// break;

// }

//}

activityReduceMoney = activityOrdinaryPrice – activityPrice;

}

return activityPrice;

}

}

/// <summary>

/// 优惠券

/// </summary>

public class CalDiscountCoupon : ICalDiscountPromotion {

public int CalDiscount() {

return 0;

}

}

}

原文链接:https://blog.csdn.net/weixin_44019016/article/details/95618050?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165277499316782390570638%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165277499316782390570638&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-7-95618050-null-null.nonecase&utm_term=%E4%BC%98%E6%83%A0

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
文明发言,共建和谐米科社区
提交
头像

昵称

取消
昵称表情图片