お世話になっています。
クラスの分割、メンバの配置についてご意見を伺いたく投稿します。
・メンバ変数はまとめて宣言すべきか(統合クラス1)
・メンバは部品ごとにわけるべきか(統合クラス2)
・メンバは扱うオブジェクトごとに分けるべきか(統合クラス3)
・子オブジェクトのメンバを親オブジェクトで再定義するのは冗長なので子オブジェクトへの参照を公開すべきか(統合クラス4)
などのさまざまなパターン、自分ならばこうするといったご意見をいただければ幸いです。
どうぞよろしくお願いします。
C#
1 2// 値を保持するクラス 3public class ComponentA 4{ 5 public int intValue; 6 public string stringValue; 7} 8 9// クラスAの値を表示するクラス 10public class ComponentB 11{ 12 readonly ComponentA componentA; 13 14 public ComponentB( ComponentA componentA ) { this.componentA = componentA; } 15 16 public void ShowIntValue() { Console.WriteLine( string.Format( "Parent Int Value is {0}", componentA.intValue ) ); } 17 public void ShowStringValue() { Console.WriteLine( string.Format( "Parent String Value is {0}", componentA.stringValue ) ); } 18} 19 20// クラスAの値を更新するクラス 21public class ComponentC 22{ 23 readonly ComponentA componentA; 24 25 public ComponentC( ComponentA componentA ) { this.componentA = componentA; } 26 27 public void UpdateIntValue() { componentA.intValue = 256; } 28 public void UpdateStringValue() { componentA.stringValue = "text"; } 29} 30 31// 統合クラス パターン1 32public class IntegratedClass1 33{ 34 readonly ComponentA componentA; 35 readonly ComponentB componentB; 36 readonly ComponentC componentC; 37 38 public IntegratedClass1() 39 { 40 componentA = new ComponentA(); 41 componentB = new ComponentB( componentA ); 42 componentC = new ComponentC( componentA ); 43 } 44 45 // ComponentA 46 public int intValue { get { return componentA.intValue; } } 47 public string stringValue { get { return componentA.stringValue; } } 48 49 // ComponentB 50 public void ShowIntValue() { componentB.ShowIntValue(); } 51 public void ShowStringValue() { componentB.ShowStringValue(); } 52 53 // ComponentC 54 public void UpdateIntValue() { componentC.UpdateIntValue(); } 55 public void UpdateStringValue() { componentC.UpdateStringValue(); } 56} 57 58// 統合クラス パターン2 59public class IntegratedClass2 60{ 61 public IntegratedClass2() 62 { 63 componentA = new ComponentA(); 64 componentB = new ComponentB( componentA ); 65 componentC = new ComponentC( componentA ); 66 } 67 68 // ComponentA 69 readonly ComponentA componentA; 70 public int intValue { get { return componentA.intValue; } } 71 public string stringValue { get { return componentA.stringValue; } } 72 73 // ComponentB 74 readonly ComponentB componentB; 75 public void ShowIntValue() { componentB.ShowIntValue(); } 76 public void ShowStringValue() { componentB.ShowStringValue(); } 77 78 // ComponentC 79 readonly ComponentC componentC; 80 public void UpdateIntValue() { componentC.UpdateIntValue(); } 81 public void UpdateStringValue() { componentC.UpdateStringValue(); } 82} 83 84// 統合クラス パターン3 85public class IntegratedClass3 86{ 87 public IntegratedClass3() 88 { 89 componentA = new ComponentA(); 90 componentB = new ComponentB( componentA ); 91 componentC = new ComponentC( componentA ); 92 } 93 94 // IntValue 95 public int intValue { get { return componentA.intValue; } } 96 public void ShowIntValue() { componentB.ShowIntValue(); } 97 public void UpdateIntValue() { componentC.UpdateIntValue(); } 98 99 // StringValue 100 public string stringValue { get { return componentA.stringValue; } } 101 public void ShowStringValue() { componentB.ShowStringValue(); } 102 public void UpdateStringValue() { componentC.UpdateStringValue(); } 103 104 // Components 105 readonly ComponentA componentA; 106 readonly ComponentB componentB; 107 readonly ComponentC componentC; 108} 109 110// 統合クラス パターン4 111public class IntegratedClass4 112{ 113 public IntegratedClass4() 114 { 115 componentA = new ComponentA(); 116 componentB = new ComponentB( componentA ); 117 componentC = new ComponentC( componentA ); 118 } 119 120 // Components 121 public readonly ComponentA componentA; 122 public readonly ComponentB componentB; 123 public readonly ComponentC componentC; 124} 125
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。