2017-04-07 206 views
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类。 但这是解决重复的最佳方法吗?或者我应该使用继承还是不同的东西?

+0

开始点:您计划如何测试您的方法? – tym32167

回答

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

(我也觉得这是一个坏主意,有公共领域像这样 - 但我猜/希望这只是示例代码,而您的真实代码没有那些......)

其他代码(创建AB的代码除外)将使用该对象通过Base类的类型。

+0

谢谢你的回答。是的,这是一个示例,我想将公共字段更改为属性。我对此有几个问题。为此使用继承的原因是什么?为什么不使用组合作为例子?为什么你要抽象的方法? –

+1

@BarryStotter该方法是抽象的,因为基类不知道如何实现该方法。它使用继承,因为这是唯一可以让虚拟方法在不同派生类中具有不同实现的唯一方法。你可以通过在构造函数中注入一个'DoStuff()'动作来完成它,但是对于这个特殊的问题,我不认为这比使用继承更好。 –

+0

当A类中的方法的实现完全不同于B类中的方法时,那么最好不要使用抽象方法?或者,也许使用组合呢? –

2

一个简单的选择是使用inheritance。使用共享功能创建基类,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 
    } 
}