2017-05-20 2 views
0

この場合、良いOOPデザインについては疑問があります。OOPデザインセッター対メソッドのパラメータ渡し

この例は現実的ではありません。しかし、プライベートコードなので、実際の例は教えてくれません。しかし、概念の例はまったく同じです。

私は文字列のリストを格納するクラスがあり、ThereIsString(string mystring)というメソッドがあります。

私がその文字列で行う計算が、文字列の1つにlistOfStringであるかどうかによって、trueかfalseを返します。

public class StringBagAlgorithm() 
{ 
    List<string> listofString = new List<string>(); 

    public boolean ComputeString(string myString) 
    { 
     return true or false depending on the computation with the list of strings; 
    } 
} 

OK:

例(これはプライベートなアルゴリズムです)。文字列のリストがそのようにStringBagAlgorithmへの参照を持っているListOfStringsと呼ばれる別のクラスに格納されています

public class ListOfStrings() 
{ 
    List<string> listofString = new List<string>(); 
    List<string> MySecondListofString = new List<string>(); 
    StringBagAlgorithm _bagAlgorithm 

    public ListOfStrings(StringBagAlgorithm bagAlgorithm) 
    { 
     this._bagAlgorithm = bagAlgorithm; 
    } 

    public void ComputeSecondList() 
    { 
     for (int i=0; i<MySecondListofString; i++) 
      _bagAlgorithm.ComputeString(MySecondListofString[i]); 
    } 
} 

私の質問はStringBagAlgorithmにlistofStringを渡すの最良の方法は何かということです。たとえばforループでこれを実行すると、次のようになります。

_bagAlgorithm.ComputeString(MySecondListofString [i]、listofString);

またはforループを実行する前にセッターを使用して実行します。または他のオプション?

ルーズカップリングと単体テストの最適なOO設計を知りたいですか?また、私はsetterを一度使用し、すべての呼び出しでリストを渡さないことで、パフォーマンスは向上しますが、デザインは悪化していると思いますか?

+2

セッターとは共有され、変更可能なデータを意味します。メソッドパラメータとしてオブジェクトを渡し、それを変更しないで、より機能的でスレッドセーフなアプローチです。 – duffymo

+1

ComputeStringは各基本クラスに固有のものになりますか?その場合、抽象基本クラスを持つことができ、継承クラスはそれをオーバーライドしてComputeStringを実装します。 – Mez

+0

ComputeStringアルゴリズムはユニークですが、私はおそらく抽象クラスを使用するのが良い点です。しかし、私の疑問は、すべてのメソッド呼び出しでリスト全体を渡すか、セッターで渡す方が良いかどうかです。それは最高のデザインですか?ユニットテストとルーズカップリングの両方 – user3032175

答えて

0

それはそのようなものにする必要があります:あなたのクラスListOfStringsを呼び出すための

public class StringBagAlgorithm 
{ 
    public List<string> ListofString { get; set; } 

    public bool ComputeString(string myString) 
    { 
     //return true or false depending on the computation with the list of strings; 
     return true; 
    } 
} 

public class StringsComputer 
{ 
    public List<string> FirstList { get; set; } 
    public List<string> SecoundList { get; set; } 
    public StringBagAlgorithm BagAlgorithm { get; set; } 

    public StringsComputer(StringBagAlgorithm bagAlgorithm, List<string> listA, List<string> listB) 
    { 
     BagAlgorithm = bagAlgorithm; 
     FirstList = listA; 
     SecoundList = listB; 
    } 

    public StringsComputer() 
    { 
    } 

    public void ComputeSecondList() 
    { 
     if(BagAlgorithm != null) 
     { 
      for (int i = 0; i < SecoundList.Count; i++) 
       BagAlgorithm.ComputeString(SecoundList[i]); 
     } 
    } 
} 

public class program 
{ 
    public static void Main() 
    { 
     List<string> listA = new List<string>() { "A", "B", "C", "D" }; 
     List<string> listB = new List<string>() { "E", "F", "C", "H" }; 
     StringBagAlgorithm sba = new StringBagAlgorithm(); 

     StringsComputer sc = new StringsComputer() { 
      FirstList = listA, 
      SecoundList = listB, 
      BagAlgorithm = sba 
     }; 

     sc.ComputeSecondList(); 
    } 
} 

最も有名な間違い!クラスはそのタイプの1つでなければならず、多くの場合はList<MyNewClass>を行います。 SRPとインターフェース/抽象について学び、より良いコードのためにそれらを実装しようとする。

関連する問題