
# Week 06 Task 03: Command Pattern in JavaScript
In Week 06 Task 03, I continued learning Design Patterns in JavaScript.
Spots 

In Week 06 Task 03, I continued learning Design Patterns in JavaScript.

For this task, I implemented the Command Pattern by building a simple text editor with support for editing operations and undo functionality.
The main goal was to understand how commands can encapsulate actions as objects and how this makes operations easier to manage, execute, and reverse. I also wrote tests using Node.js's built-in assert module. The Command Pattern turns a request or action into a separate object. Instead of directly calling a method to perform an operation, we create a command object that knows: What action should be performed A simple structure looks like: The TextEditor performs the actual work, while the command objects describe what operation should happen. I used four main components: The receiver is the object that actually performs the operation. In my implementation, TextEditor is the receiver. It manages the text and provides operations such as: The important idea is that the receiver does the actual work. The AppendCommand represents the action of adding text.
It stores the text that needs to be appended and knows how to execute and undo that operation. When execute() is called, the text is added. When undo() is called, the same text is removed. The DeleteCommand represents a delete operation. It removes characters from the editor and stores enough information to restore them. The important part is that the command remembers what was deleted. That information is needed when the command is undone. The invoker is responsible for executing commands and keeping track of command history. The invoker doesn't need to know how each command works. It simply tells the command: This keeps the invoker separate from the actual editing logic. Suppose we want to append "Hello". First, we create the command: Then the invoker executes it: The command is then stored in the history.
the most recent command is removed from history and its undo() method is called. This provides a simple undo system. Command history is important because it allows us to reverse operations in reverse order. This is called Last-In, First-Out (LIFO) behavior. The most recent command is undone first. A simple usage could look like: Now undo the last operation: I used Node.js's built-in assert module to test the Command Pattern. Undoing append operations Undoing delete operations Undoing commands in the correct order Calling undo when there is no command history These tests helped verify that commands were being executed and reversed correctly. The final Task 03 structure was: Each file had a specific responsibility: The Command Pattern is commonly useful when actions need to be stored, queued, logged, or undone. Commands can be executed later by a worker.
The Command Pattern provides several advantages: 1. Separation of Responsibilities The receiver performs the actual work, while commands represent actions. Commands can store the information required to reverse operations. Executed commands can be stored and managed. New commands can be added without heavily modifying existing code. Commands can be stored and executed later. This task helped me understand how an operation can be represented as an object. The most important idea I learned is: Instead of directly modifying the editor, I can create commands that describe what should happen. I also learned how command history can be used to implement undo functionality. The main concepts I learned from this task are:
The Command Pattern is especially useful when operations need to be undoable, queued, logged, or controlled independently. Week 06 Task 03 gave me practical experience with the Command Pattern in JavaScript.
I built a simple text editor where editing operations were represented as command objects. I also implemented command history and undo functionality.
This task helped me understand how separating the command, invoker, and receiver can make an application more modular and easier to extend.
Overall, Week 06 helped me move beyond simply writing JavaScript code and understand how design patterns can be used to structure real applications. For further actions, you may consider blocking this person and/or reporting abuse
In Week 06 Task 03, I continued learning Design Patterns in JavaScript.
