乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern)
作者:webabcd
介绍
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。
示例
有一个Message实体类,某个类对它的操作有Insert()和Delete()方法。现在要求可以对之前的所有操作做撤销和重复。

MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command

{
/**//// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/**//// <summary>
/// Message内容
/// </summary>
public string Message
{
get
{ return _message; }
set
{ _message = value; }
}
private DateTime _publishTime;
/**//// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get
{ return _publishTime; }
set
{ _publishTime = value; }
}
}
}Action
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command

{
/**//// <summary>
/// enum
/// 定义操作的两种方法Insert和Delete
/// </summary>
public enum Action
{
/**//// <summary>
/// Insert
/// </summary>
Insert,

/**//// <summary>
/// Delete
/// </summary>
Delete
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command

{
/**//// <summary>
/// 接收者(Receiver)角色
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/**//// <summary>
/// 操作
/// </summary>
/// <param name="action">操作的方法</param>
/// <param name="mm">Message实体对象</param>
public void Operation(Action action, MessageModel mm)
{
switch (action)
{
case Action.Insert :
Insert(mm);
break;
case Action.Delete :
Delete(mm);
break;
}
}

/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
private void Insert(MessageModel mm)
{
// 代码略
}

/**//// <summary>
/// 删除Message
/// </summary>
/// <param name="mm">Message实体对象</param>
private void Delete(MessageModel mm)
{
// 代码略
}
}
}
ICommand
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command

{
/**//// <summary>
/// 命令(Command)角色
/// </summary>
public interface ICommand
{
/**//// <summary>
/// 执行
/// </summary>
/// <returns>操作的方法及操作的信息</returns>
string Execute();

/**//// <summary>
/// 取消执行
/// </summary>
/// <returns>操作的方法及操作的信息</returns>
string UnExecute();
}
}
SqlMessageCommand
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command

{
/**//// <summary>
/// 具体命令(ConcreteCommand)角色
/// </summary>
public class SqlMessageCommand : ICommand
{
/**//// <summary>
/// 操作的方法
/// </summary>
private Action _action;

/**//// <summary>
/// Message实体对象
/// </summary>
private MessageModel _mm;

/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="action">操作的方法</param>
/// <param name="mm">Message实体对象</param>
public SqlMessageCommand(Action action, MessageModel mm)
{
this._action = action;
this._mm = mm;
}

/**//// <summary>
/// 执行
/// </summary>
/// <returns>操作的方法及操作的信息</returns>
public string Execute()
{
new SqlMessage().Operation(_action, _mm);
return _action.ToString() + ":" + _mm.Message;
}

/**//// <summary>
/// 取消执行(调用一个方法,以决定取消执行的算法)
/// </summary>
/// <returns>操作的方法及操作的信息</returns>
public string UnExecute()
{
_action = GetUndoAction(_action);
new SqlMessage().Operation(_action, _mm);
return _action.ToString() + ":" + _mm.Message;
}

/**//// <summary>
/// 获得取消执行的算法
/// </summary>
/// <param name="action">操作的方法</param>
/// <returns></returns>
private Action GetUndoAction(Action action)
{
Action undo;
switch (action)
{
case Action.Insert :
undo = Action.Delete;
break;
case Action.Delete :
undo = Action.Insert;
break;
// 这句没啥用
default :
undo = Action.Insert;
break;
}
return undo;
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command

{
/**//// <summary>
/// 请求者(Invoker)角色
/// </summary>
public class Message
{
/**//// <summary>
/// 命令集合(保存每次操作)
/// </summary>
private List<ICommand> _listCommand = new List<ICommand>();

/**//// <summary>
/// 命令集合中当前要执行的命令的索引
/// </summary>
;

/**//// <summary>
/// 执行Sql
/// </summary>
/// <param name="action">操作的方法</param>
/// <param name="mm">Message实体对象</param>
/// <returns>操作的方法及操作的信息</returns>
public string Do(Action action, MessageModel mm)
{
string rtn = "";
ICommand cmd = new SqlMessageCommand(action, mm);
rtn = cmd.Execute();
_listCommand.Add(cmd);
current++;
return rtn;
}

/**//// <summary>
/// 撤销
/// </summary>
/// <param name="levels">执行撤销操作的次数</param>
/// <returns>操作的方法及操作的信息(用空格分开多条记录)</returns>
public string Undo(int levels)
{
string rtn = "";
; i < levels; i++)
{
)
{
ICommand cmd = _listCommand[--current];
rtn += cmd.UnExecute() + " ";
}
}
return rtn;
}

/**//// <summary>
/// 重复
/// </summary>
/// <param name="levels">执行重复操作的次数</param>
/// <returns>操作的方法及操作的信息(用空格分开多条记录)</returns>
public string Redo(int levels)
{
string rtn = "";
; i < levels; i++)
{
)
{
ICommand cmd = _listCommand[current++];
rtn += cmd.UnExecute() + " ";
}
}
return rtn;
}
}
}
client
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.Command;
public partial class Command : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message();
Response.Write("操作");
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第1条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第2条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第3条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第4条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Delete, new MessageModel("第2条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第5条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Delete, new MessageModel("第3条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第6条", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Do(Action.Insert, new MessageModel("第7条", DateTime.Now)));
Response.Write("<br />");
Response.Write("<br />");
Response.Write("撤销4次操作");
Response.Write("<br />");
Response.Write(m.Undo());
Response.Write("<br />");
Response.Write("<br />");
Response.Write("重复2次操作");
Response.Write("<br />");
Response.Write(m.Redo());
Response.Write("<br />");
Response.Write("<br />");
Response.Write("撤销3次操作");
Response.Write("<br />");
Response.Write(m.Undo());
}
}
运行结果
操作
Insert:第1条
Insert:第2条
Insert:第3条
Insert:第4条
Delete:第2条
Insert:第5条
Delete:第3条
Insert:第6条
Insert:第7条
撤销4次操作
Delete:第7条 Delete:第6条 Insert:第3条 Delete:第5条
重复2次操作
Insert:第5条 Delete:第3条
撤销3次操作
Insert:第3条 Delete:第5条 Insert:第2条
参考
http://www.dofactory.com/Patterns/PatternCommand.aspx
OK
[源码下载]
乐在其中设计模式(C#) - 命令模式(Command Pattern)的更多相关文章
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 设计模式-15命令模式(Command Pattern)
1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...
- 【UE4 设计模式】命令模式 Command Pattern
概述 描述 将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化:对请求排队或者记录请求日志,以及支持可撤销的操作. 命令模式是一种对象行为型模式,其别名为动作(Action)模式或事务 ...
- 设计模式 - 命令模式(command pattern) 多命令 具体解释
命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...
- 设计模式 - 命令模式(command pattern) 具体解释
命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...
- 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释
命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...
- 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需 ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...
随机推荐
- IE与FF脚本兼容性问题
(1) window.event: 表示当前的事件对象,IE有这个对象,FF没有,FF通过给事件处理函数传递事件对象 (2) 获取事件源 IE用srcElement获取事件源,而FF用target获取 ...
- Java生成目录
Java生成目录 1.说明 推断目录是否存在,假设不存在就创建该目录.并打印其路径.假设存在,打印其路径 2.实现源代码 /** * @Title:BuildFolder.java * @Packag ...
- hdu3622(二分+two-sat)
传送门:Bomb Game 题意:给n对炸弹可以放置的位置(每个位置为一个二维平面上的点),每次放置炸弹是时只能选择这一对中的其中一个点,每个炸弹爆炸的范围半径都一样,控制爆炸的半径使得所有的爆炸范围 ...
- iOS App 性能优化总结
今天简单总结一些clientapp 优化的方案和方向. 我相信开发一个app大部分团队都能够完毕,可是性能久不一样啦,和我们都写一个冒泡算法一样,我相信每一个人写的冒泡算法都不一样,这些区别就带来了性 ...
- JavaWeb 项目中的绝对路径和相对路径以及问题的解决方式
近期在做JavaWeb项目,总是出现各种的路径错误,并且发现不同情况下 / 所代表的含义不同,导致在调试路径上浪费了大量时间. 在JavaWeb项目中尽量使用绝对路径 由于使用绝对路径是绝对不会出 ...
- IT谁谁说女子不如男行业
(联合创始人拉里·佩奇(Larry Page)和Sergey Brin(Sergey Brin)曾经说过:"促进性别平衡对工作的顺利开展.该公司可以有很强的女性团队至关重要,尤其是技术的妇女 ...
- Fashion Meets Finance聚会来袭-7月19日 北京
http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NjEzMjMyMQ%3D%3D&appmsgid=10000704&itemidx= ...
- for循环中一个不容小觑的问题
for(int i=1;i<=100;i++) 作为程序猿,我们很喜欢使用这种for循环. 可是,当中隐含着一个重要的问题. 过多的编程经历可能使我们的思维产生了一些误解,在上面的for循环中, ...
- bat文件无法双击运行
问题: win7系统下新建txt文件,编辑脚本内容后,保存为test.bat.每次双击它,只会默认以txt格式打开它,而不是运行它. 解决: 1. 双击打开“我的电脑”,然后在“工具”下选择“文件夹选 ...
- HTML 5最终确定,八年后,我们再谈谈如何改变世界
从原:http://www.36kr.com/p/216655.html 我们第一次谈论HTML5要改变世界大概是由于乔布斯,他坚持在iOS上不兼容Flash,在Adobe统治多媒体开发的那个年代.这 ...