そのため、現在、Preprocessor一連のインスタンス変数マップを生成するクラスと、クラスのインスタンスがプリプロセッサが生成したマップにアクセスできるようにするメソッドServiceを持つクラスがあります。setPreprocessor(Preprocessor x)Service
現時点では、私のServiceクラスは 3 つのメソッドを連続して呼び出す必要があります。簡単にするために、それらexecutePhaseOneを 、executePhaseTwo、および と呼びましょうexecutePhaseThree。これら 3 つのメソッドはそれぞれ、Serviceインスタンス変数をインスタンス化/変更します。そのうちのいくつかは、ServiceインスタンスのPreprocessorオブジェクトへのポインターです。
私のコードは現在この構造を持っています:
Preprocessor preprocessor = new Preprocessor();
preprocessor.preprocess();
Service service = new Service();
service.setPreprocessor(preprocessor);
service.executePhaseOne();
service.executePhaseTwo();
service.executePhaseThree();
コードをより適切に整理するために、各executePhaseXXX()呼び出しを の個別のサブクラスに配置Serviceし、すべてのフェーズに共通のデータ構造を親クラス に残しServiceます。次に、 3 つのフェーズすべてを連続して実行するexecute()メソッドをService親クラスに作成します。
class ServiceChildOne extends Service {
public void executePhaseOne() {
// Do stuff
}
}
class ServiceChildTwo extends Service {
public void executePhaseTwo() {
// Do stuff
}
}
class ServiceChildThree extends Service {
public void executePhaseThree() {
// Do stuff
}
}
編集:
問題は、親クラスにexecute()メソッドをどのように記述するかです。Service私は持っている:
public void execute() {
ServiceChildOne childOne = new ServiceChildOne();
ServiceChildTwo childTwo = new ServiceChildTwo();
ServiceChildThree childThree = new ServiceChildThree();
System.out.println(childOne.preprocessor); // prints null
childOne.executePhaseOne();
childOne.executePhaseTwo();
childOne.executePhaseThree();
}
ただし、、、childOneおよびchildTwoオブジェクトchildThreeはpreprocessor、親クラスに存在するインスタンス変数にアクセスできませんService...どうすればこの問題を乗り越えることができますか?