如何避免重复?

3

我正在制作一个C#应用程序。该应用程序有两个类和多个方法。在编写代码时,我遇到了一个问题。我在两个类中使用相同的两个变量(XList和YList)和一个方法。而且我可能需要更多的类来使用这段代码。所以我创建了一个重复的问题。以下是我的代码的简单版本:

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
  }
}

我的问题是如何最好地解决这个重复问题?

经过一些研究,我发现我可能可以通过继承或组合来解决这个问题。我还读到人们选择组合而不是继承。所以我编写了以下代码来解决重复问题:

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()
  }
}

你可以看到我创建了一个处理从数据库获取数据的类。A和B类都使用了DataPreparation类。 但这是解决重复的最佳方法吗?或者我应该使用继承或其他方法?


你打算如何测试你的方法? - tym32167
2个回答

4
我认为你可能只需要一个名为DoStuff()的方法,而不是一个名为DoStuff()和另一个名为DoDifferentStuff()的方法。
然后,您可以创建一个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
    }
}

我认为像这样公开的字段是个坏主意——但我猜想/希望这只是示例代码,你真正的代码不会那样做...
除了创建A或B对象的代码外,其他代码都会通过Base类类型使用该对象。

谢谢你的回答。是的,这是一个示例,我想将公共字段更改为属性。我有几个关于此的问题。使用继承的原因是什么?为什么不使用组合,例如?还有,为什么要创建一个抽象方法? - Barry The Wizard
1
@BarryStotter 这个方法是抽象的,因为基类不知道如何实现这个方法。它使用继承,因为这是唯一的方式,可以在不同的派生类中有不同的虚拟方法实现。你可以通过将 DoStuff() 动作注入到构造函数中来使用组合实现,但对于这个特定的问题,我认为这并不比使用继承更好。 - Matthew Watson
当类A中的方法实现与类B中的方法完全不同时,是否最好不使用抽象方法?或者使用组合? - Barry The Wizard
@BarryStotter 如果你想要将对象传递给一个可以与class Aclass B一起使用的方法,那么通常最好使用多态性(即继承)。如果你不想这样做,那么使用组合可能更好。但是从你的示例中很难判断,因为它不是真正的代码。 - Matthew Watson

2

一个简单的选择是使用继承。创建一个具有共享功能的基类,AB可以从中继承。例如:

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
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接