我的问题是我想应用策略模式来保存日志
从上图
我将使用替换我的班级来形象化
上下文 = 日志类策略 = ILog
具体策略A = CreateSituation
具体策略B = NameSituation
具体策略C = StatusSituation
[来自图像的附加]
具体策略D = PriceSituation
具体策略E = DiscountSituation
在 ILog 中有这些方法 SaveLog(Log log);
每个具体的StrategyA、B、C、D、E 实现 ILog
公共类 CreateSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "This product is created";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
公共类 NameSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this produce has updated name from \"oldProduct\" to \"newProduct\"";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
公共类StatusSituation:ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this produce has updated status from \"In stock\" to \"Sold Out\"";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
公共课 PriceSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this produce has updated price from $10 to $150";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
公共类 DiscountSituation : Ilog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this product has updated discount price from $0 to $20";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
未来可以有比现在更多的情况
我展示的这些课程是解决这些问题的好方法