接口中具有不同参数的策略模式 (C#)

发布于 2024-08-16 05:00:30 字数 903 浏览 3 评论 0原文

我基本上是在尝试实现策略模式,但我想将不同的参数传递给“接口”实现(从同一对象继承),并且不知道这是否可能。 错误,

也许我选择了错误的模式,我收到类似于“StrategyA”没有实现继承的抽象成员“void DoSomething(BaseObject)”的

代码如下:

abstract class Strategy
{
 public abstract void DoSomething(BaseObject object);
}

class StrategyA : Strategy
{
 public override void DoSomething(ObjectA objectA)
 {
  // . . .
 }
}

class StrategyB : Strategy
{
 public override void DoSomething(ObjectB objectB)
 {
  // . . .
 }
}

abstract class BaseObject
{
}

class ObjectA : BaseObject
{
 // add to BaseObject
}

class ObjectB : BaseObject
{
 // add to BaseObject
}

class Context
{
   private Strategy _strategy;

 // Constructor
 public Context(Strategy strategy)
 {
  this._strategy = strategy;
 }

    // i may lose addtions to BaseObject doing this "downcasting" anyways?
 public void ContextInterface(BaseObject obj) 
 {
  _strategy.DoSomething(obj);
 }

}

I am basically trying to implement a Strategy pattern, but I want to pass different parameters to the "interfaces" implementation (that inherit from the same object) and don't know if this is possible. Maybe I'm choosing the wrong pattern, I get an error similar to

'StrategyA' does not implement inherited abstract member 'void DoSomething(BaseObject)'

with the code below:

abstract class Strategy
{
 public abstract void DoSomething(BaseObject object);
}

class StrategyA : Strategy
{
 public override void DoSomething(ObjectA objectA)
 {
  // . . .
 }
}

class StrategyB : Strategy
{
 public override void DoSomething(ObjectB objectB)
 {
  // . . .
 }
}

abstract class BaseObject
{
}

class ObjectA : BaseObject
{
 // add to BaseObject
}

class ObjectB : BaseObject
{
 // add to BaseObject
}

class Context
{
   private Strategy _strategy;

 // Constructor
 public Context(Strategy strategy)
 {
  this._strategy = strategy;
 }

    // i may lose addtions to BaseObject doing this "downcasting" anyways?
 public void ContextInterface(BaseObject obj) 
 {
  _strategy.DoSomething(obj);
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月如刀 2024-08-23 05:00:30

听起来您实际上是在尝试重新发明 访问者模式,而不仅仅是使用策略模式本来的样子。

另外,由于您使用的是 C#,我建议您阅读 Judith Bishop 的论文,标题为 论 C# 3.0 中实现的设计模式的效率。这详细介绍了访问者模式的多种方法,并且有一些有趣的、相关的有用的想法。

It sounds like you're actually trying to reinvent the Visitor pattern, instead of just using the Strategy pattern the way it was intended.

Also, since you're using C#, I'd recommend reading Judith Bishop's paper titled On the Efficiency of Design Patterns Implemented in C# 3.0. This covers multiple approaches to the visitor pattern in detail, and has some interesting, related useful ideas.

韵柒 2024-08-23 05:00:30

在C#中,方法签名包括其名称、类型参数列表和形式参数列表。在上面的代码中,“覆盖”具有与虚拟方法不同的签名,因此这是不允许的。

策略模式背后的核心思想是定义一组可互换的算法,其中隐藏着细节。但是,如果您的策略(按类型)在可接受的输入内容上有所不同,那么它们就不再可以互换。所以在这种情况下使用这种模式似乎是错误的。

In C# method signature includes its name, type parameter list and formal parameter list. In the code above "overrides" have different signatures than virtual method and thus it is not allowed.

The core idea behind Strategy Pattern is to define set of interchangeable algorithms with details hidden inside. But if your strategies differ (by type) in what they can accept as input they are no longer interchangeable. So it seems this a wrong pattern to use in this situation.

随心而道 2024-08-23 05:00:30

您可能需要考虑这篇文章:
http://hillside.net/plop/2010/papers/sobajic.pdf
该模式称为“参数化策略模式”,应该符合您的需要。基本上,它建立在策略模式的基础上,并允许策略(不同的算法)具有不同的参数。参数被封装在特殊的类中,即参数类。每个策略(即算法)都需要实现 GetParameters() 方法,该方法返回特定算法的参数实例列表。

You might want to consider this article:
http://hillside.net/plop/2010/papers/sobajic.pdf
The pattern is called "parameterized strategy pattern" and should match what you need. Basically, it builds up on the strategy pattern and allows for strategies (different algorithms) to have different parameters. Parameters are encapsulated in special classes, i.e. parameter classes. Each strategy (i.e. algorithm) needs to implement GetParameters() method which sends back the list of parmaters instances for specific algorithm.

予囚 2024-08-23 05:00:30

策略模式旨在为相同类型的输入对象提供不同的行为。

您实际上想要做的事情取决于上下文,我不确定是否可以从发布的代码中看出这一点。

The strategy pattern is meant to provide different behaviour on input objects of the same type.

What you're actually trying to do is context-dependent, and I'm not sure it can be seen from the code that was posted.

掐死时间 2024-08-23 05:00:30

您可以像这样创建一个参数类:

public class Parameters
{
  public ObjectA {get; set;}
  public ObjectB {get; set;}
}

改变您的方法以接受参数,例如:

class StrategyA : Strategy
{
 public override void DoSomething(Parameters parameters)
 {
      //  Now use ObjectA
      if(parameters.ObjectA.SomeProperty == true)
      { ... }
 }
}

这样,如果您的需求将来发生变化,您可以添加其他参数。另一种选择是使用 Dictionary 您可以执行以下操作:

class StrategyA : Strategy
{
     public override void DoSomething(Dictionary<string, object>parameters)
     {
          //  Now use ObjectA
          var someProperty = (bool)parameters["SomeProperty"];
          if() ...
     }
}

You could create a Parameters class like so:

public class Parameters
{
  public ObjectA {get; set;}
  public ObjectB {get; set;}
}

The alter your methods to accept Parameters such as:

class StrategyA : Strategy
{
 public override void DoSomething(Parameters parameters)
 {
      //  Now use ObjectA
      if(parameters.ObjectA.SomeProperty == true)
      { ... }
 }
}

This way you can additional parameters should your requirements change in the future. Another alternative is to use Dictionary<string, object> where you can do:

class StrategyA : Strategy
{
     public override void DoSomething(Dictionary<string, object>parameters)
     {
          //  Now use ObjectA
          var someProperty = (bool)parameters["SomeProperty"];
          if() ...
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文