|
| 1 | +# Command |
| 2 | + |
| 3 | +An object to instruct a specific action with all required information. |
| 4 | + |
| 5 | +## Motivation |
| 6 | + |
| 7 | +Actions and assignments can't be serialized generally, a Command pattern solves by: |
| 8 | + |
| 9 | +- provides a way to store information of actions. |
| 10 | +- can undo base on the kept command information. |
| 11 | + |
| 12 | +It's heavily used by cli implementations and GUI development. |
| 13 | + |
| 14 | +## ICommand |
| 15 | + |
| 16 | +```cs |
| 17 | +var editor = new Editor(content: "hello"); |
| 18 | +// wrap command info inside a instance |
| 19 | +var commandInsert = new EditorCommand(editor, EditorCommandType.Insert, ", world"); // [!code highlight] |
| 20 | +var commandDelete = new EditorCommand(editor, EditorCommandType.Delete, 7); // [!code highlight] |
| 21 | +
|
| 22 | +commandInsert.Execute(); // [!code highlight] |
| 23 | +commandDelete.Execute(); // [!code highlight] |
| 24 | +
|
| 25 | +public interface ICommand |
| 26 | +{ |
| 27 | + void Execute(); |
| 28 | +} |
| 29 | + |
| 30 | +public record class EditorCommand(Editor Editor, EditorCommandType CommandType, params object[]? Args) : ICommand |
| 31 | +{ |
| 32 | + public void Execute() |
| 33 | + { |
| 34 | + switch (CommandType) |
| 35 | + { |
| 36 | + case EditorCommandType.Insert: |
| 37 | + Editor.Insert((string)(Args?[0] ?? string.Empty)); |
| 38 | + break; |
| 39 | + case EditorCommandType.Delete: |
| 40 | + Editor.Delete((int)(Args?[0] ?? 0)); |
| 41 | + break; |
| 42 | + default: |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public enum EditorCommandType |
| 48 | + { |
| 49 | + Insert, Delete |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +public class Editor |
| 54 | +{ |
| 55 | + private string _content = string.Empty; |
| 56 | + |
| 57 | + public Editor(string content) => _content = content; |
| 58 | + |
| 59 | + public void Insert(string content) |
| 60 | + { |
| 61 | + _content += content; |
| 62 | + Console.WriteLine(_content); |
| 63 | + } |
| 64 | + public void Delete(int count) |
| 65 | + { |
| 66 | + _content = _content[..(_content.Length - count)]; |
| 67 | + Console.WriteLine(_content); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Undo a Command |
| 73 | + |
| 74 | +```cs |
| 75 | +var editor = new Editor(content: "hello"); |
| 76 | +var commandInsert = new EditorCommand(editor, EditorCommandType.Insert, ", world"); |
| 77 | +var commandDelete = new EditorCommand(editor, EditorCommandType.Delete, 7); |
| 78 | + |
| 79 | +commandInsert.Execute(); |
| 80 | +commandDelete.Execute(); |
| 81 | + |
| 82 | +commandDelete.Undo(); // [!code ++] |
| 83 | +commandInsert.Undo(); // [!code ++] |
| 84 | +
|
| 85 | +public interface ICommand |
| 86 | +{ |
| 87 | + void Execute(); |
| 88 | + void Undo(); // [!code ++] |
| 89 | +} |
| 90 | + |
| 91 | +public enum EditorCommandType |
| 92 | +{ |
| 93 | + Insert, Delete |
| 94 | +} |
| 95 | + |
| 96 | +public record class EditorCommand(Editor Editor, EditorCommandType CommandType, params object[]? Args) : ICommand |
| 97 | +{ |
| 98 | + public int InsertLenght { get; init; } = Args?[0] is string s ? s.Length : 0; // [!code ++] |
| 99 | +
|
| 100 | + private bool succeed; // implying last execute succeed or not // [!code ++] |
| 101 | +
|
| 102 | + public void Execute() |
| 103 | + { |
| 104 | + switch (CommandType) |
| 105 | + { |
| 106 | + case EditorCommandType.Insert: |
| 107 | + Editor.Insert((string)(Args?[0] ?? string.Empty)); |
| 108 | + succeed = true; |
| 109 | + break; |
| 110 | + case EditorCommandType.Delete: |
| 111 | + succeed = Editor.Delete((int)(Args?[0] ?? 0)); |
| 112 | + break; |
| 113 | + default: |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void Undo() // [!code ++] |
| 119 | + { // [!code ++] |
| 120 | + if (!succeed) return; // if previous execution failed, no need to undo // [!code ++] |
| 121 | + switch (CommandType) // [!code ++] |
| 122 | + { // [!code ++] |
| 123 | + case EditorCommandType.Insert: // [!code ++] |
| 124 | + Editor.Delete(InsertLenght); // [!code ++] |
| 125 | + break; // [!code ++] |
| 126 | + case EditorCommandType.Delete: // [!code ++] |
| 127 | + Editor.Insert(Editor.Deleted); // [!code ++] |
| 128 | + break; // [!code ++] |
| 129 | + default: // [!code ++] |
| 130 | + break; // [!code ++] |
| 131 | + } // [!code ++] |
| 132 | + } // [!code ++] |
| 133 | +} |
| 134 | + |
| 135 | +public class Editor |
| 136 | +{ |
| 137 | + private string _content = string.Empty; |
| 138 | + |
| 139 | + public string Deleted { get; set; } = string.Empty; // [!code ++] |
| 140 | +
|
| 141 | + public Editor(string content) => _content = content; |
| 142 | + |
| 143 | + public void Insert(string content) |
| 144 | + { |
| 145 | + _content += content; |
| 146 | + Console.WriteLine(_content); |
| 147 | + } |
| 148 | + public bool Delete(int count) // [!code highlight] |
| 149 | + { |
| 150 | + if (_content.Length >= count) // [!code ++] |
| 151 | + { // [!code ++] |
| 152 | + Deleted = _content[^count..]; // [!code ++] |
| 153 | + _content = _content[..(_content.Length - count)]; // [!code ++] |
| 154 | + Console.WriteLine(_content); // [!code ++] |
| 155 | + return true; // [!code ++] |
| 156 | + } // [!code ++] |
| 157 | + return false; // [!code ++] |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
0 commit comments