请求发送者与接收者解耦—命令模式(二)
3 完整解决方案
图4 自定义功能键核心结构图
[java] view plaincopy
-
import
java.util.*; -
-
//功能键设置窗口类
-
class
FBSettingWindow { -
private String //窗口标题title; -
//定义一个ArrayList来存储所有功能键 -
private ArrayList newfunctionButtons = ArrayList(); -
-
public FBSettingWindow(String title) { -
this.title = title; -
} -
-
public void setTitle(String title) { -
this.title = title; -
} -
-
public String getTitle() { -
return this.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 (Object obj : functionButtons) { -
System.out.println(((FunctionButton)obj).getName()); -
} -
System.out.println("------------------------------"); -
} - }
-
-
//功能键类:请求发送者
-
class
FunctionButton { -
private String //功能键名称name; -
private Command //维持一个抽象命令对象的引用command; -
-
public FunctionButton(String name) { -
this.name = name; -
} -
-
public String getName() { -
return this.name; -
} -
-
//为功能键注入命令 -
public void setCommand(Command command) { -
this.command = command; -
} -
-
//发送请求的方法 -
public void onClick() { -
System.out.print("点击功能键:"); -
command.execute(); -
} - }
-
-
//抽象命令类
-
abstract
class Command { -
public abstract void execute(); - }
-
-
//帮助命令类:具体命令类
-
class
HelpCommand extendsCommand { -
private HelpHandler //维持对请求接收者的引用hhObj; -
-
public HelpCommand() { -
hhObj = new HelpHandler(); -
} -
-
//命令执行方法,将调用请求接收者的业务方法 -
public void execute() { -
hhObj.display(); -
} - }
-
-
//最小化命令类:具体命令类
-
class
MinimizeCommand extendsCommand { -
private WindowHanlder //维持对请求接收者的引用whObj; -
-
public MinimizeCommand() { -
whObj = new WindowHanlder(); -
} -
-
//命令执行方法,将调用请求接收者的业务方法
-
public void execute() { -
whObj.minimize(); -
} - }
-
-
//窗口处理类:请求接收者
-
class
WindowHanlder { -
public void minimize() { -
System.out.println("将窗口最小化至托盘!"); -
} - }
-
-
//帮助文档处理类:请求接收者
-
class
HelpHandler { -
public void display() { -
System.out.println("显示帮助文档!"); -
} - }
[java] view plaincopy
-
import
javax.xml.parsers.*; -
import
org.w3c.dom.*; -
import
org.xml.sax.SAXException; -
import
java.io.*; -
-
public
class XMLUtil { -
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象,可以通过参数的不同返回不同类名节点所对应的实例
-
public static Object intgetBean( i) { -
try { -
//创建文档对象 -
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); -
DocumentBuilder builder = dFactory.newDocumentBuilder(); -
Document doc; -
doc = builder.parse(new File( "config.xml")); -
-
//获取包含类名的文本节点 -
NodeList nl = doc.getElementsByTagName_r("className"); -
Node classNode = null; -
if ( 0== i) { -
classNode = nl.item(0).getFirstChild(); -
} -
else { -
classNode = nl.item(1).getFirstChild(); -
} -
-
String cName = classNode.getNodeValue(); -
-
//通过类名生成实例对象并将其返回 -
Class c = Class.forName(cName); -
Object obj = c.newInstance(); -
return obj; -
} -
catch(Exception e){ -
e.printStackTrace(); -
return null; -
} -
} - }
[java] view plaincopy