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)

}
}

More from Design Patterns in C#

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details