哪种面向对象的 GUI 设计能够正确地结合命令模式和观察者模式?
是否有一种面向对象的 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
}
}
,目前我不知道使用命令模式有效且高效地处理 GUI 事件的正确方法。观察者模式是一个有用的伙伴吗?
Is there an object-oriented GUI design that would be a reasonable combination of the Command and Observer patterns?
My Java GUI attempts to combine Command and Observer patterns as follows:
- the client is an observer of the Command receiver (e.g. an input GUI screen, or a dialog)
- the invoker is an instance variable of the client
- the update() method of the client receives the Command receiver's input and updates the invoker with the appropriate Command
What is making me uncomfortable with this implementation is that the update() method comprises a huge number of conditional if statements.
e.g.
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
}
}
The proper way of using the Command pattern to effectively and efficiently handle GUI events is eluding me at the moment. Is the Observer pattern a useful partner for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,UI 的整体设计解决方案将是 MVC,或者它的一些变体(实际上,人们通常会省略显式控制器,并拥有一个基本上与控制器耦合的模型和视图)。除非您想建立一个撤消框架,否则命令在那里并不是必需的。
无论如何,如果您使用 Java,您很可能会被 UI 库(如 Swing 或 SWT)所困扰,而这个库会给您的设计带来很大的影响。
Well, the overall design solution for UI would be MVC, or some of it's variations (actually, people usually leave out an explicit controller, and have a model and a view which is basically coupled with controller). Commands are not essential there, unless you want to establish an undo framework.
In any case, if you use Java, you are likely to be stuck with a UI library (like Swing or SWT), and this library will impose a great deal of your design.