📄 +
字号:
<html><!-- #BeginTemplate "/Templates/module.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title> 设计模式之Command 命令模式</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../jdon.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="http://www.jdon.com/favicon.ico">
<meta http-equiv="refresh" content="1200">
</head>
<body bgcolor="#014E82" text="#000000">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="129" rowspan="3" align="center" valign="bottom" bgcolor="#FFFFFF">
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td align="center" >
<img src="http://www.jdon.com/images/jdon.gif" width="120" height="60"></td>
</tr>
<tr>
<td height="9"></td>
</tr>
</table>
</td>
<td valign="bottom" >
<table cellpadding="0" cellspacing="0" height="100%" width="100%" >
<tr bgcolor="#CAC9BB">
<td height="70" align="right" valign="bottom" bgcolor="#CAC9BB">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="619" height="60">
<param name="movie" value="../images/jd.swf">
<param name="quality" value="high"><param name="BGCOLOR" value="#CAC9BB">
<embed src="../images/jd.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="619" height="60"></embed>
</object></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<td height="26" bgcolor="#707070" width="222"> </td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/index.html" class="a03">首 页</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/design.htm" class="a03">设计研究</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/product.htm" class="a03">构件产品</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/trainning.htm" class="a03">培训咨询</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jdonframework/" class="a03">Jdon框架</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jive/index.jsp" class="a03">论坛</a></div></td>
</tr></table>
</td>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="11" colspan="7" bgcolor="#FFFFFF" class="bianxian">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="1" bordercolor="#000000">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="2" bgcolor="#000000">
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" valign="top" bgcolor="#FFFFD7"><table width="100%" border="0" cellspacing="0" cellpadding="3" height="200" align="center">
<tr>
<td align="left" valign="top"><!-- #BeginEditable "Menu" --> <!-- #EndEditable --> </td>
</tr>
<tr>
<td align="right">
<p><a href="http://www.jdon.com/trainning/jiaocheng.htm" target="_blank"><font color="#FF9999"><strong>在线教程</strong></font><br>
<font color="#555555"> 原创教程区</font></a></p>
<p><a href="http://www.jdon.com/jdonframework/app.htm" target="_blank"><font color="#FF9999"><strong>Jdon框架</strong></font><br>
<font color="#555555"> 在线运行演示</font></a></p>
<br>
<div id=vgad ></div>
<!-- #BeginEditable "Menu2" --> <!-- #EndEditable -->
</td>
</tr>
</table>
</td>
<td width="1" height="100%" >
<table height="100%" cellpadding="0" cellspacing="0" background="../images/blackpoint.gif" >
<tr><td> <img name="" src="" width="1" height="1" alt="" ></td></tr></table>
</td >
<td valign="top" >
<table width="100%" border="0" cellspacing="13" cellpadding="2">
<tr>
<td class="article"><!-- #BeginEditable "main" -->
<h3 align="center">设计模式之Command</h3>
<p align="center"><a href="http://www.jdon.com/aboutme.htm">板桥里人</a> http://www.jdon.com
2002/4/23/(转载请保留)</p>
<p align="center"><a href="../mybook/index.htm" target="_blank"><strong>模式实战书籍《Java实用系统开发指南》</strong></a></p>
<p>Command模式是最让我疑惑的一个模式,我在阅读了很多代码后,才感觉隐约掌握其大概原理,我认为理解设计模式最主要是掌握起原理构造,这样才对自己实际编程有指导作用.Command模式实际上不是个很具体,规定很多的模式,正是这个灵活性,让人有些confuse.</p>
<p><b>Command定义</b><br>
n 将来自客户端的请求传入一个对象,无需了解这个请求激活的 动作或有关接受这个请求的处理细节。</p>
<p>这是一种两台机器之间通讯联系性质的模式,类似传统过程语
言的 CallBack功能。 </p>
<p><strong>优点:<br>
</strong>解耦了发送者和接受者之间联系。 发送者调用一个操作,接受者接受请求执行相应的动作,因为使用Command模式解耦,发送者无需知道接受者任何接口。</p>
<p>不少Command模式的代码都是针对图形界面的,它实际就是菜单命令,我们在一个下拉菜单选择一个命令时,然后会执行一些动作.</p>
<p>将这些命令封装成在一个类中,然后用户(调用者)再对这个类进行操作,这就是Command模式,换句话说,本来用户(调用者)是直接调用这些命令的,如菜单上打开文档(调用者),就直接指向打开文档的代码,使用Command模式,就是在这两者之间增加一个中间者,将这种直接关系拗断,同时两者之间都隔离,基本没有关系了.</p>
<p>显然这样做的好处是符合封装的特性,降低耦合度,Command是将对行为进行封装的典型模式,Factory是将创建进行封装的模式,<br>
从Command模式,我也发现设计模式一个"通病":好象喜欢将简单的问题复杂化, 喜欢在不同类中增加第三者,当然这样做有利于代码的健壮性
可维护性 还有复用性.</p>
<p><b>如何使用?</b><br>
具体的Command模式代码各式各样,因为如何封装命令,不同系统,有不同的做法.下面事例是将命令封装在一个Collection的List中,任何对象一旦加入List中,实际上装入了一个封闭的黑盒中,对象的特性消失了,只有取出时,才有可能模糊的分辨出:</p>
典型的Command模式需要有一个接口.接口中有一个统一的方法,这就是"将命令/请求封装为对象":<br>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC">public interface Command {<br>
public abstract void execute ( );<br>
}</td>
</tr>
</table>
<br>
具体不同命令/请求代码是实现接口Command,下面有三个具体命令<br>
<table width="80%" border="0" cellspacing="3" cellpadding="3" bgcolor="#CCCCCC">
<tr>
<td>public class Engineer implements Command {<br>
<br>
public void execute( ) {<br>
//do Engineer's command<br>
}<br>
}
<p>public class Programmer implements Command {<br>
<br>
public void execute( ) {<br>
//do programmer's command<br>
}<br>
}</p>
<p>public class Politician implements Command {<br>
<br>
public void execute( ) {<br>
//do Politician's command<br>
}<br>
}</p>
</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -