Mid
From PDF
GoF Patterns
Gang of Four Patterns
Usage: ○ The Program class demonstrates how the Command Pattern is used to?
dd text to the document and then undo the action.
- The text is added via the AddTextCommand, and after executing the
command, the document’s content is displayed. Then, the Undo() method is
called, and the content is reverted to its previous state (empty).
class Program
{
static void Main()
{
var document = new Document();
var textEditor = new TextEditor();
var command = new AddTextCommand(document, "Hello, World!");
textEditor.ExecuteCommand(command);
Console.WriteLine(document); // Outputs: Hello, World!
textEditor.Undo();
Console.WriteLine(document); // Outputs: (empty)
}
}Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png