乐在其中设计模式(C#) - 命令模式(Command Pattern)-阿里云开发者社区

开发者社区> 开发与运维> 正文

乐在其中设计模式(C#) - 命令模式(Command Pattern)

简介:
[索引页]
[源码下载]


乐在其中设计模式(C#) - 命令模式(Command Pattern)


作者:webabcd


介绍
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。


示例
有一个Message实体类,某个类对它的操作有Insert()和Delete()方法。现在要求可以对之前的所有操作做撤销和重复。
 
MessageModel
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Command 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// Message实体类 
InBlock.gif        /// </summary> 
InBlock.gif        public class MessageModel 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="msg">Message内容</param> 
InBlock.gif                /// <param name="pt">Message发布时间</param> 
InBlock.gif                public MessageModel(string msg, DateTime pt) 
InBlock.gif                { 
InBlock.gif                        this._message = msg; 
InBlock.gif                        this._publishTime = pt; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private string _message; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message内容 
InBlock.gif                /// </summary> 
InBlock.gif                public string Message 
InBlock.gif                { 
InBlock.gif                        get { return _message; } 
InBlock.gif                        set { _message = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private DateTime _publishTime; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message发布时间 
InBlock.gif                /// </summary> 
InBlock.gif                public DateTime PublishTime 
InBlock.gif                { 
InBlock.gif                        get { return _publishTime; } 
InBlock.gif                        set { _publishTime = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Action
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Command 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// enum 
InBlock.gif        /// 定义操作的两种方法Insert和Delete 
InBlock.gif        /// </summary> 
InBlock.gif        public enum Action 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// Insert 
InBlock.gif                /// </summary> 
InBlock.gif                Insert, 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// Delete 
InBlock.gif                /// </summary> 
InBlock.gif                Delete 
InBlock.gif        } 
InBlock.gif}
 
SqlMessage
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Command 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 接收者(Receiver)角色 
InBlock.gif        /// Sql方式操作Message 
InBlock.gif        /// </summary> 
InBlock.gif        public class SqlMessage 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 操作 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="action">操作的方法</param> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                public void Operation(Action action, MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        switch (action) 
InBlock.gif                        { 
InBlock.gif                                case Action.Insert :    
InBlock.gif                                        Insert(mm);    
InBlock.gif                                        break
InBlock.gif                                case Action.Delete : 
InBlock.gif                                        Delete(mm); 
InBlock.gif                                        break
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 插入Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                private void Insert(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        // 代码略 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 删除Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                private void Delete(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        // 代码略 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
ICommand
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Command 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 命令(Command)角色 
InBlock.gif        /// </summary> 
InBlock.gif        public interface ICommand 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 执行 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns>操作的方法及操作的信息</returns> 
InBlock.gif                string Execute(); 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 取消执行 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns>操作的方法及操作的信息</returns> 
InBlock.gif                string UnExecute(); 
InBlock.gif        } 
InBlock.gif}
 
SqlMessageCommand
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Command 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 具体命令(ConcreteCommand)角色 
InBlock.gif        /// </summary> 
InBlock.gif        public class SqlMessageCommand : ICommand 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 操作的方法 
InBlock.gif                /// </summary> 
InBlock.gif                private Action _action; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message实体对象 
InBlock.gif                /// </summary> 
InBlock.gif                private MessageModel _mm; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="action">操作的方法</param> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                public SqlMessageCommand(Action action, MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        this._action = action; 
InBlock.gif                        this._mm = mm; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 执行 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns>操作的方法及操作的信息</returns> 
InBlock.gif                public string Execute() 
InBlock.gif                { 
InBlock.gif                        new SqlMessage().Operation(_action, _mm); 
InBlock.gif 
InBlock.gif                        return _action.ToString() + ":" + _mm.Message; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 取消执行(调用一个方法,以决定取消执行的算法) 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns>操作的方法及操作的信息</returns> 
InBlock.gif                public string UnExecute() 
InBlock.gif                { 
InBlock.gif                        _action = GetUndoAction(_action); 
InBlock.gif                        new SqlMessage().Operation(_action, _mm); 
InBlock.gif 
InBlock.gif                        return _action.ToString() + ":" + _mm.Message; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获得取消执行的算法 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="action">操作的方法</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                private Action GetUndoAction(Action action) 
InBlock.gif                { 
InBlock.gif                        Action undo; 
InBlock.gif 
InBlock.gif                        switch (action) 
InBlock.gif                        { 
InBlock.gif                                case Action.Insert :    
InBlock.gif                                        undo = Action.Delete;    
InBlock.gif                                        break
InBlock.gif                                case Action.Delete : 
InBlock.gif                                        undo = Action.Insert; 
InBlock.gif                                        break
InBlock.gif                                // 这句没啥用 
InBlock.gif                                default : 
InBlock.gif                                        undo = Action.Insert; 
InBlock.gif                                        break
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        return undo; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Message
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Command 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 请求者(Invoker)角色 
InBlock.gif        /// </summary> 
InBlock.gif        public class Message 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 命令集合(保存每次操作) 
InBlock.gif                /// </summary> 
InBlock.gif                private List<ICommand> _listCommand = new List<ICommand>(); 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 命令集合中当前要执行的命令的索引 
InBlock.gif                /// </summary> 
InBlock.gif                private int current = 0; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 执行Sql 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="action">操作的方法</param> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                /// <returns>操作的方法及操作的信息</returns> 
InBlock.gif                public string Do(Action action, MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        string rtn = ""; 
InBlock.gif 
InBlock.gif                        ICommand cmd = new SqlMessageCommand(action, mm); 
InBlock.gif                        rtn = cmd.Execute(); 
InBlock.gif 
InBlock.gif                        _listCommand.Add(cmd); 
InBlock.gif                        current++; 
InBlock.gif 
InBlock.gif                        return rtn; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 撤销 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="levels">执行撤销操作的次数</param> 
InBlock.gif                /// <returns>操作的方法及操作的信息(用空格分开多条记录)</returns> 
InBlock.gif                public string Undo(int levels) 
InBlock.gif                { 
InBlock.gif                        string rtn = ""; 
InBlock.gif 
InBlock.gif                        for (int i = 0; i < levels; i++) 
InBlock.gif                        { 
InBlock.gif                                if (current > 0) 
InBlock.gif                                { 
InBlock.gif                                        ICommand cmd = _listCommand[--current]; 
InBlock.gif                                        rtn += cmd.UnExecute() + " "
InBlock.gif                                } 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        return rtn; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 重复 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="levels">执行重复操作的次数</param> 
InBlock.gif                /// <returns>操作的方法及操作的信息(用空格分开多条记录)</returns> 
InBlock.gif                public string Redo(int levels) 
InBlock.gif                { 
InBlock.gif                        string rtn = ""; 
InBlock.gif 
InBlock.gif                        for (int i = 0; i < levels; i++) 
InBlock.gif                        { 
InBlock.gif                                if (current < _listCommand.Count - 1) 
InBlock.gif                                { 
InBlock.gif                                        ICommand cmd = _listCommand[current++]; 
InBlock.gif                                        rtn += cmd.UnExecute() + " "
InBlock.gif                                } 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        return rtn; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
client
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifusing Pattern.Command; 
InBlock.gif 
InBlock.gifpublic partial class Command : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Message m = new Message(); 
InBlock.gif 
InBlock.gif                Response.Write("操作"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第1条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第2条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第3条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第4条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Delete, new MessageModel("第2条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第5条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Delete, new MessageModel("第3条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第6条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Do(Action.Insert, new MessageModel("第7条", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif 
InBlock.gif                Response.Write("撤销4次操作"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Undo(4)); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif 
InBlock.gif                Response.Write("重复2次操作"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Redo(2)); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif 
InBlock.gif                Response.Write("撤销3次操作"); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Undo(3)); 
InBlock.gif        } 
InBlock.gif}
 
 
运行结果
操作
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
[源码下载]




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344545,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章