15.命令模式(Command Pattern)
using System; namespace ConsoleApplication8
{
class Program
{
/// <summary>
/// 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。
/// 但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。
/// 在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,可以实现二者之间的松耦合[李建忠]。
/// 这就是本文要说的Command模式。
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Document doc = new Document();
doc.Display();
doc.Undo();
doc.Redo();
Console.Read();
} /// <summary>
/// 文档类
/// </summary>
public class Document
{
/// <summary>
/// 显示操作
/// </summary>
public void Display()
{
Console.WriteLine("Display");
} /// <summary>
/// 撤销操作
/// </summary>
public void Undo()
{
Console.WriteLine("Undo");
} /// <summary>
/// 恢复操作
/// </summary>
public void Redo()
{
Console.WriteLine("Redo");
}
} /// <summary>
/// 抽象命令
/// </summary>
public abstract class DocumentCommand
{
public Document _document;
public DocumentCommand(Document doc)
{
this._document = doc;
} /// <summary>
/// 执行
/// </summary>
public abstract void Execute();
} /// <summary>
/// 显示命令
/// </summary>
public class DisplayCommand : DocumentCommand
{
public DisplayCommand(Document doc)
: base(doc)
{ } public override void Execute()
{
_document.Display();
}
} /// <summary>
/// 撤销命令
/// </summary>
public class UndoCommand : DocumentCommand
{
public UndoCommand(Document doc)
: base(doc)
{ } public override void Execute()
{
_document.Undo();
}
} /// <summary>
/// 重做命令
/// </summary>
public class RedoCommand : DocumentCommand
{
public RedoCommand(Document doc)
: base(doc)
{ } public override void Execute()
{
_document.Redo();
}
} /// <summary>
/// Invoker角色
/// </summary> public class DocumentInvoker
{
DocumentCommand _discmd;
DocumentCommand _undcmd;
DocumentCommand _redcmd; public DocumentInvoker(DocumentCommand discmd, DocumentCommand undcmd, DocumentCommand redcmd)
{
this._discmd = discmd;
this._undcmd = undcmd;
this._redcmd = redcmd;
} public void Display()
{
_discmd.Execute();
} public void Undo()
{
_undcmd.Execute();
} public void Redo()
{
_redcmd.Execute();
}
}
}
}
15.命令模式(Command Pattern)的更多相关文章
- 设计模式-15命令模式(Command Pattern)
1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...
- 设计模式 - 命令模式(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 參考: 命名模式(撤销) ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- C#设计模式——命令模式(Command Pattern)
一.概述通常来说,“行为请求者”与“行为实现者”是紧耦合的.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这些情况下,将“行为请求者”与“行为实 ...
- 设计模式(六):控制台中的“命令模式”(Command Pattern)
今天的博客中就来系统的整理一下“命令模式”.说到命令模式,我就想起了控制台(Console)中的命令.无论是Windows操作系统(cmd.exe)还是Linux操作系统(命令行式shell(Comm ...
随机推荐
- 我对自己提的几个关于cocos2dx的几个问题
1.友元函数的定义: 2.运算符重载: 3.内存关机机制: 4.动作侦听: 5.单点触摸: 6.触摸目标判断: 7.事件传递: 8.多点触摸: 9.加速传感器: 10.物理按键交互: 11.绘图API ...
- [POJ2109]Power of Cryptography
[POJ2109]Power of Cryptography 试题描述 Current work in cryptography involves (among other things) large ...
- 机器学习中的Bias(偏差),Error(误差),和Variance(方差)有什么区别和联系?
前几天搜狗的一道笔试题,大意是在随机森林上增加一棵树,variance和bias如何变化呢? 参考知乎上的讨论:https://www.zhihu.com/question/27068705 另外可参 ...
- Vijos 1055 奶牛浴场
Description 求一个不覆盖指定点的最大子矩阵,\(n,m \leqslant 3\times 10^5,S \leqslant 5\times 10^3\) . Sol 没有名字的算法都叫x ...
- EXT Grid celleditor列编辑,动态控制某一单元格只读
listeners : { beforeedit:function(editor, context, eOpts) { if(context.record.data.hasRatio == " ...
- espcms内容页相册调用代码
{%forlist from=$photo key=i%} <li style="position: absolute; width: 600px; left: 0px; top: 0 ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- mount --bind 重启后失效的解决办法
vsftp不支持软链接,可以用mount来支持不同的目录结构 mount --bind /home/www/web/ROOT/img/upload /ftp/private/upload 重启后失效. ...
- ACM/ICPC 之 Bellman Ford练习题(ZOJ1791(POJ1613))
这道题稍复杂一些,需要掌握字符串输入的处理+限制了可以行走的时间. ZOJ1791(POJ1613)-Cave Raider //限制行走时间的最短路 //POJ1613-ZOJ1791 //Time ...
- HTML DOM scale() 方法
语法 scale(sx, sy) 参数 参数 描述 sx, sy 水平和垂直的缩放因子. 描述 scale() 方法为画布的当前变换矩阵添加一个缩放变换.缩放通过独立的水平和垂直缩放因子来完成.例如, ...