{"id":5304,"date":"2019-12-19T16:47:15","date_gmt":"2019-12-19T11:17:15","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5304"},"modified":"2026-11-12T18:19:53","modified_gmt":"2026-11-12T12:49:53","slug":"what-is-design-pattern-and-how-to-implement-them","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/what-is-design-pattern-and-how-to-implement-them\/","title":{"rendered":"What Is the Command Design Pattern in JavaScript? A Practical Implementation Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date:<\/strong> December 19, 2019<br><strong>Last Updated:<\/strong> November 10, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Software applications often contain multiple objects that need to communicate and perform specific operations. As applications grow, managing these interactions can become complicated, resulting in tightly coupled code, maintenance challenges, and reduced scalability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is where <strong>software design patterns<\/strong> become useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Design patterns are reusable solutions to commonly occurring software design problems. They provide structured approaches to organizing code, improving maintainability, and making applications easier to extend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explore the <strong>Command Design Pattern in JavaScript<\/strong>, understand how it works, and implement it using a simple example of switching a light on and off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is a Software Design Pattern?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A software design pattern is a reusable approach to solving a common problem in software development. It is not a complete solution or a piece of code that can simply be copied into every project. Instead, it provides a proven structure that developers can adapt to their requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Design patterns are commonly grouped into three categories:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Creational Patterns:<\/strong> Focus on object creation, such as Singleton, Factory Method, and Builder.<\/li>\n\n\n\n<li><strong>Structural Patterns:<\/strong> Focus on how classes and objects are organized, such as Adapter, Decorator, and Facade.<\/li>\n\n\n\n<li><strong>Behavioral Patterns:<\/strong> Focus on communication and responsibilities between objects, such as Command, Observer, and Strategy.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing the right design pattern can help developers create flexible and maintainable applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is the Command Design Pattern?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Command Design Pattern<\/strong> is a behavioral design pattern that converts a request or operation into a standalone object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of directly calling a method on an object, the application creates a command object that contains the operation to be performed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This approach separates the object that requests an action from the object that actually performs it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Command Pattern typically involves four components:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Command:<\/strong> Defines an interface or convention for executing an operation.<\/li>\n\n\n\n<li><strong>Concrete Command:<\/strong> Implements the operation and connects it to the receiver.<\/li>\n\n\n\n<li><strong>Receiver:<\/strong> Performs the actual business operation.<\/li>\n\n\n\n<li><strong>Invoker:<\/strong> Triggers the command without needing to understand its internal implementation.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This pattern is particularly useful when implementing action queues, undo and redo functionality, task scheduling, and event-driven operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Implement the Command Design Pattern in JavaScript<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s understand the implementation using a simple example: switching a light on and off.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Invoker Class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Switch<\/code> class acts as the invoker. It stores the commands and executes them when requested.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Switch {\n    constructor() {\n        this.commands = [];\n    }\n\n    storeAndExecute(command) {\n        this.commands.push(command);\n        command.execute();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>commands<\/code> stores the executed command objects.<\/li>\n\n\n\n<li><code>storeAndExecute()<\/code> saves the command and invokes its <code>execute()<\/code> method.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the Receiver Class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Light<\/code> class acts as the receiver. It contains the actual operations for switching the light on and off.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Light {\n    turnOn() {\n        console.log(\"Light is ON\");\n    }\n\n    turnOff() {\n        console.log(\"Light is OFF\");\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The receiver does not need to know how the commands are triggered.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create the Concrete Command Classes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, create two command classes: <code>PressUpCommand<\/code> and <code>PressDownCommand<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each command receives a <code>Light<\/code> object through its constructor and executes the corresponding operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command to switch the light on:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class PressUpCommand {\n    constructor(light) {\n        this.light = light;\n    }\n\n    execute() {\n        this.light.turnOn();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command to switch the light off:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class PressDownCommand {\n    constructor(light) {\n        this.light = light;\n    }\n\n    execute() {\n        this.light.turnOff();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both classes follow the same <code>execute()<\/code> method convention. This allows the invoker to execute different commands without knowing their specific implementations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create Objects and Execute the Commands<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, create the required objects and execute the commands.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const light = new Light();\n\nconst switchUp = new PressUpCommand(light);\nconst switchDown = new PressDownCommand(light);\n\nconst lightSwitch = new Switch();\n\nlightSwitch.storeAndExecute(switchUp);\nlightSwitch.storeAndExecute(switchDown);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Expected output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Light is ON\nLight is OFF<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Switch<\/code> class does not directly call <code>turnOn()<\/code> or <code>turnOff()<\/code>. It simply executes the command objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This demonstrates how the Command Pattern separates the invoker from the receiver.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does the Command Pattern Work?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The execution flow can be understood as follows:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The application creates a <code>Light<\/code> receiver object.<\/li>\n\n\n\n<li>Concrete command objects receive the <code>Light<\/code> object.<\/li>\n\n\n\n<li>The <code>Switch<\/code> invoker receives the command objects.<\/li>\n\n\n\n<li>The invoker calls the <code>execute()<\/code> method.<\/li>\n\n\n\n<li>The command delegates the operation to the <code>Light<\/code> receiver.<\/li>\n\n\n\n<li>The receiver performs the requested operation.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This separation makes it easier to introduce additional commands without modifying the invoker.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of the Command Design Pattern<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Command Pattern provides several benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Loose Coupling:<\/strong> Separates the object requesting an operation from the object performing it.<\/li>\n\n\n\n<li><strong>Extensibility:<\/strong> New commands can be introduced without changing the invoker.<\/li>\n\n\n\n<li><strong>Action History:<\/strong> Commands can be stored for logging, auditing, or tracking operations.<\/li>\n\n\n\n<li><strong>Undo and Redo:<\/strong> Commands can be extended with additional methods to reverse or repeat operations.<\/li>\n\n\n\n<li><strong>Flexible Execution:<\/strong> Commands can be queued, scheduled, or executed later.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">When Should You Use the Command Pattern?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Consider using the Command Pattern when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need to separate user actions from business logic.<\/li>\n\n\n\n<li>Your application requires undo and redo functionality.<\/li>\n\n\n\n<li>Operations need to be stored or executed later.<\/li>\n\n\n\n<li>You need to maintain a queue of tasks or requests.<\/li>\n\n\n\n<li>Different actions must be triggered through a common interface.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, avoid introducing design patterns unnecessarily. For a simple operation, a direct method call may be easier to understand and maintain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Software design patterns provide reusable approaches to common software development challenges. The Command Design Pattern is particularly useful when you need to separate the request for an operation from its execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using JavaScript classes, we implemented a simple light-switch example to demonstrate how the invoker, concrete commands, and receiver work together.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding design patterns and knowing when to apply them can help developers write cleaner, more flexible, and maintainable code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are learning software architecture, explore other behavioral, creational, and structural patterns to understand how they can be applied to real-world development projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the Command Design Pattern in JavaScript?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Command Design Pattern is a behavioral pattern that encapsulates an operation inside an object. It separates the object requesting an action from the object performing it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. What are the main components of the Command Pattern?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The four main components are Command, Concrete Command, Receiver, and Invoker. Each component has a separate responsibility in executing an operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What are the advantages of the Command Pattern?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It provides loose coupling, extensibility, action history, and support for implementing undo and redo functionality.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Where is the Command Pattern used?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is commonly used in GUI applications, task queues, transaction processing, automation systems, and applications that require undo and redo operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Is the Command Pattern suitable for every application?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It is useful when operations need to be encapsulated, queued, or tracked. For simple operations, direct method calls may be more appropriate.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/understanding-javascript-cookies\/\">JavaScript Cookies: How to Set, Get, and Delete Cookies<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/synchronous-vs-asynchronous-javascript\/\">Synchronous vs Asynchronous JavaScript<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Need Assistance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with software architecture, JavaScript development, or implementing scalable application solutions?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/pheonixsolutions.com\/devops-cloud\/\">Pheonix Solutions<\/a> technical team can assist with software development, application architecture, and technology implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For assistance, <a href=\"https:\/\/pheonixsolutions.com\/contact\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/contact\/\">contact us<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: December 19, 2019Last Updated: November 10, 2026 Introduction Software applications often contain multiple objects that need to communicate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13247,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1023],"tags":[738,661,739],"class_list":["post-5304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ui-ux-design","tag-design-pattern","tag-javascript","tag-programming","psol-cat-ui-ux-design"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1ny","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-2026-11-12-at-6.18.19-pm.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5304"}],"version-history":[{"count":8,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5304\/revisions"}],"predecessor-version":[{"id":13251,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5304\/revisions\/13251"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/13247"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}