Sitemap
JavaScript-World

JavaScript-World

Elevating web experiences with 10+ yrs in JS🚀.Crafting seamless UIs,optimizing performance, mentoring next-gen coders.Passionate about learning&innovation.🌐✨

JavaScript Command Design Pattern

2 min readJun 4, 2023

--

Press enter or click to view image in full size
Photo by Mohammad Rahmani on Unsplash

The Command Pattern is a design pattern that encapsulates actions or operations as objects, enabling you to store, execute, and manage them efficiently. This pattern is particularly useful in JavaScript development, as it promotes the separation of concerns and improves code maintainability.

In this blog post, we will explore the Command Pattern in detail, along with examples to demonstrate its implementation in JavaScript.

What is the Command Pattern?

The Command Pattern involves creating objects that represent actions or operations, allowing you to separate the execution of these actions from their implementation. This pattern promotes the decoupling of the object that invokes the action from the object that performs it.

Implementing the Command Pattern in JavaScript

Let’s dive into a simple example to understand how the Command Pattern can be implemented in JavaScript.

class Calculator {
constructor() {
this.value = 0;
}
  execute(command) {
this.value = command.execute(this.value);
}
}
class AddCommand {
constructor(amount) {
this.amount = amount;
}
execute(currentValue) {
return currentValue + this.amount;
}
}
class SubtractCommand {
constructor(amount) {
this.amount = amount;
}
execute(currentValue) {
return currentValue - this.amount;
}
}
const calculator = new Calculator();const add5 = new AddCommand(5);
const subtract2 = new SubtractCommand(2);
calculator.execute(add5); // Value: 0 + 5 = 5
calculator.execute(subtract2); // Value: 5 - 2 = 3
console.log(calculator.value); // Output: 3

In the example above, we define a Calculator class that maintains a value property and provides an execute method to execute commands. We then create two command classes, AddCommand and SubtractCommand, each with their own execute method that performs the corresponding operation.

Get JavaScript-World’s stories in your inbox

Join Medium for free to get updates from this writer.

By using the Command Pattern, we can easily extend the Calculator class's functionality without modifying its implementation, adhering to the Open/Closed Principle.

Conclusion

The Command Pattern is a powerful design pattern that allows you to encapsulate actions or operations as objects, promoting separation of concerns and improving code maintainability. By implementing this pattern in your JavaScript projects, you can create flexible and scalable code that is easy to manage and extend.

If you enjoyed the article and would like to show your support, be sure to:

👏 Applaud for the story (50 claps) to help this article get featured

👉Follow me on Medium

Check out more content on my Medium profile

--

--

JavaScript-World
JavaScript-World