Welcome

首页 / 软件开发 / .NET编程技术 / .Net设计模式实例之命令模式(Command Pattern)

.Net设计模式实例之命令模式(Command Pattern)2011-05-06 博客园 灵动生活一、命令模式简介(Brief Introduction)

命令模式(Command Pattern)将请求封装为一个对象,从而使你用不同的请求对客户进 行参数化,对请求排队或纪录请求日志,以及支持可撤销的操作。

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations

二、解决的问题(What To Solve)

当需要有撤销或者恢复操作时,可以考虑使用命令模式。

三、命令模式分析(Analysis)1、命令模式结构

Command抽象类:声明一个接口,用于执行操作,declares an interface for executing an operation。

ConcreteCommand实现类:将一个接收者对象绑定到一个动作。调用接收者相应的操作, 以实现Execute 。

defines a binding between a Receiver object and an action

implements Execute by invoking the corresponding operation(s) on Receiver。

Receiver类:知道如何执行一个请求相关的操作。knows how to perform the operations associated with carrying out the request.

Invoker类:要求命令执行一个请求。asks the command to carry out the request 。