?? 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 + -