Tag: 策略模式

参数化通用接口 – 创建一个Dictionary <Type,Interface >?

对不起,标题令人困惑,我不知道我说的是对的,不知道这个叫做什么……如果你想要的话,请在阅读问题后随意编辑。 当我注意到有很多地方可以使用策略模式时,我正在重构我的旧代码。 我有一个库存系统和物品 – 有多种方法可以添加物品 – 正常方式,有力的方式等等。您还可以用多种方式再次交换物品 – 所以我认为那些是好地方使用那种模式。 这是我有的,用于添加项目: public interface IAddingStrategy where T : AddArgs { bool Add(T args); } public class NormalAdd : IAddingStrategy { public bool Add(NormalAddArgs args) { // normal adding logic… } } public class ForceAdd : IAddingStrategy { public bool Add(ForceAddArgs args) { // force adding logic… } […]

策略模式与dependency injection

策略模式与dependency injection有何不同? 即以下是您可以使用策略模式执行的操作: class Foo{ private readonly ISortAlgo _sortAlgo; public Foo(ISortAlgo sortAlgo) { _sortAlgo = sortAlgo; } public void Sort() { _sortAlgo.sort(); } } 使用DI你可以做同样的事情,基本上你可以有构造函数,setter,接口等注入。 它会产生与战略模式相同的效果。 我知道DI也有其他原则,例如松耦合,可测试性,布线等。 在实施方面,我没有看到太大的区别。 策略模式和DI有什么区别?

在c#中执行此通用抽象类的最佳方法是什么?

我知道我做得不对,但我也知道有办法做到这一点。 我试图尽可能地通用和抽象,否则我的代码将变得非常混乱。 所以我在这里使用策略模式作为GetAggregateClient()方法。 我想要一个名为AbstractAggregate的抽象类,以便它使用generics。 将使用的类型是一系列数据类,即BlogItem,ResourceItem和AskItem。 这些数据类都inheritance自ListItem。 这就是背景信息。 这里的问题是我希望GetAbstractAggregate()返回一个实现AbstractAggregate的客户端类的实例,其中指定的项目类型取决于传入的枚举。但是,我不能返回“AbstractAggregate”。 编译器不会让我这样,因为AbstractAggregateFactory类不是通用的。 有没有人有最好的方法来做到这一点? 非常感谢。 public static class AggregateHelper { public enum AggregateTypes { TankTruckBlog, AskTankTruck, Resources } } public static class AbstractAggregateFactory { public static AbstractAggregate GetAggregateClient(AggregateHelper.AggregateTypes type) { switch (type) { case AggregateHelper.AggregateTypes.AskTankTruck: return new AskTankTruckAggregate(); case AggregateHelper.AggregateTypes.TankTruckBlog: return new TankTruckBlogAggregate(); case AggregateHelper.AggregateTypes.Resources: return new ResourcesAggregate(); default: […]

如何在C#中使用策略模式?

这是我到目前为止所拥有的: namespace Strategy { interface IWeaponBehavior { void UseWeapon(); } } namespace Strategy { class Knife : IWeaponBehavior { public void UseWeapon() { Console.WriteLine(“You used the knife to slash the enemy! SLASH SLASH!”); } } } namespace Strategy { class Pan : IWeaponBehavior { public void UseWeapon() { Console.WriteLine(“You use the pan! 100% Adamantium power! […]

在策略设计模式的情况下,Hangfire服务器无法选择作业

我有以下申请: 1) Mvc应用程序 :Hangfire客户端,我将在那里排队作业和主持仪表板。这个应用程序将包含我的类库的参考。 2) 控制台应用程序 :Hangfire服务器将存在于此控制台应用程序中。 3) 类库 :hangfire服务器(控制台应用程序)和hangfire客户端(asp.net mvc)之间的共享库,我的长时间运行的代码将驻留在该库中.Hangfire服务器将执行该库的代码。 我在战略设计模式下的类库中有如下结构。 代码取自以下: 参考: 接口: public interface IOperation { Output Process(Input input); bool AppliesTo(Type type); } public interface IOperationStrategy { Output Process(Type type,Input input); } 操作: public class Add : IOperation { public bool AppliesTo(Type type) { return typeof(Add).Equals(type); } public Output Process(Input input) { […]