2017-04-07 14 views
3

私はC#アプリケーションを作っています。アプリケーションには2つのクラスと複数のメソッドがあります。コードを書いている間、私は問題を見つけました。私は同じ2つの変数(XListとYList)と両方のクラスで1つのメソッドを使用します。おそらく私はこのコードでもっとクラスを必要とします。だから私は重複の問題を作りました。以下は私のコードの簡単なバージョンです:重複を避けるにはどうすればいいですか?

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()と呼ばれるのではなく1はDoStuff()と呼ばれ、他のDoDifferentStuff()と呼ばれる持ってすべきだと思います。

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

(私もそれが公共の場を持つことは悪いアイデアだと思う:

その後、派生クラスで異なる方法で実装される抽象DoStuff()方法を共通のコードを実装するためにABCを作成し、持つことができますこれはちょうどサンプルコードで、あなたの実際のコードにはそれらがないと思っています。)

他のコード(AまたはBを作成するコードを除く)は、オブジェクトを使用しますBaseクラスタイプを使用します。

+0

ありがとうございます。はい、サンプルです。パブリックフィールドをプロパティに変更したいと思います。私はこれについていくつか質問があります。このために継承を使用する理由は何ですか?なぜ構図を使わないの?そしてなぜあなたは抽象メソッドを作成しますか? –

+1

@BarryStotterメソッドは、基本クラスがメソッドの実装方法を知らないため、抽象メソッドです。これは継承を使用します。これは、異なる派生クラスで異なる実装を持つ仮想メソッドを持つ唯一の方法であるためです。 'DoStuff()'アクションをコンストラクタに挿入することでコンポジションで行うことができますが、この特定の問題については、継承を使用するよりも優れているとは思いません。 –

+0

クラスAのメソッドの実装がクラスBのメソッドと完全に異なる場合、抽象メソッドを使用しない方が良いでしょうか?それとも、構図を使うのか? –

2

単純なオプションの1つは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 
    } 
} 
関連する問題