{"id":2320,"date":"2019-11-04T13:29:54","date_gmt":"2019-11-04T10:29:54","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2320"},"modified":"2024-12-16T22:32:32","modified_gmt":"2024-12-16T19:32:32","slug":"command-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2019\/11\/04\/command-pattern\/","title":{"rendered":"Pattern Team"},"content":{"rendered":"<p><a href=\"https:\/\/pixabay.com\/illustrations\/vegan-go-vegan-compassion-sign-1343429\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2327\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/11\/govegan.png\" alt=\"\" width=\"400\" height=\"260\" srcset=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/11\/govegan.png 400w, https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/11\/govegan-300x195.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>The Command pattern is a behavioral design pattern.<\/p>\n<p>This is the pattern I&#8217;ve been sitting with the longest, it&#8217;s so simple it&#8217;s very complex. But personally, I find the beauty of self-study is that you have all the time in the world to explore a particular issue from all angles.<\/p>\n<p>So, in GoF the applicability is described quite concisely and clearly:<br \/><b><i>Encapsulates a request as an object, allowing you to parameterize clients with different requests, use queues, log requests, and perform cancellation operations.<\/i><\/b><\/p>\n<p>Now we will implement a simple version of the command from the description:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>string fakeTrumpsRequest = \u201cSELECT * from Users where name beginsWith DonaldTrump\u201d\n<\/code><\/pre>\n<\/div>\n<p>We encapsulated the request in a string class object, it can configure clients, add commands to the queue, log, cancel (using the \u201cSnapshot\u201d pattern)<\/p>\n<p>I think this is quite enough to implement SQL queries and the like, but then the implementation details begin, different application options, the pattern code base, client roles also vary greatly, and auxiliary classes are added.<\/p>\n<h3>Material<\/h3>\n<p>The Command pattern begins with the <b>Command protocol<\/b>, which contains a single <b>execute()<\/b> method. Next comes the <b>Specific Command<\/b> and the Receiver.<\/b> The CC implements the operation on the Receiver, describes the connection between the Receiver and the action. Is anything unclear? Me too, but let&#8217;s move on. <b>The Client<\/b> creates an instance of the <b>Specific Command<\/b>, associates it with the <b>Receiver<\/b>. <b>Invoker<\/b> is an object that carries out the process of launching the <b>Command<\/b>.<\/p>\n<p>Now let&#8217;s try to understand it with an example, let&#8217;s say we want to update myOS on a myPhone phone, to do this we launch the myOS_Update! application, in it we press the Update Now! button, after 10 seconds the system will report a successful update.<\/p>\n<p><b>The client <\/b> in the example above is the myOS_Update! application, the <b>Invoker<\/b> is the \u201cUpdate Now!\u201d button, it runs the <b>Specific Command <\/b> of updating the system using the execute() method, which calls the <b>Receiver <\/b> \u2013 the operating system update daemon.<\/p>\n<h3>Example of use<\/h3>\n<p>Let&#8217;s say the UI of the myOS_Update! application is so good that they decided to sell it as a separate product to provide an interface for updating other operating systems. In this case, we will implement the application with support for extension via libraries, the libraries will contain implementations of <b>Specific Commands, Receivers,<\/b> we will leave static\/immutable <b>Invoker<\/b>, <b>Client<\/b>, protocol <b>Commands<\/b>.<\/p>\n<p>This eliminates the need to support mutable code, since our code will remain unchanged, problems can only arise during implementation on the client side, due to errors in the code of their <b>Specific Commands<\/b> and <b>Receivers<\/b>. Also, in such an implementation, there is no need to transfer the source code of the main application, that is, we have implemented the encapsulation of commands and UI interactions using the <b>Command<\/b> pattern.<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/command\" target=\"_blank\" rel=\"noopener\">https:\/\/refactoring.guru\/ru\/design-patterns\/command <\/a><br \/><a href=\"https:\/\/www.amazon.com\/Design-Patterns-Elements-Reusable-Object-Oriented\/dp\/0201633612\" target=\"_blank\" rel=\"noopener\">https:\/\/www.amazon.com\/Design-Patterns-Elements-Reusable-Object-Oriented\/dp\/0201633612<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Command pattern is a behavioral design pattern. This is the pattern I&#8217;ve been sitting with the longest, it&#8217;s so simple it&#8217;s very complex. But personally, I find the beauty of self-study is that you have all the time in the world to explore a particular issue from all angles. So, in GoF the applicability<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2019\/11\/04\/command-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Pattern Team&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[127,95],"class_list":["post-2320","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-command","tag-patterns","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["en","ru","zh","de","fr","ja","pt"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/comments?post=2320"}],"version-history":[{"count":14,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2320\/revisions"}],"predecessor-version":[{"id":3935,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2320\/revisions\/3935"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=2320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=2320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=2320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}