変更できない既存のフレームワークがあります。2 つのプロパティを読み取ります。
ClassA=somepackage.AnImplementationOfInterfaceAClassB=somepackage.AnImplementationOfInterfaceB
public methodAa new ClassA()、public methodBanew ClassB()をその順序で呼び出す
class Cインターフェイス A、B を実装し、フックを提供するmethodC1を作成methodC2しclass Dてオーバーライドします ( methodA&methodBには多くの定型文が含まれており、実装が複雑です - methodC1&methodC2はビジネス ロジックをカプセル化します)。次に、私のプロパティは
ClassA=somepackage.classDClassB=somepackage.classD
問題は、クラス D を実装する人が次のようなものを書きたくなるかもしれないということです:
class D extends class C
{
private int foo; //must be non-static due to multi-threaded new Class D() calls going on
int methodC1() {this.foo = read number from network}
methodC2(int x) {y = this.foo;} //methodC2 is always called after methodC1
//methodA, methodB implementation inherited from C
}
しかし、フレームワークはclass Dを呼び出す前に毎回の新しいオブジェクトを実際に作成し、「this」参照の使用に依存できないため、これmethodAは期待どおりには機能しません。methodB
as の定義methodC1は、の呼び出しがのオーバーライドされたものではなく、の実装に関連付けられているため、機能しません。methodC2staticmethodC1CD
本当に書かれるべきものは次のとおりです。
class D extends class C
{
int methodC1() {return number from network;}
methodC2(int x) {y = x} //here y is using the return value of methodC1
//methodA, methodB implementation inherited from C
}
また、のみ をオーバーライド可能にしたいmethodC1、methodC2つまり、D で作業しているプログラマーが混乱することはありませんmethodA
理想的なデザインは
- プロパティは 1 つのクラスのみを参照します
methodC1、methodC2そのクラスにいること
課題のまとめ
- いいえ
this、methodC1_methodC2 - 作れない
methodC1、methodC2static - プロパティはインスタンス化可能なクラスのみを取ります
このフレームワークを設計するにはどうすればよいですか? これも解決可能ですか?methodC1、の署名を変更できますmethodC2。