您的位置:首页 > 其它

设计模式学习总结7 - 行为型2 - Command命令模式

2010-03-04 16:14 781 查看
Command命令模式(行为型)

作用:

命令模式分离客户端的操作请求和请求的执行者。这种模式非常有用,它支持:
1、发送请求给多个接受着
2、排队、日志、拒绝请求
3、原始、简单的操作组合成高层事务
4、重做、撤销功能
Role
The Command pattern creates distance between the client that requests an operation and the object that can perform it. This pattern is particularly versatile. It cansupport:
• Sending requests to different receivers
• Queuing, logging, and rejecting requests
• Composing higher-level transactions from primitive operations
• Redo and Undo functionality

设计:

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Command
{

class Program
{
delegate void Invoker();
static Invoker Execute, Undo, Redo;

class Command
{
public Command(Receiver receiver)
{
Execute = receiver.Action;
Redo = receiver.Action;
Undo = receiver.Reverse;
}
}

public class Receiver
{
string build, oldbuild;
string s = "some string ";
public void Action()
{
oldbuild = build;
build += s;
Console.WriteLine("Receiver is adding " + build);
}

public void Reverse()
{
build = oldbuild;
Console.WriteLine("Receiver is reverting to " + build);
}
}

static void Main(string[] args)
{
new Command (new Receiver());
Execute();
Redo();
Undo();
Execute();
Console.ReadLine();
}
}
}

应用场景:

命令可以由不同的方式由不同的接受者执行
简单的操作实现高层命令

在不同的时候指明,排队,执行命令
需要命令支持撤销功能
支持审计、所有命令执行日志记录
Use the Command pattern when…
You have:
• Commands that different receivers can handle in different ways
• A high-level set of commands that are implemented by primitive operations
You want to:
• Specify, queue, and execute commands at different times
• Support an Undo function for commands
• Support auditing and logging of all changes via commands

总结:

Command命令模式是一种行为型模式,它主要解决的问题是:在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。GoF《设计模式》中说道:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
Command模式的几个要点:
1、 Command模式的根本目的在于将“行为请求者”与“行为实现者”解耦,在面向对象语言中,常见的实现手段是“将行为抽象为对象”。
2、 实现ICommand接口的具体命令对象Command 有时候根据需要可能会保存一些额外的状态信息。
3、 Command模式与C#中的Delegate有些类似。但两者定义行为接口的规范有所区别:Command以面向对象中的“接口-实现”类定义行为接口规范,更严格,更符合抽象原则:Delegate以函数签名来定义行为接口规范,更灵活,但抽象能力比较弱

实现Command模式需要考虑的一些问题:
1、 支持取消和重做:为了达到这个目的Command类中要存储额外的状态信息。也就是上图中Command的state属性。
2、 避免取消操作中过程中的错误积累:由于命令重复的执行、取消执行和重执行的过程可能会积累错误,以致一个应用的状态最终偏离初始值。这就有必要在Command中存入更多的信息以保证这些对象可被精确的复原。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: