Tag: strategy pattern

C#:抽象策略基类,用作Strategy对象的抽象工厂

我正在尝试为我的公司创建一个基于Web的工具,实质上,它使用地理输入来生成表格结果。 目前,三个不同的业务领域使用我的工具并接收三种不同的输出。 幸运的是,所有输出都基于主表 – 子表的相同概念,甚至它们共享一个共同的主表。 不幸的是,在每种情况下,子表的相关行包含非常不同的数据。 因为这是唯一的争用点,我将FetchChildData方法提取到一个名为DetailFinder的单独类中。 结果,我的代码看起来像这样: DetailFinder DetailHandler; if (ReportType == “Planning”) DetailHandler = new PlanningFinder(); else if (ReportType == “Operations”) DetailHandler = new OperationsFinder(); else if (ReportType == “Maintenance”) DetailHandler = new MaintenanceFinder(); DataTable ChildTable = DetailHandler.FetchChildData(Master); PlanningFinder,OperationsFinder和MaintenanceFinder都是DetailFinder的子类。 我刚被要求增加对另一个业务领域的支持,并且if阻止趋势, if不愿继续这样做。 我更喜欢的是有一个看起来像这样的解析方法: DetailFinder DetailHandler = DetailFinder.Parse(ReportType); 但是,我DetailFinder知道如何让DetailFinder知道哪个子类处理每个字符串,甚至是什么子类存在而不仅仅将if块移动到Parse方法。 有没有办法让子类用抽象的DetailFinder注册自己?

Unity解析多个类

如何获得microsoft unity以“构造”给定接口类型的类列表。 很简单的例子: List list = new List(); list.Add(new NewYorkShippingCalculation()); list.Add(new FloridaShippingCalculation()); list.Add(new AlaskShippingCalculation()); //Not What I want public void calcship(List list) { var info = new ShippingInfo(list); info.CalculateShippingAmount(State.Alaska) } //Somehow in unity, must i do this for all the concrete classes? //how does it know to give a list. Container.RegisterType();?? //What I want public […]

用于状态处理的多态Enum

如何在不使用C#中的switch或if语句的情况下处理枚举? 例如 enum Pricemethod { Max, Min, Average } ……我有一篇文章 public class Article { private List _pricehistorie; public List Pricehistorie { get { return _pricehistorie; } set { _pricehistorie = value; } } public Pricemethod Pricemethod { get; set; } public double Price { get { switch (Pricemethod) { case Pricemethod.Average: return Average(); case Pricemethod.Max: […]

dependency injection与策略模式

关于这个话题的讨论很多,但每个人似乎都错过了一个明显的答案。 我想帮助审查这个“明显的”IOC容器解决方案。 各种对话假设运行时选择策略和使用IOC容器。 我将继续这些假设。 我还想补充一个假设,即它不是必须选择的单一策略。 相反,我可能需要检索一个对象图,该对象图在整个图的节点中找到了几个策略。 我将首先快速概述两个常用的解决方案,然后我将展示我希望看到IOC容器支持的“明显”替代方案。 我将使用Unity作为示例语法,但我的问题不是Unity特有的。 命名绑定 这种方法要求每个新策略都手动添加绑定: Container.RegisterType(); Container.RegisterType(“Alpha”); Container.RegisterType(“Beta”); ……然后明确要求正确的策略: var strategy = Container.Resolve(“Alpha”); 优点:简单,并得到所有IOC容器的支持 缺点: 通常将调用者绑定到IOC容器,当然要求调用者知道有关策略的信息(例如名称“Alpha”)。 必须手动将每个新策略添加到绑定列表中。 此方法不适合处理对象图中的多个策略。 简而言之,它不符合要求。 抽象工厂 为了说明这种方法,假设以下类: public class DataAccessFactory{ public IDataAccess Create(string strategy){ return //insert appropriate creation logic here. } public IDataAccess Create(){ return //Choose strategy through ambient context, such as thread-local-storage. } } public […]

使用Unity的策略模式和dependency injection

我终于忍受了Dependency Injection(早就应该); 我开始玩Unity并遇到战略模式的问题。 我可以使用容器向我返回基于名称的策略的特定实现,但我没有看到我应该如何在上下文中获得正确的策略。 让我们举一个简单的例子说明:上下文是一辆汽车,它有一个IEngine(战略),有2个实现,FastEngine和SlowEngine。 代码将沿着这些方向看: public interface IEngine { double MaxSpeed { get; } } internal class FastEngine:IEngine { public double MaxSpeed { get { return 100d; } } } internal class SlowEngine:IEngine { public double MaxSpeed { get { return 10d; } } } public class Car { private IEngine engine; public double […]