简体   繁体   English

如何避免重复?

[英]How to avoid duplication?

I'm making a C# application. 我正在制作一个C#应用程序。 The application has two classes and multiple methods. 该应用程序有两个类和多个方法。 While writing the code I stumbled on a problem. 在编写代码时,我偶然发现了一个问题。 I use the same two variables (XList and YList) and one method in both classes. 我在两个类中使用相同的两个变量(XList和YList)和一个方法。 And probably I will need more classes with this code. 并且我可能需要更多类来使用此代码。 So I created a duplication problem. 所以我创建了一个重复问题。 Below is a simple version of my code: 以下是我的代码的简单版本:

public class A {
  private testEntities db = new testEntities();
  public List<int> XList = new List<int>();
  public List<int> YList = new List<int>();

  public void GetAllInfo()
  {
    // Get the data from a database and add to a list
    XList = db.Table1.ToList();
    YList = db.Table2.ToList();
  }

  public void DoStuff()
  {
    // Do Stuff with XList and YList
  }
}

public class B {
  private testEntities db = new testEntities();
  public List<int> XList = new List<int>();
  public List<int> YList = new List<int>();

  public void GetAllInfo()
  {
    // Get the data from a database and add to a list (the same as in class A)
    XList = db.Table1.ToList();
    YList = db.Table2.ToList();
  }

  public void DoDifferentStuff()
  {
    // Do ddifferent stuff with XList and YList then in class A
  }
}

My question is what is the best way to solve this duplication problem? 我的问题是解决这个重复问题的最佳方法是什么?

After some research I found that I can probably solve this with inheritance or composition. 经过一些研究后,我发现我可以通过继承或组合来解决这个问题。 I also read that people choose composition over inheritance. 我还读到人们选择构图而不是继承。 So I wrote the following code to solve the duplication: 所以我编写了以下代码来解决重复问题:

public class DataPreparation
{
  private testEntities db = new testEntities();
  public List<int> XList = new List<int>();
  public List<int> YList = new List<int>();

  public void GetAllInfo()
  {
    // Get the data from a database and add to a list
    XList = db.Table1.ToList();
    YList = db.Table2.ToList();
  }

  // Implement other methods
}

public class A 
{
  public void MethodName()
  { 
    DataPreparation dataPreparation = new DataPreparation();
    dataPreparation.GetAllInfo();

    UseDataX(dataPreparation.XList);
    UseDataY(dataPreparation.YList);

    // Implementation UseDataX() and UseDataY()
  }
}

public class B 
{
  public void MethodName()
  { 
    DataPreparation dataPreparation = new DataPreparation();
    dataPreparation.GetAllInfo();

    VisualizeDataX(dataPreparation.XList);
    VisualizeDataY(dataPreparation.YList);

    // Implementation VisualizeDataX() and VisualizeDataY()
  }
}

As you can see I made a class that handles getting the data from the database. 正如您所看到的,我创建了一个处理从数据库中获取数据的类。 And that the class A and B use the DataPreparation class. 而A类和B类使用DataPreparation类。 But is this the best way to solve the duplication? 但这是解决重复问题的最佳方法吗? Or should I use inheritance or something different? 或者我应该使用继承或不同的东西?

I think you should probably only have one method called DoStuff() rather than one called DoStuff() and another called DoDifferentStuff() . 我认为你应该只有一个名为DoStuff()方法,而不是一个名为DoStuff() ,另一个名为DoDifferentStuff()

Then you can create an ABC to implement the common code, and have an abstract DoStuff() method that is implemented differently in the derived classes: 然后,您可以创建一个ABC来实现公共代码,并在派生类中实现不同的抽象DoStuff()方法:

public abstract class Base
{
    private testEntities db = new testEntities();

    public List<int> XList = new List<int>();
    public List<int> YList = new List<int>();

    public void GetAllInfo()
    {
        // Get the data from a database and add to a list (the same as in class A)
        XList = db.Table1.ToList();
        YList = db.Table2.ToList();
    }

    public abstract void DoStuff();
}

public class A: Base
{
    public override void DoStuff()
    {
        // Do Stuff with XList and YList
    }
}

public class B: Base
{
    public override void DoStuff()
    {
        // Do ddifferent stuff with XList and YList then in class A
    }
}

(I also think it's a bad idea to have public fields like that - but I'm guessing/hoping that this is just sample code and your real code doesn't have those...) (我也认为拥有这样的公共字段是一个坏主意 - 但我猜/希望这只是示例代码,而您的真实代码没有那些......)

Other code (except code that creates an A or a B ) would use the object via the Base class type. 其他代码(创建AB代码除外)将通过Base类类型使用该对象。

One simple option is to use inheritance . 一个简单的选择是使用继承 Make a base class with the shared functionality and the A and B can inherit from that. 使用共享功能创建基类, AB可以继承该基类。 For example: 例如:

public abstract class Base
{
    private testEntities db = new testEntities();
    public List<int> XList = new List<int>();
    public List<int> YList = new List<int>();

    public void GetAllInfo()
    {
        // Get the data from a database and add to a list
        XList = db.Table1.ToList();
        YList = db.Table2.ToList();
    }
}

public class A : Base
{
    public void DoStuff()
    {
        // Do Stuff with XList and YList
    }
}

public class B : Base
{
    public void DoDifferentStuff()
    {
        // Do ddifferent stuff with XList and YList then in class A
    }
}

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

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