Command と Observer のパターンを適切に組み合わせたオブジェクト指向の GUI 設計はありますか?
私の Java GUI は、コマンド パターンとオブザーバー パターンを次のように組み合わせようとします。
- クライアントはコマンドレシーバーのオブザーバーです (入力 GUI 画面やダイアログなど)。
- 呼び出し元はクライアントのインスタンス変数です
- クライアントの update() メソッドはコマンド受信者の入力を受け取り、適切なコマンドで呼び出し元を更新します
この実装で不快に感じているのは、update() メソッドが膨大な数の条件付き if ステートメントで構成されていることです。
例えば
public class Client implements Observer {
InputScreen inputScreen;
Invoker director;
InputScreenCommand inputScreenCommand;
public Client {
inputScreen = new InputScreen();
inputScreen.registerObserver(this);
inputScreenCommand = new InputScreenCommand(inputScreen)
director.setCommand(inputScreenCommand);
director.invoke();
}
public void update(String command) {
if (command.equals("Input Screen 2")) {
inputScreen.removeObserver(this);
// generate new receiver/subject
inputScreen2.registerObserver(this);
// generate new Command
director.setCommand(inputScreen2Command);
director.invoke();
}
// and so on through all the permutations of input from receivers
}
}
Command パターンを使用して GUI イベントを効果的かつ効率的に処理する適切な方法は、現時点ではわかりません。オブザーバー パターンはその有用なパートナーですか?