Net设计模式实例之命令模式(Command Pattern)
一、命令模式简介(Brief Introdu ction)
命令模式(Com
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、命令模式结构
Com
ConcreteCom
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 com
2、源代码
1、Com |
abstract class Com { protected Receiver receiver; public Com { this.receiver = receiver; } public abstract void Execute(); } |
2、ConcreteCom |
class ConcreteCom { // Constructor public ConcreteCom :base(receiver) { } public override void Execute() { receiver.Action(); } } |
3、Receiver类:知道如何执行一个请求相关的操作 |
class Receiver { public void Action() { Console.WriteLine("Ca } } |
4、Invoker类:要求命令执行一个请求 |
class Invoker { private Com public void SetCom { this._com } public void ExecuteCom { _com } } |
4、客户端代码 |
static void { // Create receiver, com Receiver receiver = new Receiver(); Com Invoker invoker = new Invoker(); // Set and execute com invoker.SetCom invoker.ExecuteCom Console.ReadKey(); } |
3、程序运行结果
四.案例分析(Example)
1、场景
使用命令模式进行计算器计算,可以是加减乘除等运算,可以进行Undo操作和Rodo操作。如下图所示
Com
CalculatorCom
UnExecute方法:执行Undo操作。
Calculator 类-Operation方法:执行加减乘除操作。
User类:要求命令Calculator执行一个计算请求。
Compute方法:加减乘除等计算操作
Undo方法:撤销操作。Redo方法:重复操作。
2、代码
1、抽象命令类Com |
/// <sum /// The 'Com /// </sum abstract class Com { public abstract void Execute(); public abstract void UnExecute(); } /// <sum /// The 'ConcreteCom /// </sum class CalculatorCom { private char _operator; private int _operand; private Calculator _calculator; // Constructor public CalculatorCom { this._calculator = calculator; this._operator = @operator; this._operand = operand; } // Gets operator public char Operator { set { _operator = value; } } // Get operand public int Operand { set { _operand = value; } } // Execute new com public override void Execute() { _calculator.Operation(_operator, _operand); } // Unexecute last com public override void UnExecute() { _calculator.Operation(Undo(_operator), _operand); } // Returns opposite operator for given operator private char Undo(char @operator) { switch (@operator) { case '+': return '-'; case '-': return '+'; case '*': return '/'; case '/': return '*'; default: throw new ArgumentException("@operator"); } } } |
2、计算器类Calculator |
/// <sum /// The 'Receiver' class /// </sum class Calculator { private int _curr = 0; public void Operation(char @operator, int operand) { switch (@operator) { case '+': _curr += operand; break; case '-': _curr -= operand; break; case '*': _curr *= operand; break; case '/': _curr /= operand; break; } Console.WriteLine( "Current value = {0,3} (fo _curr, @operator, operand); } } |
3、请求类User |
/// <sum /// The 'Invoker' class /// </sum class User { // Initializers private Calculator _calculator = new Calculator(); private List<Com private int _current = 0; public void Redo(int levels) { Console.WriteLine("\n---- Redo {0} levels ", levels); // Perform redo operations for (int i = 0; i < levels; i++) { if (_current < _com { Com com } } } public void Undo(int levels) { Console.WriteLine("\n---- Undo {0} levels ", levels); // Perform undo operations for (int i = 0; i < levels; i++) { if (_current > 0) { Com com } } } public void Compute(char @operator, int operand) { // Create com Com _calculator, @operator, operand); com // Add com _com _current++; } |
4、客户端代码 |
static void { // Create user and let her compute User user = new User(); // User presses calculator buttons user.Compute('+', 100); user.Compute('-', 50); user.Compute('*', 10); user.Compute('/', 2); // Undo 4 com user.Undo(4); // Redo 3 com user.Redo(3); Console.ReadKey(); } |
3、程序运行结果
五、总结(Summa ry)
命令模式(Com
出处: http://www.cnblogs.com/ywqu
如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。