[{"data":1,"prerenderedAt":788},["ShallowReactive",2],{"i-simple-icons:github":3,"i-simple-icons:x":8,"i-brand:linkedin":10,"i-simple-icons:medium":14,"i-lucide:mail":16,"i-lucide:sun":18,"pattern:\u002Flearn\u002Fdesign-patterns\u002Fcommand":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fcommand":729,"i-lucide:lightbulb":766,"i-lucide:workflow":768,"i-lucide:link":770,"i-lucide:square-terminal":772,"i-lucide:list-ordered":774,"i-lucide:bell":776,"i-lucide:toggle-left":778,"i-lucide:shuffle":780,"i-lucide:layout-template":782,"i-lucide:footprints":784,"i-lucide:copy":786},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M14.234 10.162L22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299l-.929-1.329L3.076 1.56h3.182l5.965 8.532l.929 1.329l7.754 11.09h-3.182z\"\u002F>",{"left":4,"top":11,"width":12,"height":12,"rotate":4,"vFlip":6,"hFlip":6,"body":13},32,448,"\u003Cpath fill=\"currentColor\" d=\"M416 32L31.9 32C14.3 32 0 46.5 0 64.3L0 447.7C0 465.5 14.3 480 31.9 480L416 480c17.6 0 32-14.5 32-32.3l0-383.4C448 46.5 433.6 32 416 32zM135.4 416l-66.4 0 0-213.8 66.5 0 0 213.8-.1 0zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77zM384.3 416l-66.4 0 0-104c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9l0 105.8-66.4 0 0-213.8 63.7 0 0 29.2 .9 0c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9l0 117.2z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"M4.21 0A4.2 4.2 0 0 0 0 4.21v15.58A4.2 4.2 0 0 0 4.21 24h15.58A4.2 4.2 0 0 0 24 19.79v-1.093a5 5 0 0 1-.422.02c-2.577 0-4.027-2.146-4.09-4.832a8 8 0 0 1 .022-.708c.093-1.186.475-2.241 1.105-3.022a3.9 3.9 0 0 1 1.395-1.1c.468-.237 1.127-.367 1.664-.367h.023q.151 0 .303.01V4.211A4.2 4.2 0 0 0 19.79 0Zm.198 5.583h4.165l3.588 8.435l3.59-8.435h3.864v.146l-.019.004c-.705.16-1.063.397-1.063 1.254h-.003l.003 10.274c.06.676.424.885 1.063 1.03l.02.004v.145h-4.923v-.145l.019-.005c.639-.144.994-.353 1.054-1.03V7.267l-4.745 11.15h-.261L6.15 7.569v9.445c0 .857.358 1.094 1.063 1.253l.02.004v.147H4.405v-.147l.019-.004c.705-.16 1.065-.397 1.065-1.253V6.987c0-.857-.358-1.094-1.064-1.254l-.018-.004zm19.25 3.668c-1.086.023-1.733 1.323-1.813 3.124H24V9.298a1.4 1.4 0 0 0-.342-.047m-1.862 3.632c-.1 1.756.86 3.239 2.204 3.634v-3.634z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":17},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m22 7l-8.991 5.727a2 2 0 0 1-2.009 0L2 7\"\u002F>\u003Crect width=\"20\" height=\"16\" x=\"2\" y=\"4\" rx=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":19},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"12\" cy=\"12\" r=\"4\"\u002F>\u003Cpath d=\"M12 2v2m0 16v2M4.93 4.93l1.41 1.41m11.32 11.32l1.41 1.41M2 12h2m16 0h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41\"\u002F>\u003C\u002Fg>",{"id":21,"title":22,"body":23,"description":715,"example":716,"extension":717,"group":718,"icon":719,"meta":720,"navigation":305,"order":216,"path":721,"rawbody":722,"seo":723,"sitemap":724,"source":725,"stem":726,"summary":727,"__hash__":728},"patterns\u002Flearn\u002Fdesign-patterns\u002Fcommand.md","Command",{"type":24,"value":25,"toc":705},"minimark",[26,31,40,53,60,64,80,84,89,100,104,112,116,119,174,180,184,233,351,479,591,686,701],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34,35,39],"p",{},"Usually, the code that wants something done calls the object that does it: a button calls ",[36,37,38],"code",{},"light.turnOn()",". This ties the button to the light. You can't reuse the button for anything else, and you can't keep a record of what was pressed.",[32,41,42,43,46,47,49,50,52],{},"The Command pattern puts the request in an object of its own. A command has one method, ",[36,44,45],{},"execute()",", and knows which object to call and how. That object is the receiver. The object that triggers the action, the invoker, only holds a ",[36,48,22],{}," and calls ",[36,51,45],{}," on it. It doesn't know what the command does.",[32,54,55,56,59],{},"Because a request is now an object, you can store it, pass it around, put it in a queue or write it to a log. Keep a history of executed commands and give each an ",[36,57,58],{},"undo()"," method, and you can reverse them.",[27,61,63],{"id":62},"when-to-use-it","When to use it",[65,66,67,71,74,77],"ul",{},[68,69,70],"li",{},"You want buttons, menu items or keyboard shortcuts to trigger actions without knowing what those actions do.",[68,72,73],{},"You need undo and redo: keep each executed command in a history and reverse it.",[68,75,76],{},"You want to queue requests, schedule them or run them later on another thread.",[68,78,79],{},"You want to log every operation so it can be replayed after a crash.",[27,81,83],{"id":82},"pros-and-cons","Pros and cons",[85,86,88],"h3",{"id":87},"pros","Pros",[65,90,91,94,97],{},[68,92,93],{},"It decouples the classes that invoke operations from the classes that perform them.",[68,95,96],{},"It makes undo, redo and deferred execution possible, since each request is an object you can keep.",[68,98,99],{},"You can add new commands without changing the invoker, and combine simple commands into larger ones.",[85,101,103],{"id":102},"cons","Cons",[65,105,106,109],{},[68,107,108],{},"It adds many small classes: one for every action.",[68,110,111],{},"The extra layer can make the code harder to read: to see what a button does, you have to find its command.",[27,113,115],{"id":114},"the-example","The example",[32,117,118],{},"The example controls a light. Turning it on and turning it off are two commands, and each knows the light it acts on.",[65,120,121,132,148,166],{},[68,122,123,128,129,131],{},[124,125,126],"strong",{},[36,127,22],{},": the command interface, with a single ",[36,130,45],{}," method.",[68,133,134,139,140,143,144,147],{},[124,135,136],{},[36,137,138],{},"Light",": the receiver, the object that does the real work in ",[36,141,142],{},"turnOn()"," and ",[36,145,146],{},"turnOff()",".",[68,149,150,143,155,160,161,163,164,147],{},[124,151,152],{},[36,153,154],{},"TurnOnLightCommand",[124,156,157],{},[36,158,159],{},"TurnOffLightCommand",": concrete commands. Each holds a ",[36,162,138],{}," and calls one of its methods in ",[36,165,45],{},[68,167,168,173],{},[124,169,170],{},[36,171,172],{},"Demo",": the client. It creates the light and the two commands.",[32,175,176,177,179],{},"The example has no invoker. In a full version, something like a remote control would hold the commands and call ",[36,178,45],{}," when a button is pressed.",[27,181,183],{"id":182},"the-code","The code",[185,186,192],"pre",{"className":187,"code":188,"filename":189,"language":190,"meta":191,"style":191},"language-java shiki shiki-themes haytham-dark haytham-light","public interface Command {\n    void execute();\n}\n","Command.java","java","",[36,193,194,214,227],{"__ignoreMap":191},[195,196,199,203,206,210],"span",{"class":197,"line":198},"line",1,[195,200,202],{"class":201},"sVsk0","public",[195,204,205],{"class":201}," interface",[195,207,209],{"class":208},"sNwy7"," Command",[195,211,213],{"class":212},"sVG6I"," {\n",[195,215,217,220,224],{"class":197,"line":216},2,[195,218,219],{"class":201},"    void",[195,221,223],{"class":222},"sVz7n"," execute",[195,225,226],{"class":212},"();\n",[195,228,230],{"class":197,"line":229},3,[195,231,232],{"class":212},"}\n",[185,234,237],{"className":187,"code":235,"filename":236,"language":190,"meta":191,"style":191},"public class Light {\n    public void turnOn() {\n        System.out.println(\"The light is on\");\n    }\n\n    public void turnOff() {\n        System.out.println(\"The light is off\");\n    }\n}\n","Light.java",[36,238,239,251,267,294,300,307,321,341,346],{"__ignoreMap":191},[195,240,241,243,246,249],{"class":197,"line":198},[195,242,202],{"class":201},[195,244,245],{"class":201}," class",[195,247,248],{"class":208}," Light",[195,250,213],{"class":212},[195,252,253,256,259,262,265],{"class":197,"line":216},[195,254,255],{"class":201},"    public",[195,257,258],{"class":201}," void",[195,260,261],{"class":222}," turnOn",[195,263,264],{"class":212},"()",[195,266,213],{"class":212},[195,268,269,273,275,279,281,284,287,291],{"class":197,"line":229},[195,270,272],{"class":271},"sWFPS","        System",[195,274,147],{"class":212},[195,276,278],{"class":277},"s4Awn","out",[195,280,147],{"class":212},[195,282,283],{"class":222},"println",[195,285,286],{"class":212},"(",[195,288,290],{"class":289},"sUOzB","\"The light is on\"",[195,292,293],{"class":212},");\n",[195,295,297],{"class":197,"line":296},4,[195,298,299],{"class":212},"    }\n",[195,301,303],{"class":197,"line":302},5,[195,304,306],{"emptyLinePlaceholder":305},true,"\n",[195,308,310,312,314,317,319],{"class":197,"line":309},6,[195,311,255],{"class":201},[195,313,258],{"class":201},[195,315,316],{"class":222}," turnOff",[195,318,264],{"class":212},[195,320,213],{"class":212},[195,322,324,326,328,330,332,334,336,339],{"class":197,"line":323},7,[195,325,272],{"class":271},[195,327,147],{"class":212},[195,329,278],{"class":277},[195,331,147],{"class":212},[195,333,283],{"class":222},[195,335,286],{"class":212},[195,337,338],{"class":289},"\"The light is off\"",[195,340,293],{"class":212},[195,342,344],{"class":197,"line":343},8,[195,345,299],{"class":212},[195,347,349],{"class":197,"line":348},9,[195,350,232],{"class":212},[185,352,355],{"className":187,"code":353,"filename":354,"language":190,"meta":191,"style":191},"public class TurnOnLightCommand implements Command {\n    private final Light light;\n\n    public TurnOnLightCommand(Light light) {\n        this.light = light;\n    }\n\n    @Override\n    public void execute() {\n        light.turnOn();\n    }\n}\n","TurnOnLightCommand.java",[36,356,357,373,389,393,410,428,432,436,444,456,469,474],{"__ignoreMap":191},[195,358,359,361,363,366,369,371],{"class":197,"line":198},[195,360,202],{"class":201},[195,362,245],{"class":201},[195,364,365],{"class":208}," TurnOnLightCommand",[195,367,368],{"class":201}," implements",[195,370,209],{"class":208},[195,372,213],{"class":212},[195,374,375,378,381,383,386],{"class":197,"line":216},[195,376,377],{"class":201},"    private",[195,379,380],{"class":201}," final",[195,382,248],{"class":201},[195,384,385],{"class":271}," light",[195,387,388],{"class":212},";\n",[195,390,391],{"class":197,"line":229},[195,392,306],{"emptyLinePlaceholder":305},[195,394,395,397,399,401,403,405,408],{"class":197,"line":296},[195,396,255],{"class":201},[195,398,365],{"class":222},[195,400,286],{"class":212},[195,402,138],{"class":201},[195,404,385],{"class":271},[195,406,407],{"class":212},")",[195,409,213],{"class":212},[195,411,412,416,418,421,424,426],{"class":197,"line":302},[195,413,415],{"class":414},"sheII","        this",[195,417,147],{"class":212},[195,419,420],{"class":277},"light",[195,422,423],{"class":212}," =",[195,425,385],{"class":271},[195,427,388],{"class":212},[195,429,430],{"class":197,"line":309},[195,431,299],{"class":212},[195,433,434],{"class":197,"line":323},[195,435,306],{"emptyLinePlaceholder":305},[195,437,438,441],{"class":197,"line":343},[195,439,440],{"class":212},"    @",[195,442,443],{"class":201},"Override\n",[195,445,446,448,450,452,454],{"class":197,"line":348},[195,447,255],{"class":201},[195,449,258],{"class":201},[195,451,223],{"class":222},[195,453,264],{"class":212},[195,455,213],{"class":212},[195,457,459,462,464,467],{"class":197,"line":458},10,[195,460,461],{"class":271},"        light",[195,463,147],{"class":212},[195,465,466],{"class":222},"turnOn",[195,468,226],{"class":212},[195,470,472],{"class":197,"line":471},11,[195,473,299],{"class":212},[195,475,477],{"class":197,"line":476},12,[195,478,232],{"class":212},[185,480,483],{"className":187,"code":481,"filename":482,"language":190,"meta":191,"style":191},"public class TurnOffLightCommand implements Command {\n    private final Light light;\n\n    public TurnOffLightCommand(Light light) {\n        this.light = light;\n    }\n\n    @Override\n    public void execute() {\n        light.turnOff();\n    }\n}\n","TurnOffLightCommand.java",[36,484,485,500,512,516,532,546,550,554,560,572,583,587],{"__ignoreMap":191},[195,486,487,489,491,494,496,498],{"class":197,"line":198},[195,488,202],{"class":201},[195,490,245],{"class":201},[195,492,493],{"class":208}," TurnOffLightCommand",[195,495,368],{"class":201},[195,497,209],{"class":208},[195,499,213],{"class":212},[195,501,502,504,506,508,510],{"class":197,"line":216},[195,503,377],{"class":201},[195,505,380],{"class":201},[195,507,248],{"class":201},[195,509,385],{"class":271},[195,511,388],{"class":212},[195,513,514],{"class":197,"line":229},[195,515,306],{"emptyLinePlaceholder":305},[195,517,518,520,522,524,526,528,530],{"class":197,"line":296},[195,519,255],{"class":201},[195,521,493],{"class":222},[195,523,286],{"class":212},[195,525,138],{"class":201},[195,527,385],{"class":271},[195,529,407],{"class":212},[195,531,213],{"class":212},[195,533,534,536,538,540,542,544],{"class":197,"line":302},[195,535,415],{"class":414},[195,537,147],{"class":212},[195,539,420],{"class":277},[195,541,423],{"class":212},[195,543,385],{"class":271},[195,545,388],{"class":212},[195,547,548],{"class":197,"line":309},[195,549,299],{"class":212},[195,551,552],{"class":197,"line":323},[195,553,306],{"emptyLinePlaceholder":305},[195,555,556,558],{"class":197,"line":343},[195,557,440],{"class":212},[195,559,443],{"class":201},[195,561,562,564,566,568,570],{"class":197,"line":348},[195,563,255],{"class":201},[195,565,258],{"class":201},[195,567,223],{"class":222},[195,569,264],{"class":212},[195,571,213],{"class":212},[195,573,574,576,578,581],{"class":197,"line":458},[195,575,461],{"class":271},[195,577,147],{"class":212},[195,579,580],{"class":222},"turnOff",[195,582,226],{"class":212},[195,584,585],{"class":197,"line":471},[195,586,299],{"class":212},[195,588,589],{"class":197,"line":476},[195,590,232],{"class":212},[185,592,595],{"className":187,"code":593,"filename":594,"language":190,"meta":191,"style":191},"public class Demo {\n    public static void main(String[] args) {\n        Light light = new Light();\n        new TurnOnLightCommand(light);\n        new TurnOffLightCommand(light);\n    }\n}\n","Demo.java",[36,596,597,608,635,653,666,678,682],{"__ignoreMap":191},[195,598,599,601,603,606],{"class":197,"line":198},[195,600,202],{"class":201},[195,602,245],{"class":201},[195,604,605],{"class":208}," Demo",[195,607,213],{"class":212},[195,609,610,612,615,617,620,622,625,628,631,633],{"class":197,"line":216},[195,611,255],{"class":201},[195,613,614],{"class":201}," static",[195,616,258],{"class":201},[195,618,619],{"class":222}," main",[195,621,286],{"class":212},[195,623,624],{"class":201},"String",[195,626,627],{"class":212},"[]",[195,629,630],{"class":271}," args",[195,632,407],{"class":212},[195,634,213],{"class":212},[195,636,637,640,643,646,649,651],{"class":197,"line":229},[195,638,639],{"class":201},"        Light",[195,641,642],{"class":271}," light ",[195,644,645],{"class":212},"=",[195,647,648],{"class":201}," new",[195,650,248],{"class":222},[195,652,226],{"class":212},[195,654,655,658,660,662,664],{"class":197,"line":296},[195,656,657],{"class":201},"        new",[195,659,365],{"class":222},[195,661,286],{"class":212},[195,663,420],{"class":271},[195,665,293],{"class":212},[195,667,668,670,672,674,676],{"class":197,"line":302},[195,669,657],{"class":201},[195,671,493],{"class":222},[195,673,286],{"class":212},[195,675,420],{"class":271},[195,677,293],{"class":212},[195,679,680],{"class":197,"line":309},[195,681,299],{"class":212},[195,683,684],{"class":197,"line":323},[195,685,232],{"class":212},[32,687,688,689,691,692,694,695,697,698,147],{},"Running ",[36,690,172],{}," prints nothing: it creates the two commands but never calls ",[36,693,45],{}," on them. Calling ",[36,696,45],{}," on the first one would print ",[36,699,700],{},"The light is on",[702,703,704],"style",{},"html pre.shiki code .sVsk0, html code.shiki .sVsk0{--shiki-default:#9D9FFE;--shiki-light:#6762D3}html pre.shiki code .sNwy7, html code.shiki .sNwy7{--shiki-default:#D4D7FE;--shiki-light:#3F3B87}html pre.shiki code .sVG6I, html code.shiki .sVG6I{--shiki-default:#85858F;--shiki-light:#71717A}html pre.shiki code .sVz7n, html code.shiki .sVz7n{--shiki-default:#EDEDEF;--shiki-light:#0B0B0C}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html pre.shiki code .sWFPS, html code.shiki .sWFPS{--shiki-default:#C4C4CC;--shiki-light:#3F3F46}html pre.shiki code .s4Awn, html code.shiki .s4Awn{--shiki-default:#C5DDFE;--shiki-light:#534DAF}html pre.shiki code .sUOzB, html code.shiki .sUOzB{--shiki-default:#9EC7FE;--shiki-light:#2873D1}html pre.shiki code .sheII, html code.shiki .sheII{--shiki-default:#70ADFF;--shiki-light:#195CAE}",{"title":191,"searchDepth":216,"depth":216,"links":706},[707,708,709,713,714],{"id":29,"depth":216,"text":30},{"id":62,"depth":216,"text":63},{"id":82,"depth":216,"text":83,"children":710},[711,712],{"id":87,"depth":229,"text":88},{"id":102,"depth":229,"text":103},{"id":114,"depth":216,"text":115},{"id":182,"depth":216,"text":183},"The Command pattern turns a request into an object, so you can pass it around, queue it, log it or undo it later. Explained with a worked Java example of commands that switch a light on and off.","Commands that turn a light on and off","md","Behavioral","lucide:square-terminal",{},"\u002Flearn\u002Fdesign-patterns\u002Fcommand","---\ntitle: Command\ngroup: Behavioral\nicon: lucide:square-terminal\nsummary: 'Turn a request into an object, so it can be passed around, queued, logged or undone.'\ndescription: 'The Command pattern turns a request into an object, so you can pass it around, queue it, log it or undo it later. Explained with a worked Java example of commands that switch a light on and off.'\nexample: 'Commands that turn a light on and off'\norder: 2\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FBehavioral\u002FCommand\n---\n\n## The idea\n\nUsually, the code that wants something done calls the object that does it: a button calls `light.turnOn()`. This ties the button to the light. You can't reuse the button for anything else, and you can't keep a record of what was pressed.\n\nThe Command pattern puts the request in an object of its own. A command has one method, `execute()`, and knows which object to call and how. That object is the receiver. The object that triggers the action, the invoker, only holds a `Command` and calls `execute()` on it. It doesn't know what the command does.\n\nBecause a request is now an object, you can store it, pass it around, put it in a queue or write it to a log. Keep a history of executed commands and give each an `undo()` method, and you can reverse them.\n\n## When to use it\n\n- You want buttons, menu items or keyboard shortcuts to trigger actions without knowing what those actions do.\n- You need undo and redo: keep each executed command in a history and reverse it.\n- You want to queue requests, schedule them or run them later on another thread.\n- You want to log every operation so it can be replayed after a crash.\n\n## Pros and cons\n\n### Pros\n\n- It decouples the classes that invoke operations from the classes that perform them.\n- It makes undo, redo and deferred execution possible, since each request is an object you can keep.\n- You can add new commands without changing the invoker, and combine simple commands into larger ones.\n\n### Cons\n\n- It adds many small classes: one for every action.\n- The extra layer can make the code harder to read: to see what a button does, you have to find its command.\n\n## The example\n\nThe example controls a light. Turning it on and turning it off are two commands, and each knows the light it acts on.\n\n- **`Command`**: the command interface, with a single `execute()` method.\n- **`Light`**: the receiver, the object that does the real work in `turnOn()` and `turnOff()`.\n- **`TurnOnLightCommand`** and **`TurnOffLightCommand`**: concrete commands. Each holds a `Light` and calls one of its methods in `execute()`.\n- **`Demo`**: the client. It creates the light and the two commands.\n\nThe example has no invoker. In a full version, something like a remote control would hold the commands and call `execute()` when a button is pressed.\n\n## The code\n\n```java [Command.java]\npublic interface Command {\n    void execute();\n}\n```\n\n```java [Light.java]\npublic class Light {\n    public void turnOn() {\n        System.out.println(\"The light is on\");\n    }\n\n    public void turnOff() {\n        System.out.println(\"The light is off\");\n    }\n}\n```\n\n```java [TurnOnLightCommand.java]\npublic class TurnOnLightCommand implements Command {\n    private final Light light;\n\n    public TurnOnLightCommand(Light light) {\n        this.light = light;\n    }\n\n    @Override\n    public void execute() {\n        light.turnOn();\n    }\n}\n```\n\n```java [TurnOffLightCommand.java]\npublic class TurnOffLightCommand implements Command {\n    private final Light light;\n\n    public TurnOffLightCommand(Light light) {\n        this.light = light;\n    }\n\n    @Override\n    public void execute() {\n        light.turnOff();\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        Light light = new Light();\n        new TurnOnLightCommand(light);\n        new TurnOffLightCommand(light);\n    }\n}\n```\n\nRunning `Demo` prints nothing: it creates the two commands but never calls `execute()` on them. Calling `execute()` on the first one would print `The light is on`.\n",{"title":22,"description":715},{"loc":721},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FBehavioral\u002FCommand","learn\u002Fdesign-patterns\u002Fcommand","Turn a request into an object, so it can be passed around, queued, logged or undone.","MHbwJeKOTEqzt7XLj1OcjHemJfDEQxxBml62llLcnqs",[730,735,736,741,746,751,756,761],{"path":731,"title":732,"icon":733,"summary":734,"order":198},"\u002Flearn\u002Fdesign-patterns\u002Fchain-of-responsibility","Chain of Responsibility","lucide:link","Pass a request along a chain of handlers, where each one can deal with it or hand it to the next.",{"path":721,"title":22,"icon":719,"summary":727,"order":216},{"path":737,"title":738,"icon":739,"summary":740,"order":229},"\u002Flearn\u002Fdesign-patterns\u002Fiterator","Iterator","lucide:list-ordered","Walk through the items of a collection one by one, without knowing how the collection stores them.",{"path":742,"title":743,"icon":744,"summary":745,"order":296},"\u002Flearn\u002Fdesign-patterns\u002Fobserver","Observer","lucide:bell","One object announces a change, and every object that subscribed to it hears about it.",{"path":747,"title":748,"icon":749,"summary":750,"order":302},"\u002Flearn\u002Fdesign-patterns\u002Fstate","State","lucide:toggle-left","An object changes its behaviour when its internal state changes, as if it had changed its class.",{"path":752,"title":753,"icon":754,"summary":755,"order":309},"\u002Flearn\u002Fdesign-patterns\u002Fstrategy","Strategy","lucide:shuffle","Put each way of doing a task in its own class, and choose which one to use while the program runs.",{"path":757,"title":758,"icon":759,"summary":760,"order":323},"\u002Flearn\u002Fdesign-patterns\u002Ftemplate-method","Template Method","lucide:layout-template","A base class fixes the steps of a task, and subclasses fill in the steps that differ.",{"path":762,"title":763,"icon":764,"summary":765,"order":343},"\u002Flearn\u002Fdesign-patterns\u002Fvisitor","Visitor","lucide:footprints","Add new operations to a set of classes without changing them, by moving each operation into its own visitor.",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":767},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M15 14c.2-1 .7-1.7 1.5-2.5c1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5c.7.7 1.3 1.5 1.5 2.5m0 4h6m-5 4h4\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":769},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"8\" height=\"8\" x=\"3\" y=\"3\" rx=\"2\"\u002F>\u003Cpath d=\"M7 11v4a2 2 0 0 0 2 2h4\"\u002F>\u003Crect width=\"8\" height=\"8\" x=\"13\" y=\"13\" rx=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":771},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71\"\u002F>\u003Cpath d=\"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":773},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m7 11l2-2l-2-2m4 6h4\"\u002F>\u003Crect width=\"18\" height=\"18\" x=\"3\" y=\"3\" rx=\"2\" ry=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":775},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M11 5h10m-10 7h10m-10 7h10M4 4h1v5M4 9h2m.5 11H3.4c0-1 2.6-1.925 2.6-3.5a1.5 1.5 0 0 0-2.6-1.02\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":777},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M10.268 21a2 2 0 0 0 3.464 0m-10.47-5.674A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":779},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"9\" cy=\"12\" r=\"3\"\u002F>\u003Crect width=\"20\" height=\"14\" x=\"2\" y=\"5\" rx=\"7\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":781},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m18 14l4 4l-4 4m0-20l4 4l-4 4\"\u002F>\u003Cpath d=\"M2 18h1.973a4 4 0 0 0 3.3-1.7l5.454-8.6a4 4 0 0 1 3.3-1.7H22M2 6h1.972a4 4 0 0 1 3.6 2.2M22 18h-6.041a4 4 0 0 1-3.3-1.8l-.359-.45\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":783},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"18\" height=\"7\" x=\"3\" y=\"3\" rx=\"1\"\u002F>\u003Crect width=\"9\" height=\"7\" x=\"3\" y=\"14\" rx=\"1\"\u002F>\u003Crect width=\"5\" height=\"7\" x=\"16\" y=\"14\" rx=\"1\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":785},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 16v-2.38C4 11.5 2.97 10.5 3 8c.03-2.72 1.49-6 4.5-6C9.37 2 10 3.8 10 5.5c0 3.11-2 5.66-2 8.68V16a2 2 0 1 1-4 0m16 4v-2.38c0-2.12 1.03-3.12 1-5.62c-.03-2.72-1.49-6-4.5-6C14.63 6 14 7.8 14 9.5c0 3.11 2 5.66 2 8.68V20a2 2 0 1 0 4 0m-4-3h4M4 13h4\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":787},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"\u002F>\u003Cpath d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"\u002F>\u003C\u002Fg>",1793131416775]