ImageVerifierCode 换一换
格式:DOCX , 页数:27 ,大小:145.46KB ,
资源ID:9650691      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9650691.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(设计模式总结通过命令模式.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

设计模式总结通过命令模式.docx

1、设计模式总结 通过命令模式注:文档内容基本上来自于网上,并加上自己的理解而成。有的觉得网友总结得非常好,就完全照搬下来,供学习之用。然而,有的摘抄并没有加上原链接和出处,请谅解。通过命令模式,通过在客户端和具体的命令之间添加一层Invoker,剪断了客户端和具体服务提供者之间的耦合,降低了两者之间的耦合度,同时也增加了灵活性,比如我们可以灵活的某一个请求的服务提供者,通过单独的服务提供者Command类,可以很方便的提供redo和undo的功能等等,这些都是命令模式的优势。在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务

2、”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,实现二者之间的松耦合。这就是命令模式(Command Pattern)即命令模式的核心是要解决“行为请求者”和“行为实现”都之间的耦合,以达到灵活多变的效果。目标:客户只需要发命令,而不需要管命令是如何被执行的!Command patternFrom Wikipedia, the free encyclopediaThis article includes alist of references, related reading orexternal links, but

3、its sources remain unclear because it lacksinline citations.Pleaseimprovethis article by introducing more precise citations.(December 2012)Inobject-oriented programming, thecommand patternis abehaviouraldesign patternin whichan object is used to represent andencapsulateall the information needed to

4、call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.Four terms always associated with the command pattern arecommand,receiver,invokerandclient.A concretecommandobject has areceiverobject and invokes a method o

5、f the receiver in a way that is specific to that receivers class(即在concrete command Object里面有 receiver 的成员变量,并且有触发 receiver 动作的方法,这个方法是所有receiver所共有的接口级别的方法). The receiver then does the work.A concrete command object is separately passed to aninvokerobject, which invokes the command, and optionally

6、does bookkeeping about the command execution. Any concrete command object can be passed to the same invoker object(即在Invoker有一个接受Concrete Command的方法,形如setCommand(XXX)). Both an invoker object and several concrete command objects are held by aclientobject(Client创建具体的 Concrete Command). The client con

7、tains the decision making about which commands to execute at which points.To execute a command, it passes the command object to the invoker object.See example code below.Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a

8、 time of their choosing without the need to know the class of the method or the method parameters. Using an invoker object allows bookkeeping about command executions to be conveniently performed, as well as implementing different modes for commands, which are managed by the invoker object, without

9、the need for the client to be aware of the existence of bookkeeping or modes.Contentshide 1Uses 2Structure 3Terminology 4Example 4.1C# 4.2Java 4.3Python 4.4Scala 4.5Javascript 5See also 6References 7External linkseditUsesCommand objects are useful for implementing:GUI buttons and menu itemsInSwingan

10、dBorland Delphiprogramming, anActionis a command object. In addition to the ability to perform the desired command, anActionmay have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only theActionobject.Macror

11、ecordingIf all user actions are represented by command objects, a program can record a sequence of actions simply by keeping a list of the command objects as they are executed. It can then play back the same actions by executing the same command objects again in sequence. If the program embeds a scr

12、ipting engine, each command object can implement atoScript()method, and user actions can then be easily recorded as scripts.Mobile CodeUsing languages such as Java where code can be streamed/slurped from one location to another via URLClassloaders and Codebases the commands can enable new behavior t

13、o be delivered to remote locations (EJB Command, Master Worker)Multi-levelundoIf all user actions in a program are implemented as command objects, the program can keep a stack of the most recently executed commands. When the user wants to undo a command, the program simply pops the most recent comma

14、nd object and executes itsundo()method.NetworkingIt is possible to send whole command objects across the network to be executed on the other machines, for example player actions in computer games.Parallel ProcessingWhere the commands are written as tasks to a shared resource and executed by many thr

15、eads in parallel (possibly on remote machines -this variant is often referred to as the Master/Worker pattern)Progress barsSuppose a program has a sequence of commands that it executes in order. If each command object has agetEstimatedDuration()method, the program can easily estimate the total durat

16、ion. It can show a progress bar that meaningfully reflects how close the program is to completing all the tasks.Thread poolsA typical, general-purpose thread pool class might have a publicaddTask()method that adds a work item to an internal queue of tasks waiting to be done. It maintains a pool of t

17、hreads that execute commands from the queue. The items in the queue are command objects. Typically these objects implement a common interface such asjava.lang.Runnablethat allows the thread pool to execute the command even though the thread pool class itself was written without any knowledge of the

18、specific tasks for which it would be used.TransactionalbehaviorSimilar to undo, a database engine or software installer may keep a list of operations that have been or will be performed. Should one of them fail, all others can be reversed or discarded (usually calledrollback). For example, if two da

19、tabase tables that refer to each other must be updated, and the second update fails, the transaction can be rolled back, so that the first table does not now contain an invalid reference.WizardsOften a wizard presents several pages of configuration for a single action that happens only when the user

20、 clicks the Finish button on the last page. In these cases, a natural way to separate user interface code from application code is to implement the wizard using a command object. The command object is created when the wizard is first displayed. Each wizard page stores its GUI changes in the command

21、object, so the object is populated as the user progresses. Finish simply triggers a call toexecute(). This way, the command class contains no user interface code.editStructure UPDATE: The explanation for the Receiver block above should be The actual work to be done by the command (action)editTermino

22、logyThe terminology used to describe command pattern implementations is not consistent and can therefore be confusing. This is the result ofambiguity, the use ofsynonyms, and implementations that may obscure the original pattern by going well beyond it.1. Ambiguity.2. The termcommandis ambiguous. Fo

23、r example,move up, move upmay refer to a single (move up) command that should be executed twice, or it may refer to two commands, each of which happens to do the same thing (move up). If the former command is added twice to an undo stack, both items on the stack refer to the same command instance. T

24、his may be appropriate when a command can always be undone the same way (e.g. move down). Both theGang of Fourand theJava example belowuse this interpretation of the termcommand. On the other hand, if the latter commands are added to an undo stack, the stack refers to two separate objects. This may

25、be appropriate when each object on the stack must contain information that allows the command to be undone. For example, to undo adelete selectioncommand, the object may contain a copy of the deleted text so that it can be re-inserted, if thedelete selectioncommand must be undone. Note that using a

26、separate object for each invocation of a command is also an example of thechain of responsibility pattern.3. The termexecuteis also ambiguous. It may refer to running the code identified by the command objectsexecutemethod. However, in MicrosoftsWindows Presentation Foundationa command is considered

27、 to have been executed when the commandsexecutemethod has been invoked, but that does not necessarily mean that the application code has run. That occurs only after some further event processing.4. Synonyms andhomonyms.5. Client, Source, Invoker: the button, toolbar button, or menu item clicked, the

28、 shortcut key pressed by the user.6. Command Object, Routed Command Object, Action Object: a singleton object (e.g. there is only one CopyCommand object), which knows about shortcut keys, button images, command text, etc. related to the command. A source/invoker object calls the Command/Action objec

29、ts execute/performAction method. The Command/Action object notifies the appropriate source/invoker objects when the availability of a command/action has changed. This allows buttons and menu items to become inactive (grayed out) when a command/action cannot be executed/performed.7. Receiver, Target

30、Object: the object that is about to be copied, pasted, moved, etc. The receiver object owns the method that is called by the commandsexecutemethod. The receiver is typically also the target object. For example, if the receiver object is acursorand the method is calledmoveUp, then one would expect th

31、at the cursor is the target of the moveUp action. On the other hand, if the code is defined by the command object itself, the target object will be a different object entirely.8. Command Object, routed event args, event object: the object that is passed from the source to the Command/Action object,

32、to the Target object to the code that does the work. Each button click or shortcut key results in a new command/event object. Some implementations add more information to the command/event object as it is being passed from one object (e.g. CopyCommand) to another (e.g. document section). Other implementations put command/event objects in other event objects (like a box inside a bigger box) as they move along the

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1