📄 commandpattern.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>Command 模式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">Design Pattern: Command 模式</a></h1>
如果您写过Java的Swing视窗程式,您可能使用过Command模式了,例如在您按下JMenuItem的“剪下”选项时,执行对JTextArea的选定文字之剪下动作,并将状态列设定为文件已修改状态。<br>
<br>
在设计Swing时,设计人员是不可能知道使用Swing类别的人,在某个事件发生后所要执行的动作是什么的,他们采用了Command模式,以上面的需求作为例子,一个实作的片段可能像是这个样子:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">menuCut.addActionListener( </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> new ActionListener() { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public void actionPerformed(ActionEvent e) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> // textArea 是 JTextArea的一个实例 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> textArea.cut(); </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> });</span><br>
</div>
<br>
上面个这片段采用的是Java的匿名类别(Anonymous
class),一个不具名的类别实作了ActionListener介面,它只有一个方法actionPerformed(),使用
addActionListener()为JMenuItem加入这个类别的实例,一但这个JMenuItem被按下,它就会调用
actionPerformed()方法,以执行您所定义的工作, UML 类别图如下所示:<br>
<div style="text-align: center;"><img style="width: 480px; height: 269px;" alt="Command" title="Command" src="images/command-1.jpg"><br>
</div>
使用Command模式,您可以根据实际的需求来调用执行的物件,至于执行的细节封装在事先定义好的方法(例如actionPerformed()方
法,下面实际撰写个程式作为示范:<br>
<ul>
<li> Invoker.java </li>
</ul>
<pre>import java.util.*;<br><br>public class Invoker {<br> private Map commands;<br> <br> public Invoker() {<br> commands = new HashMap();<br> }<br> <br> public void addCommand(String commName,<br> ICommand command) {<br> commands.put(commName, command);<br> }<br> <br> public void request(String commName) {<br> ICommand command = (ICommand) commands.get(commName);<br> command.execute();<br> }<br>} <br></pre>
<br>
<ul>
<li> ICommand.java </li>
</ul>
<pre>public interface ICommand {<br> public void execute();<br>} <br></pre>
<br>
<ul>
<li> UpperCaseHello.java </li>
</ul>
<pre>public class UpperCaseHello implements ICommand {<br> private String name;<br> <br> public UpperCaseHello(String name) {<br> this.name = name; <br> }<br> <br> public void execute() {<br> System.out.println("HELLO, " + name.toUpperCase());<br> }<br>} <br></pre>
<br>
<ul>
<li> LowerCaseHello.java </li>
</ul>
<pre>public class LowerCaseHello implements ICommand {<br> private String name;<br> <br> public LowerCaseHello(String name) {<br> this.name = name; <br> }<br> <br> public void execute() {<br> System.out.println("hello, " + name.toLowerCase());<br> }<br>} <br></pre>
<br>
Client模拟随机的请求,Invoker事先并不知道Client会发出什么需求,但它总是可以执行Client的请求:<br>
<ul>
<li>Client.java</li>
</ul>
<pre>public class Client {<br> public static void main(String[] args) {<br> Invoker invoker = new Invoker();<br> invoker.addCommand("JUSTIN", <br> new UpperCaseHello("Justin"));<br> invoker.addCommand("momor", <br> new LowerCaseHello("momor"));<br> <br> // simulate random request<br> String[] req = {"JUSTIN", "momor"};<br> while(true) {<br> int i = (int) (Math.random() * 10) % 2;<br> invoker.request(req[i]);<br> }<br> }<br>} <br></pre>
<p></p>
像上面这种将请求封装起来的模式,就是Command模式,它将请求后的实作部份留待使用者实作,Command模式的UML类别图如下所示:<br>
<div style="text-align: center;"><br>
</div>
<div style="text-align: center;"><img style="width: 408px; height: 275px;" alt="Command" title="Command" src="images/command-2.jpg"><br>
<br>
</div>
Command模式是个相当常见的模式,除了先前谈过的Swing视窗程式设计例子之外,现在许多<a href="MVCPattern.htm">Web MVC</a> <a href="FrameWork.htm">Framework</a>
也都采用Command模式来设计Controller,在这样的例子中,Container就好比Command模式中Client的角色,而前端
Controller(例如JSP技术中通常会采用的Dispatcher
Servlet)就相当于Invoker,而Action则相当于ICommand的角色,至于Receiver角色就是您封装在Action中执行的物
件了。<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -