请求发送者与接收者解耦——命令模式(二)

3 完整解决方案

为了降低功能键与功能处理类之间的耦合度,香港服务器,让用户可以自定义每一个功能键的功能,美国服务器,Sunny软件公司开发人员使用命令模式来设计“自定义功能键”模块,其核心结构如图4所示:

图4自定义功能键核心结构图

在图4中,FBSettingWindow是“功能键设置”界面类,FunctionButton充当请求调用者,Command充当抽象命令类,MinimizeCommand和HelpCommand充当具体命令类,香港服务器租用,WindowHanlder和HelpHandler充当请求接收者。完整代码如下所示:

1 import java.util.*; FBSettingWindow { 5private String title; //窗口标题 ArrayList<FunctionButton> functionButtons = new ArrayList<FunctionButton>(); FBSettingWindow(String title) { 10this.title = title; 11 } setTitle(String title) { 14this.title = title; 15 } String getTitle() { .title; 19 } addFunctionButton(FunctionButton fb) { 22 functionButtons.add(fb); 23 } removeFunctionButton(FunctionButton fb) { 26 functionButtons.remove(fb); 27 } display() { 31System.out.println(“显示窗口:” + this.title); 32System.out.println(“显示功能键:”); 33for (Object obj : functionButtons) { 34 System.out.println(((FunctionButton)obj).getName()); 35 } 36System.out.println(“——————————“); 37 } 38 } FunctionButton { Command command; FunctionButton(String name) { 46this.name = name; 47 } String getName() { .name; 51 } setCommand(Command command) { 55this.command = command; 56 } onClick() { 60System.out.print(“点击功能键:”); 61 command.execute(); 62 } 63 } Command { execute(); 68 } HelpCommand extends Command { HelpCommand() { 75hhObj = new HelpHandler(); 76 } execute() { 80 hhObj.display(); 81 } 82 } MinimizeCommand extends Command { MinimizeCommand() { 89whObj = new WindowHanlder(); 90 } execute() { 94 whObj.minimize(); 95 } 96 } WindowHanlder { minimize() { 101System.out.println(“将窗口最小化至托盘!”); 102 } 103 } HelpHandler { display() { 108System.out.println(“显示帮助文档!”); 109 } 110 } 旁观者的姓名永远爬不到比赛的计分板上。

请求发送者与接收者解耦——命令模式(二)

相关文章:

你感兴趣的文章:

标签云: