C#、容器类设计模式?

发布于 2024-12-13 06:31:30 字数 1703 浏览 4 评论 0原文

我深入研究了 C#,但遇到了一个我似乎无法解决的问题:

我设计了一个财务回测和交易引擎,作为其中的一部分,我喜欢实现一个“策略容器”。这个策略容器应该有两个目的。 (1) 保存执行数学计算的不同策略的实例,以及 (2) 提供访问器,以便将新的报价数据传递到策略实例中并从策略实例接收某些返回的信息。

我上面提到的不同策略应该是唯一的类,但是它们是从基类派生的。我想在这些策略中执行完全不同的操作,因此我认为将它们设计为从一个基类派生的单独的类是可行的。一个复杂之处是我需要策略容器中包含的每个策略类的多个实例。每个策略类还包含一个股票代码列表,每个代码都应该获取其策略实例。

策略容器的另一个重要点是它可以创建、实例化以及稍后调用每个策略实例。

我想问你们中的一些人对于如何设计和实现这个有什么想法:

  • 总体上最好的设计模式是什么?
  • 正如所描述的,如果我为每个策略创建不同的类,那么我显然也会得到不同的类型,我如何将所有这些都保存在策略容器内的一个容器/集合中?
  • 我如何通过反射或其他方式创建、实例化和调用每个策略实例的方法,而不知道最终会得到多少个策略实例。我确实知道策略的名称和类型,也知道股票代码的名称。
  • 我只是想提供我想要连接的策略(我提供策略名称=类名称?),并为每个策略提供一个股票代码列表。策略容器将为列表所提供的策略类型的每个策略中的每个交易品种创建实例,并随后调用“RunStrategy(Quote quote)”方法,该方法将新的报价输入到每个实例中,以便可以执行计算。
  • 一个目标是保持每个策略类的接口尽可能干净,大多数标准(重复)功能在派生基类中实现。

我不要求完整的源代码,而是要求您认为我应该如何设计这个东西以及如何完成上述每一点。这是我自己的东西,不会变成任何商业产品。我非常擅长编写实现数学点点滴滴的代码,但我不太熟悉设计模式和系统架构。

编辑: Graymatter,我玩了一下,看来你提供的正是我想要的东西。多谢。

class Program
{
    static void Main(string[] args)
    {
        List<IStrategy> container = new List<IStrategy>();

        container.Add(new StrategyOne());

        container[0].AddValue(50);

        Console.ReadLine();
    }




}

public interface IStrategy
{
    void AddValue(int value);  
}


public class StrategyOne : StrategyBase
{
    public override void Calculates()
    {
        Console.WriteLine("This is my value: " + myValue);
    }

}

public class StrategyBase : IStrategy
{
    protected int myValue;

    public void AddValue(int value)
    {
        Console.WriteLine("Run Strategy in Base");

        myValue = value;

        Calculates();
    }

    public virtual void Calculates()
    {

    }  
}

I delved into C# but came across a problem that I seem unable to solve:

I designed a financial back test and trading engine and as part of that I like to implement a "Strategy Container". This strategy container should serve two purposes. (1) hold instances of different strategies that perform mathematical computations, and (2) provide accessors in order to pass new tick data into the strategy instances and to receive certain information back from the strategy instances.

The different strategies I mentioned above should be unique classes which, however, derive from a base class. I want to perform completely different operations within those strategies thus I thought designing them as separate classes that derive from one base class would be feasible. One complication is that I need multiple instances of each strategy class that is contained in the Strategy Container. Each strategy class also holds a list of stock symbols and each symbol should get its on strategy instance.

Another important point of the Strategy container is that it can create, instantiate, and later also invoke each strategy instance.

I wanted to ask some of you what kind of ideas you have how I could go about designing and implementing this:

  • what would be the overall best design pattern?
  • as described if I create different classes for each strategy then I would obviously end up with different types as well, how could I hold all those in one container/collection within the Strategy Container?
  • How can I either through reflection or other means create, instantiate, and invoke methods of each single strategy instance without knowing how many I would end up with. I do know the names and types of the strategies and I know the names of the stock symbols.
  • I simply want to supply which strategies I want to hook up (I supply the strategy names = class names?) and for each strategy a list of stock symbols. The Strategy Container would create instances for each symbol in each strategy of the strategy type that the list was supplied for and would subsequently invoke a "RunStrategy(Quote quote)" method that feeds new quotes into each instance so that computations can be performed.
  • One goal would be to keep the interface of each strategy class as clean as possible, with most of the standard (repetitive) functionality being implemented in the derived-from base class.

I do not ask for full source code but for ideas what you think how I should design this thing and how I could accomplish each of above points. This is something for myself and wont turn into any commercial product. I am very comfortable with writing code that implements the mathematical bits and pieces but I am less familiar with design patterns and system architecture.

Edit:
Graymatter, I played a bit around and seems you provided exactly what I was looking for. Thanks a lot.

class Program
{
    static void Main(string[] args)
    {
        List<IStrategy> container = new List<IStrategy>();

        container.Add(new StrategyOne());

        container[0].AddValue(50);

        Console.ReadLine();
    }




}

public interface IStrategy
{
    void AddValue(int value);  
}


public class StrategyOne : StrategyBase
{
    public override void Calculates()
    {
        Console.WriteLine("This is my value: " + myValue);
    }

}

public class StrategyBase : IStrategy
{
    protected int myValue;

    public void AddValue(int value)
    {
        Console.WriteLine("Run Strategy in Base");

        myValue = value;

        Calculates();
    }

    public virtual void Calculates()
    {

    }  
}

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

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

发布评论

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

评论(2

末骤雨初歇 2024-12-20 06:31:30

您确实应该考虑使用此类系统的接口。该接口是您与策略的契约,您可以为该接口提供任意数量的不同实现。然后您的容器将包含接口。

像这样:

public interface IStrategy
{
  void RunStrategy(Quote quote);
}

那么你的实现将是这样的:

public class StrategyOne : IStrategy
{
  void RunStrategy(Quote quote)
  {
  }
}

容器可以是:

List<IStrategy> container = new List<IStrategy>();

You should really look at using interfaces for this type of system. The interface is your contract with the strategy and you can any number of different implementations for the interface. Your container would then contain interfaces.

Something like:

public interface IStrategy
{
  void RunStrategy(Quote quote);
}

Then your implementations would be something like this:

public class StrategyOne : IStrategy
{
  void RunStrategy(Quote quote)
  {
  }
}

The container could be:

List<IStrategy> container = new List<IStrategy>();
萤火眠眠 2024-12-20 06:31:30

那么,设计应该出现需求分析/处理。我不会只是决定使用这个或那个模式,然后尝试看看它如何适合该领域。

从基类派生

同样,这必须由基类要完成的通用功能来保证,否则,您只需要一个接口。策略模式由通用接口定义,其中所有策略都作用于通用数据结构。

所以我的建议是放弃对设计的任何预先判断,完全专注于您领域的需求和建模。好的设计就会由此产生。

Well, design should emerge out of the requirement analysis/crunching. I would not just decide to use this or that pattern and then try to see how it fits to the domain.

derive from a base class

Again this must be warranted by the common functionality that is to be done by the base class, otherwise, an interface is all you need. Strategy pattern is defined by a common interface where all strategies act upon a common data structure.

So my suggestion is to abandon any pre-judgement about the design and fully focus on the requirements and the modelling of your domain. Good design will just emerge out of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文