实验名称 命令模式的应用 日期 2019年 04 月9 日
一、实验目的:
1) 掌握命令模式(Commond)的特点
2) 分析具体问题,使用命令模式进行设计。
开发人员为公司内部OA系统开发了一个桌面版应用程序,该应用程序为用户提供了一系列自定义功能键,用户可以通过这些功能键来实现一些快捷操作。为了让用户能够灵活地进行功能键的设置,开发人员提供了一个“功能键设置”窗口,该窗口界面所示:
通过如图所示界面,用户可以将功能键和相应功能绑定在一起,还可以根据需要来修改功能键的设置,而且系统在未来可能还会增加一些新的功能或功能键。
为了降低功能键与功能处理类之间的耦合度,让用户可以自定义每一个功能键的功能,软件公司开发人员使用命令模式来设计“自定义功能键”模块,其核心结构如图4所示:
用JAVA语言实现 (或C#控制台应用程序实现)。绘制该模式的UML图。
二、实验环境:
三、实验内容:
【模式UML图】
【模式代码(JAVA语言实现)】
package 实验11;
public class Client {
public static void main(String[] args) {
FBSettingWindow fbsw = new FBSettingWindow("功能按键设置");
FunctionButton fb1,fb2;
fb1 = new FunctionButton("功能键1");
fb2 = new FunctionButton("功能键2");
Command command1,command2;
command1 = (Command) XMLUtil.getBean(0);
command2 = (Command) XMLUtil.getBean(1);
fb1.SetCommand(command1);
fb2.SetCommand(command2);
fbsw.addFunctionButton(fb1);
fbsw.addFunctionButton(fb2);
fbsw.display();
fb1.onClick();
fb2.onClick();
}
}
package 实验11;
public class Command {
public void execute(){
}
}
实验11.HelpCommand
实验11.MinimizeCommand
package 实验11;
import java.util.ArrayList;
public class FBSettingWindow {
private String Title;
private ArrayList functionButtons = new ArrayList<>();
public FBSettingWindow(String title){
this.Title = title;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public void addFunctionButton(FunctionButton fb){
functionButtons.add(fb);
}
public void removeFunctionButton(FunctionButton fb){
functionButtons.remove(fb);
}
public void display(){
System.out.println("显示窗口:" + this.Title);
System.out.println("显示功能按键:");
for(FunctionButton fb : functionButtons){
System.out.println(fb.getName());
}
}
}
package 实验11;
public class FunctionButton {
private String name;
private Command command;
public String getName() {
return name;
}
public void setName(String Name) {
name = Name;
}
public FunctionButton(String Name){
this.name = Name;
}
public void SetCommand(Command command){
this.command = command;
}
public void onClick(){
System.out.println("点击功能键:");
if(command != null){
command.execute();
}
}
}
package 实验11;
public class HelpCommand extends Command{
private HelpHandler hhObj;
public HelpCommand(){
hhObj = new HelpHandler();
}
@Override
public void execute(){
if(hhObj != null){
hhObj.display();
}
}
}
package 实验11;
public class HelpHandler {
public void display(){
System.out.println("使用帮助文档");
}
}
package 实验11;
public class MinimizeCommand extends Command{
private WindowHandler whObj;
public MinimizeCommand(){
whObj = new WindowHandler();
}
@Override
public void execute(){
if(whObj != null){
whObj.minimize();
}
}
}
package 实验11;
public class WindowHandler {
public void minimize(){
System.out.println("最小化窗口");
}
}
package 实验11;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XMLUtil {
public static Object getBean(int i){
try {
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("D:\\软件集合\\IntelliJ IDEA Community Edition 2018.3.2\\JAVA\\JAVA\\src\\实验11\\Config.xml"));
NodeList nl = ((org.w3c.dom.Document) doc).getElementsByTagName("className");
Node classNode = null;
if(i == 0){
classNode = nl.item(0).getFirstChild();
}
else {
classNode = nl.item(1).getFirstChild();
}
String cName = ((Node) classNode).getNodeValue();
Class c = Class.forName(cName);
Object obj = c.newInstance();
return obj;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
【运行截图】
四、心得体会:
通过本次实验,学会了使用命令模式。命令模式的适用性如下:
认为是命令的地方都可以使用命令模式,比如: 1、GUI 中每一个按钮都是一条命令。 2、模拟 CMD