简体   繁体   English

OOP创建和复制依赖于一个值的对象

[英]OOP creating and copying an object that depends on one value

I am sorry but I didn't know what to call this post (if you have a better title please tell me in a comment). 很抱歉,但我不知道该怎么称呼(如果您的标题更好,请在评论中告诉我)。

Say for instance you have the following Object whose purpose is to create chart series of the data specified in the Constructor : 假设您有以下Object其目的是创建Constructor指定的数据的图表系列:

/**
 * Helper to generate chart series 
 */
public class ChartHelper
{
    public System.Windows.Forms.DataVisualization.Charting.Chart ChartType { get; set; }
    public String TimeType { get; set; }
    private readonly List<IObject> _datalist;
    private readonly TimeType _timeType;
    private readonly DateTime _stopDate;
    private readonly DateTime _startDate;

    public ChartHelper(List<IObject> dataList, TimeType timeType, DateTime startDate, DateTime stopDate)
    {
        _startDate = startDate;
         _stopDate = stopDate;
        _datalist = dataList;
        _timeType = timeType;
    }


    public System.Windows.Forms.DataVisualization.Charting.Chart GetChart()
    {
        CreateSeries(_startDate);
        return ChartType;
    }

    private void CreateSeries(DateTime seriesTime)
    {
      //Do something
    }

   //More internal private methods

}

Now say for instance you have a program that creates 10 different Charts but only the value of the List<IObject> dataList changes. 现在说,例如,您有一个程序可以创建10个不同的Charts但是只有List<IObject> dataList的值List<IObject> dataList更改。

Then you could do one of two things: 然后,您可以执行以下两项操作之一:

  1. Create 10 different ChartHelper Objects 创建10个不同的ChartHelper Objects
  2. Use the same Object and change the dataList value 使用相同的Object并更改dataList

This is of course an example of how the problem could be presented when developing (ive met this problem several times) 这当然是在开发时如何提出问题的示例(我多次遇到此问题)

My question is, is there a design pattern that helps you solve this issue ? 我的问题是,有没有一种可以帮助您解决此问题的设计模式? Or is there a best practice method that would be useful for these situations? 还是有针对这些情况的最佳实践方法? It is important for me to learn these methods as I wish to improve my own skills. 对我来说,学习这些方法很重要,因为我希望提高自己的技能。

If only the data is different then I would recommend using the same class and creating 10 different objects from it. 如果只有数据不同,那么我建议使用相同的类并从中创建10个不同的对象。

If however the implementation of the CreateSeries would be different depending on the type of data, than this would be a candidate for the Strategy pattern. 但是,如果CreateSeries的实现根据数据类型而有所不同,则可以选择使用Strategy模式。 In that case you would extract the creation of the series behind an interface and provide implementations for the different kinds of series. 在这种情况下,您将在界面后面提取系列的创建,并提供不同类型系列的实现。 You could then also have a factory that picks the correct strategy depending on the data and composes a chart (helper). 然后,您还可以拥有一个工厂,该工厂根据数据选择正确的策略并组成图表(帮助程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM