あるWebアプリケーションの連続する画面間において、
上位画面で入力・取得した値を持ち回りたい場合に、
どの様に値を保持するのがベターなのでしょうか?
今自分で考えているのは以下のパターンで、
②か③の方法で実装しようと思っていますが、
②は上位画面の変更・差し替えには弱い様に思えますし、
③がいいのかな?と漠然と考えています。
①親子共に同じデータ(または必要な分のデータ)を持つ。
データの受渡しはコンストラクタに親画面を渡すか、
アクセッサだけで渡すかのいずれか
Java
1class Parent { 2 int id; 3 String code; 4 String name; 5} 6 7class Child { 8 9 Child () { 10 } 11 12 Child (Parent parent) { 13 this.id = parent.id; 14 this.code = parent.code; 15 this.name = parent.name; 16 } 17 18 int id; 19 String code; 20 String name; 21 22 int getId() { 23 return this.id; 24 } 25 26 String getCode() { 27 return this.code; 28 } 29} 30 31class Grandson { 32 Grandson() { 33 } 34 35 Grandson(Parent parent) { 36 this.id = parent.id; 37 this.code = parent.code; 38 } 39 40 Grandson(Child child) { 41 this.id = child.id; 42 this.code = child.code; 43 } 44 45 int id; 46 String code; 47}
②子孫はその上位クラスのフィールドを持つ場合
データのアクセスは親クラスを通す
Java
1class Parent { 2 int id; 3 String code; 4 String name; 5} 6 7class Child { 8 Child (Parent parent) { 9 this.parent = parent; 10 } 11 12 Parent parent; 13 14 int getId() { 15 return parent.id; 16 } 17 18 String getCode() { 19 return parent.code; 20 } 21} 22 23class Grandson { 24 Grandson (Child child) { 25 this.child = child; 26 } 27 Child child; 28}
③子孫は何かしらの上位クラスのフィールドを持つ
データのアクセスはインターフェースを通す
Java
1interface ParentInformation { 2 int getId(); 3 String getCode(); 4 String getName(); 5} 6 7class Parent implements ParentInformation { 8 int id; 9 String code; 10 String name; 11 12} 13 14class Child implements ParentInformation { 15 Child (ParentInformation parent) { 16 this.parent = parent; 17 } 18 19 ParentInformation parent; 20 21 int getId() { 22 return parent.getId(); 23 } 24 25 String getCode() { 26 return parent.getCode(); 27 } 28} 29 30class Grandson { 31 Grandson (ParentInformation parent) { 32 this.parent = parent; 33 } 34 ParentInformation parent; 35}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/04 05:32