Code Example |
1 // Command 2![](/Images/OutliningIndicators/None.gif) 3 // Intent: "Encapsulate a request as an object, thereby letting you 4 // parameterize clients with different requests, queue or log 5 // requests, and support undoable operations". 6![](/Images/OutliningIndicators/None.gif) 7 // For further information, read "Design Patterns", p233, Gamma et al., 8 // Addison-Wesley, ISBN:0-201-63361-2 9![](/Images/OutliningIndicators/None.gif) 10![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Notes: 11 * Commands are at the heart of a modern GUI app. 12 * They must be nameable, undoable, recordable, 13 * configurable, executable and repeatable. 14 * 15 * In addition to the command here, a command type could also be useful. 16 * It could store the name of a command, information about its icon, etc. 17 */ 18 19 namespace Command_DesignPattern 20![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) { 21 using System; 22![](/Images/OutliningIndicators/InBlock.gif) 23 abstract class Command 24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 25 abstract public void Execute(); 26 protected Receiver r; 27 public Receiver R 28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 29 set 30![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 31 r = value; 32 } 33 } 34 } 35![](/Images/OutliningIndicators/InBlock.gif) 36 class ConcreteCommand : Command 37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 38 override public void Execute() 39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 40 Console.WriteLine("Command executed"); 41 r.InformAboutCommand(); 42 } 43 } 44![](/Images/OutliningIndicators/InBlock.gif) 45 class Receiver 46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 47 public void InformAboutCommand() 48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 49 Console.WriteLine("Receiver informed about command"); 50 } 51 52 } 53![](/Images/OutliningIndicators/InBlock.gif) 54 class Invoker 55![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 56 private Command command; 57 public void StoreCommand(Command c) 58![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 59 command = c; 60 } 61 public void ExecuteCommand() 62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 63 command.Execute(); 64 } 65 } 66![](/Images/OutliningIndicators/InBlock.gif) 67![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 68 /// Summary description for Client. 69 /// </summary> 70 public class Client 71![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 72 public static int Main(string[] args) 73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 74 // Set up everything 75 Command c = new ConcreteCommand(); 76 Receiver r = new Receiver(); 77 c.R = r; 78 Invoker i = new Invoker(); 79 i.StoreCommand(c); 80![](/Images/OutliningIndicators/InBlock.gif) 81 // now let application run 82![](/Images/OutliningIndicators/InBlock.gif) 83 // the invoker is how the command is exposed for the end-user 84 // (or a client) initiates the command, 85 // (e.g. toolbar button, menu item) 86![](/Images/OutliningIndicators/InBlock.gif) 87 i.ExecuteCommand(); 88![](/Images/OutliningIndicators/InBlock.gif) 89 return 0; 90 } 91 } 92 } 93![](/Images/OutliningIndicators/None.gif) 94![](/Images/OutliningIndicators/None.gif) |