2017-04-07 183 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 
    } 
}