我正在实施折扣计算模型。我确实有一个 <#>One 类:
public class Product
{
public string Name { get; set; }
public Size Size { get; set; }
public decimal Price { get; set; }
}我也有一个订单类:
public class Order
{
public DateTime Date { get; set; }
public string ProductName { get; set; }
public Size Size { get; set; }
}我有一些折扣:
在我的实现过程中,我想使用策略模式,但我没有多少细微之处:
解决方案:
IDiscount.cs
public interface IDiscount
{
public bool IsApplicable(Order order, int itemsCountForDiscount);
public decimal GetDiscountAmount(Order order);
}竞争折扣
public class CompetiveDiscount : IDiscount
{
Size Size { get; set; }
decimal LowestProductPrice { get; set; }
public CompetiveDiscount(Size size)
{
Size = size;
LowestProductPrice = GetLowestProductPriceBySize(size);
}
public decimal GetDiscountAmount(Product product)
{
return product.Price - LowestPriceProvider;
}
public bool IsApplicable(Product product, int itemsCountForDiscount)
{
//returns if applicable
}
private decimal GetLowestProductPriceBySize(Size size)
{
//returns lowest price of products
}
}数量折扣数
public class QuantityDiscount: IDiscount
{
string ProductName { get; set; }
int ItemsCountForDiscount { get; set; }
Period Period { get; set; } //enum for period
public QuantityDiscount(string productName, int itemsCountForDiscount, Period period)
{
ProductName = productName;
ItemsCountForDiscount = itemsCountForDiscount;
Period = period;
}
public decimal GetDiscountAmount(Product product)
{
return product.Price;
}
public bool IsApplicable(Product product, int itemsCountForDiscount)
{
//returns if applicable
}
}我的问题是,如果我确实需要在一个折扣中使用Date,那么我如何才能使这个模型工作,但在另一个折扣中,我不需要。使用连锁责任设计模式更好吗?
发布于 2020-08-17 18:17:08
策略模式的目的是封装一系列算法,并可互换地使用它们。但你现在的需求似乎完全不同:
但是,如果您仍然希望使用“策略”,则必须提供对计算数据的统一访问:
另一种办法是采用类似的方法,而是使用责任链。链中的每个折扣计算器都会发现它是否适用,如果需要,请调用下一个计算器。
但是如果我在你的地方,我不会专注于这个问题的设计模式。我会选择一个临时的打印引擎,你可以在其中添加规则,然后让引擎去找哪些规则适用,如何解决竞争规则之间的冲突,计算适用的折扣,并将它们结合起来。一旦您设计了这个引擎,您就可以开始考虑根据实际的设计问题来重构它,而不是过早的选择。
与Not相关的:在ERP上做了很多工作,我可以告诉您,折扣计算非常复杂,很少符合一个简单的模式,而且您的两种情况只触及销售人员所能发明的表面。
https://softwareengineering.stackexchange.com/questions/414913
复制相似问题