{"id":44821,"date":"2015-09-30T06:45:01","date_gmt":"2015-09-30T03:45:01","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44821"},"modified":"2023-12-07T13:09:40","modified_gmt":"2023-12-07T11:09:40","slug":"command-design-pattern","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html","title":{"rendered":"Command Design Pattern Example"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">Java Design Patterns<\/a>.<\/em><\/p>\n<p>In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of them. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">here<\/a>!<\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#what\">2. What is the Command Design Pattern<\/a><\/dt>\n<dt><a href=\"#implement\">3. Implementing the Command Design Pattern<\/a><\/dt>\n<dt><a href=\"#when\">4. When to use the Command Design Pattern<\/a><\/dt>\n<dt><a href=\"#command\">5. Command Design Pattern in JDK<\/a><\/dt>\n<dt><a href=\"#download\">6. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>The Command Design Pattern is a behavioral design pattern and helps to decouples the invoker from the receiver of a request.<\/p>\n<p>To understand the Command Design Pattern let&#8217;s create an example to execute different types of jobs. A job can be anything in a system, for example, sending emails, SMS, logging, and performing some IO functions.<\/p>\n<p>The Command pattern would help to decouple the invoker from a receiver and helps to execute any type of job without knowing its implementation. Let us make this example more interesting by creating threads which will help to execute these jobs concurrently. As these jobs are independent of each other, so the sequence of execution of these jobs is not really important. We will create a thread pool to limit the number of threads to execute jobs. A command object will encapsulates jobs and will hand it over to a thread from the pool that will execute the job.<\/p>\n<p>Before implementing the example, let\u2019s know more about the Command Design Pattern.<\/p>\n<h2><a name=\"what\"><\/a>2. What is the Command Design Pattern<\/h2>\n<p>The intent of the Command Design Pattern is to encapsulate a request as an object, thereby letting the developer to parameterize clients with different requests, queue or log requests, and support undoable operations.<\/p>\n<p>In general, an object-oriented application consists of a set of interacting objects each offering limited, focused functionality. In response to user interaction, the application carries out some kind of processing. For this purpose, the application makes use of the services of different objects for the processing requirement.<\/p>\n<p>In terms of implementation, the application may depend on a designated object that invokes methods on these objects by passing the required data as arguments. This designated object can be referred to as an invoker as it invokes operations on different objects. The invoker may be treated as part of the client application. The set of objects that actually contain the implementation to offer the services required for the request processing can be referred to as <code>Receiver<\/code> objects.<\/p>\n<p>Using the Command pattern, the invoker that issues a request on behalf of the client and the set of service-rendering <code>Receiver<\/code> objects can be decoupled. The Command pattern suggests creating an abstraction for the processing to be carried out or the action to be taken in response to client requests. This abstraction can be designed to declare a common interface to be implemented by different concrete implementers referred to as <code>Command<\/code> objects. Each <code>Command<\/code> object represents a different type of client request and the corresponding processing.<\/p>\n<p>A given <code>Command<\/code> object is responsible for offering the functionality required to process the request it represents, but it does not contain the actual implementation of the functionality. <code>Command<\/code> objects make use of <code>Receiver<\/code> objects in offering this functionality.<\/p>\n<p><figure id=\"attachment_7433\" aria-describedby=\"caption-attachment-7433\" style=\"width: 565px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/command_pattern_class_diagram_1.jpg\"><img decoding=\"async\" class=\"wp-image-7433 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/command_pattern_class_diagram_1.jpg\" alt=\"Figure 1 - Command pattern Class diagram\" width=\"565\" height=\"175\" \/><\/a><figcaption id=\"caption-attachment-7433\" class=\"wp-caption-text\">Figure 1 &#8211; Command pattern Class diagram<\/figcaption><\/figure><\/p>\n<p><strong>Command<\/strong><\/p>\n<ul>\n<li>Declares an interface for executing an operation.<\/li>\n<\/ul>\n<p><strong>ConcreteCommand<\/strong><\/p>\n<ul>\n<li>Defines a binding between a <code>Receiver<\/code> object and an action.<\/li>\n<li>Implements <code>Execute<\/code> by invoking the corresponding operation(s) on <code>Receiver<\/code>.<\/li>\n<\/ul>\n<p><strong>Client<\/strong><\/p>\n<ul>\n<li>Creates a <code>ConcreteCommand<\/code> object and sets its receiver.<\/li>\n<\/ul>\n<p><strong>Invoker<\/strong><\/p>\n<ul>\n<li>Asks the command to carry out the request.<\/li>\n<\/ul>\n<p><strong>Receiver<\/strong><\/p>\n<ul>\n<li>Knows how to perform the operations associated with carrying out a request. Any class may serve as a <code>Receiver<\/code>.<\/li>\n<\/ul>\n<h2><a name=\"implement\"><\/a>3. Implementing the Command Design Pattern<\/h2>\n<p>We will implement the example using a command object. The command object will be referenced by a common interface and will contain a method that will be used to execute the requests. The concrete command classes will override that method and will provide their own specific implementation to execute the request.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic interface Job {\n\n\tpublic void run();\n}\n<\/pre>\n<p>The <code>Job<\/code> interface is the command interface, contains a single method <code>run<\/code>, which is executed by a thread. Our command\u2019s <code>execute<\/code> method is the run method which will be used to execute by a thread in order to get the work done.<\/p>\n<p>There would be a different type of jobs that can be executed. The following are the different concrete classes whose instances will be executed by the different command objects.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class Email {\n\n\tpublic void sendEmail(){\n\t\tSystem.out.println(\"Sending email.......\");\n\t}\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class FileIO {\n\n\tpublic void execute(){\n\t\tSystem.out.println(\"Executing File IO operations...\");\n\t}\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class Logging {\n\n\tpublic void log(){\n\t\tSystem.out.println(\"Logging...\");\n\t}\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class Sms {\n\n\tpublic void sendSms(){\n\t\tSystem.out.println(\"Sending SMS...\");\n\t}\n}\n<\/pre>\n<p>The following are the different command classes that encapsulate the above classes and implement the <code>Job<\/code> interface.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class EmailJob implements Job{\n\n\tprivate Email email;\n\t\n\tpublic void setEmail(Email email){\n\t\tthis.email = email;\n\t}\n\t\n\t@Override\n\tpublic void run() {\n\t\tSystem.out.println(\"Job ID: \"+Thread.currentThread().getId()+\" executing email jobs.\");\n\t\tif(email!=null){\n\t\t\temail.sendEmail();\n\t\t}\n\t\t\n\t\ttry {\n\t\t\tThread.sleep(1000);\n\t\t} catch (InterruptedException e) {\n\t\t\tThread.currentThread().interrupt();\n\t\t}\n\t\t\n\t}\n\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class FileIOJob implements Job{\n\n\tprivate FileIO fileIO;\n\t\n\tpublic void setFileIO(FileIO fileIO){\n\t\tthis.fileIO = fileIO;\n\t}\n\t\n\t@Override\n\tpublic void run() {\n\t\tSystem.out.println(\"Job ID: \"+Thread.currentThread().getId()+\" executing fileIO jobs.\");\n\t\tif(fileIO!=null){\n\t\t\tfileIO.execute();\n\t\t}\n\t\t\n\t\ttry {\n\t\t\tThread.sleep(1000);\n\t\t} catch (InterruptedException e) {\n\t\t\tThread.currentThread().interrupt();\n\t\t}\n\t\t\n\t}\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class LoggingJob implements Job{\n\n\tprivate Logging logging;\n\t\n\tpublic void setLogging(Logging logging){\n\t\tthis.logging = logging;\n\t}\n\t\n\t@Override\n\tpublic void run() {\n\t\tSystem.out.println(\"Job ID: \"+Thread.currentThread().getId()+\" executing logging jobs.\");\n\t\tif(logging!=null){\n\t\t\tlogging.log();\n\t\t}\n\t\t\n\t\ttry {\n\t\t\tThread.sleep(1000);\n\t\t} catch (InterruptedException e) {\n\t\t\tThread.currentThread().interrupt();\n\t\t}\n\t\t\n\t}\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\npublic class SmsJob implements Job{\n\n\tprivate Sms sms;\n\t\n\tpublic void setSms(Sms sms) {\n\t\tthis.sms = sms;\n\t}\n\n\n\t@Override\n\tpublic void run() {\n\t\tSystem.out.println(\"Job ID: \"+Thread.currentThread().getId()+\" executing sms jobs.\");\n\t\tif(sms!=null){\n\t\t\tsms.sendSms();\n\t\t}\n\t\t\n\t\ttry {\n\t\t\tThread.sleep(1000);\n\t\t} catch (InterruptedException e) {\n\t\t\tThread.currentThread().interrupt();\n\t\t}\n\t\t\n\t}\n\n}\n<\/pre>\n<p>The above classes hold a reference to their respective classes that will be used to get the job done. The classes override the <code>run<\/code> method and do the work requested. For example, the <code>SmsJob<\/code> class is used to send sms, its run method calls the <code>sendSms<\/code> method of the <code>Sms<\/code> object in order to get the job done.<\/p>\n<p>You can set different objects one by one to the same <code>command<\/code> object.<\/p>\n<p>The below is the <code>ThreadPool<\/code> class used to create pool of threads and allow a thread to fetch and execute the job from the job queue.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\nimport java.util.concurrent.BlockingQueue;\nimport java.util.concurrent.LinkedBlockingQueue;\n\npublic class ThreadPool {\n\t\n\tprivate final BlockingQueue&lt;Job&gt; jobQueue;\n\tprivate final Thread[] jobThreads;\n\tprivate volatile boolean shutdown;\n\n\tpublic ThreadPool(int n)\n\t{\n\t\tjobQueue = new LinkedBlockingQueue&lt;&gt;();\n\t\tjobThreads = new Thread[n];\n\n\t\tfor (int i = 0; i &lt; n; i++) {\n\t\t\tjobThreads[i] = new Worker(\"Pool Thread \" + i);\n\t\t\tjobThreads[i].start();\n\t\t}\n\t}\n\n\tpublic void addJob(Job r)\n\t{\n\t\ttry {\n\t\t\tjobQueue.put(r);\n\t\t} catch (InterruptedException e) {\n\t\t\tThread.currentThread().interrupt();\n\t\t}\n\t}\n\n\tpublic void shutdownPool()\n\t{\n\t\twhile (!jobQueue.isEmpty()) {\n\t\t\ttry {\n\t\t\t\tThread.sleep(1000);\n\t\t\t} catch (InterruptedException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t\tshutdown = true;\n\t\tfor (Thread workerThread : jobThreads) {\n\t\t\tworkerThread.interrupt();\n\t\t}\n\t}\n\n\tprivate class Worker extends Thread\n\t{\n\t\tpublic Worker(String name)\n\t\t{\n\t\t\tsuper(name);\n\t\t}\n\n\t\tpublic void run()\n\t\t{\n\t\t\twhile (!shutdown) {\n\t\t\t\ttry {\n\t\t\t\t\tJob r = jobQueue.take();\n\t\t\t\t\tr.run();\n\t\t\t\t} catch (InterruptedException e) {\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n}\n<\/pre>\n<p>The above class is used to create n threads (worker threads). Each worker thread will wait for a job in a queue and then execute the job and will go back to waiting state. The class contains a job queue; when a new job will be added into the queue, a worker thread from the pool will execute the job.<\/p>\n<p>We also include a <code>shutdownPool<\/code> method which will used to shut down the pool by interrupting all the worker threads only when the job queue is empty. The <code>addJob<\/code> method is used to add jobs to the queues.<br \/>\n[ulp id=&#8217;7eyRwVlMDyxg6Ptw&#8217;]<br \/>\n&nbsp;<br \/>\nNow, let\u2019s test the code.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.commandpattern;\n\n\npublic class TestCommandPattern {\n\tpublic static void main(String[] args)\n    {\n        init();\n    }\n \n    private static void init()\n    {\n        ThreadPool pool = new ThreadPool(10);\n        \n        Email email = null;\n        EmailJob  emailJob = new EmailJob();\n        \n        Sms sms = null;\n        SmsJob smsJob = new SmsJob();\n        \n        FileIO fileIO = null;;\n        FileIOJob fileIOJob = new FileIOJob();\n        \n        Logging logging = null;\n        LoggingJob logJob = new LoggingJob();\n        \n        for (int i = 0; i &lt; 5; i++) {\n        \temail = new Email();\n        \temailJob.setEmail(email);\n        \t\n        \tsms = new Sms();\n        \tsmsJob.setSms(sms);\n        \t\n        \tfileIO = new FileIO();\n        \tfileIOJob.setFileIO(fileIO);\n        \t\n        \tlogging = new Logging();\n        \tlogJob.setLogging(logging);\n        \t\n            pool.addJob(emailJob);\n            pool.addJob(smsJob);\n            pool.addJob(fileIOJob);\n            pool.addJob(logJob);\n        }\n        pool.shutdownPool();\n    }\n\n}\n<\/pre>\n<p>The above code will result to the following output:<\/p>\n<pre class=\"brush:bash\">Job ID: 9 executing email jobs.\nSending email.......\nJob ID: 12 executing logging jobs.\nJob ID: 17 executing email jobs.\nSending email.......\nJob ID: 13 executing email jobs.\nSending email.......\nJob ID: 10 executing sms jobs.\nSending SMS...\nJob ID: 11 executing fileIO jobs.\nExecuting File IO operations...\nJob ID: 18 executing sms jobs.\nSending SMS...\nLogging...\nJob ID: 16 executing logging jobs.\nLogging...\nJob ID: 15 executing fileIO jobs.\nExecuting File IO operations...\nJob ID: 14 executing sms jobs.\nSending SMS...\nJob ID: 12 executing fileIO jobs.\nExecuting File IO operations...\nJob ID: 10 executing logging jobs.\nLogging...\nJob ID: 18 executing email jobs.\nSending email.......\nJob ID: 16 executing sms jobs.\nSending SMS...\nJob ID: 14 executing fileIO jobs.\nExecuting File IO operations...\nJob ID: 9 executing logging jobs.\nLogging...\nJob ID: 17 executing email jobs.\nSending email.......\nJob ID: 13 executing sms jobs.\nSending SMS...\nJob ID: 15 executing fileIO jobs.\nExecuting File IO operations...\nJob ID: 11 executing logging jobs.\nLogging...\n<\/pre>\n<p>Please note that the output may differ on subsequent executions.<\/p>\n<p>In the above class, we created a thread pool with 10 threads. Then, we set different command objects with different jobs and add these jobs to the queue using the <code>addJob<\/code> method of the <code>ThreadPool<\/code> class. As soon as the job is inserted into the queue, a thread executes the job and removes it from the queue.<\/p>\n<p>We have set different type of jobs, but by using the Command Design Pattern, we decouple the job from the invoker thread. The thread will execute any kind of object that implements the <code>Job<\/code> interface. The different command objects encapsulate the different object and executed the requested operations on these objects.<\/p>\n<p>The output shows the different threads executing the different job. By watching the job id in the output, you can clearly see that a single thread is executing more than one job. This is because after executing a job the thread sends back to the pool.<\/p>\n<p>The advantage of the Command Design Pattern is that you can add more different kind of jobs without changing the existing classes. The leads to more flexibility, and maintainability and also reduce the chances of bugs in the code.<\/p>\n<h2><a name=\"when\"><\/a>4. When to use the Command Design Pattern<\/h2>\n<p>Use the Command pattern when you want to:<\/p>\n<ul>\n<li>Parameterize objects by an action to perform.<\/li>\n<li>Specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.<\/li>\n<li>Support undo. The Command&#8217;s <code>Execute<\/code> operation can store state for reversing its effects in the command itself. The <code>Command<\/code> interface must have an added <code>Un-execute<\/code> operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling <code>Un-execute<\/code> and <code>Execute<\/code>, respectively.<\/li>\n<li>Support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and re-executing them with the Execute operation.<\/li>\n<li>Structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions.<\/li>\n<\/ul>\n<h2><a name=\"command\"><\/a>5. Command Design Pattern in JDK<\/h2>\n<ul>\n<li><code>java.lang.Runnable<\/code><\/li>\n<li><code>javax.swing.Action<\/code><\/li>\n<\/ul>\n<h2><a name=\"download\"><\/a>6. Download the Source Code<\/h2>\n<p>This was a lesson on the Command Design Pattern. You may download the source code here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/CommandPattern-Project.zip\"><strong><strong>CommandPattern-Project<\/strong><\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of &hellip;<\/p>\n","protected":false},"author":967,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[145],"class_list":["post-44821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-design-patterns"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Command Design Pattern Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Command Design Pattern Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-30T03:45:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-07T11:09:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rohit Joshi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Joshi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html\"},\"author\":{\"name\":\"Rohit Joshi\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\"},\"headline\":\"Command Design Pattern Example\",\"datePublished\":\"2015-09-30T03:45:01+00:00\",\"dateModified\":\"2023-12-07T11:09:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html\"},\"wordCount\":1443,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Design Patterns\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html\",\"name\":\"Command Design Pattern Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-30T03:45:01+00:00\",\"dateModified\":\"2023-12-07T11:09:40+00:00\",\"description\":\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/command-design-pattern.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Command Design Pattern Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\",\"name\":\"Rohit Joshi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"caption\":\"Rohit Joshi\"},\"description\":\"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/rohit-joshi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Command Design Pattern Example - Java Code Geeks","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html","og_locale":"en_US","og_type":"article","og_title":"Command Design Pattern Example - Java Code Geeks","og_description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-30T03:45:01+00:00","article_modified_time":"2023-12-07T11:09:40+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Rohit Joshi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rohit Joshi","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html"},"author":{"name":"Rohit Joshi","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20"},"headline":"Command Design Pattern Example","datePublished":"2015-09-30T03:45:01+00:00","dateModified":"2023-12-07T11:09:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html"},"wordCount":1443,"commentCount":1,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Design Patterns"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html","name":"Command Design Pattern Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-30T03:45:01+00:00","dateModified":"2023-12-07T11:09:40+00:00","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/command-design-pattern.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Command Design Pattern Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20","name":"Rohit Joshi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","caption":"Rohit Joshi"},"description":"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/rohit-joshi"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/967"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44821"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}