{"id":27,"date":"2020-09-27T09:47:18","date_gmt":"2020-09-27T09:47:18","guid":{"rendered":"https:\/\/system.camp\/index.php\/2020\/09\/27\/the-command-design-pattern\/"},"modified":"2020-10-06T09:56:22","modified_gmt":"2020-10-06T09:56:22","slug":"the-command-design-pattern","status":"publish","type":"post","link":"https:\/\/system.camp\/tutorial\/the-command-design-pattern\/","title":{"rendered":"The Command Design Pattern"},"content":{"rendered":"\n
The command design pattern is a behavioral design pattern. This pattern focuses on encapsulating all the data and related actions of an object. This design pattern allows separating the objects that produce the behavior and the objects that consume them.<\/p>\n\n\n\n
The command design pattern consists of 4 major things:<\/p>\n\n\n\n
Let\u2019s dive into the code to understand the concepts better.<\/p>\n\n\n\n
public interface Command {\n public void execute();\n}\n<\/code><\/pre>\n\n\n\n- The request class which has all the data and methods related to an object.<\/li><\/ul>\n\n\n\n
public class Greet {\n private static final String HELLO = \"Hola!\";\n private static final String BYE = \"Adios!\";\n public void hey() {\n System.out.println(HELLO);\n }\n public void bye() {\n System.out.println(BYE);\n }\n}\n<\/code><\/pre>\n\n\n\n- The concrete classes that implement the Command interface and call necessary functions from the Request class.<\/li><\/ul>\n\n\n\n
public class SayHi implements Command {\n Greet greet;\n public SayHi(Greet greet) {\n this.greet = greet;\n }\n @Override\n public void execute() {\n greet.hey();\n }\n}\n<\/code><\/pre>\n\n\n\npublic class SayBye implements Command {\n Greet greet;\n public SayBye(Greet greet) {\n this.greet = greet;\n }\n @Override\n public void execute() {\n greet.bye();\n }\n}\n<\/code><\/pre>\n\n\n\n- The orchestrator or the broker class.<\/li><\/ul>\n\n\n\n
import java.util.ArrayList;\nimport java.util.List;\n\/\/ This is the orchestrator which helps us take all the commands\n\/\/ we want to execute and adds them to a list after which we can\n\/\/ execute all of them\npublic class Orchestrator {\n List<Command> commandList = new ArrayList<>();\n public void addCommandToList(Command command) {\n commandList.add(command);\n }\n public void executeAllCommands() {\n commandList.forEach(Command::execute);\n }\n}\n<\/code><\/pre>\n\n\n\n- The main class that executes everything.<\/li><\/ul>\n\n\n\n
public class Main {\n public static void main(String[] args) {\n \/\/ The Greet object has the commands that we need to execute\n Greet greet = new Greet();\n \/\/ The Orchestrator object will help add commands to list and execute them\n Orchestrator orchestrator = new Orchestrator();\n \/\/ These are the commands we want to be executed\n Command sayHi = new SayHi(greet);\n Command sayBye = new SayBye(greet);\n orchestrator.addCommandToList(sayHi);\n orchestrator.addCommandToList(sayBye);\n orchestrator.executeAllCommands();\n }\n}\n<\/code><\/pre>\n\n\n\nNow don’t be so lazy! Try to run the code above and find out what the output is \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"
The command design pattern is a behavioral design pattern. This pattern focuses on encapsulating all the data and related actions of an object. This design pattern allows separating the objects that produce the behavior and the objects that consume them.<\/p>\n","protected":false},"author":2,"featured_media":63,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[35],"tags":[17,18,3],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/27"}],"collection":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":3,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/27\/revisions\/65"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media\/63"}],"wp:attachment":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}