Client<\/strong> – It creates the command with the appropriate receiver bypassing the receiver to the command’s constructor. After that, it also associates the resulting command with an invoker.<\/li><\/ul>\n\n\n\nLet’s understand a situation after which it will be clear why the command pattern is useful. Imagine the case of a TV. A TV can be turned ON<\/strong> by either<\/p>\n\n\n\nRemote ON Button<\/li> On Button on the tv.<\/li><\/ol>\n\n\n\nBoth these trigger points do the same thing i.e. turn the TV on. In order to ON the TV, we can implement the ON command object with the receiver as the TV. When execute() method is called on this ON command object, it in turn call TV.on() function. So in this case:<\/p>\n\n\n\n
Receiver <\/strong>is the TV<\/strong><\/li>Command <\/strong>is the ON command object <\/strong>which embeds TV<\/strong><\/li>Invoker<\/strong> is the Remote ON Button<\/strong> or the ON Button on the TV<\/strong>. Both embed the ON command object<\/strong><\/li><\/ul>\n\n\n\nNotice here that we have wrapped the request of turning the TV<\/strong> on into an ON command object <\/strong>which can be invoked by multiple invokers. This ON command object embeds the receiver (TV here) and can be executed independently.<\/p>\n\n\n\nAs another example, imagine the case of an Adobe Photoshop<\/strong> Application. In Photoshop a Save operation can be triggered from 3 places<\/p>\n\n\n\nFrom the menu.<\/li> From the button on the upper bar.<\/li> Using shortcut Ctrl+S.<\/li><\/ol>\n\n\n\nAll three trigger points do the same thing, i.e save the current image in the application. This saves can be wrapped into a Save Command Object with a current image open in the application as the receiver. <\/p>\n\n\n\n
What’s the benefit of creating a separate command object in the above examples.<\/p>\n\n\n\n
It decouples the UI logic from underlying business logic<\/li> No need to create different handlers for each of the invokers.<\/li> The command object contains all the information it needs to execute. Hence it can also be used for delayed execution.<\/li><\/ol>\n\n\n\nLet’s look at the UML diagram now.<\/p>\n\n\n\n
<\/span>UML Diagram:<\/strong><\/span><\/h2>\n\n\n\nNotice how Invoker embeds the command. The request is sent to the Invoker and it passes the request to the encapsulated command object.<\/li> All the Concrete Command Object embed the receiver<\/li><\/ul>\n\n\n\n <\/figure><\/li><\/ul><\/figure>\n\n\n\n<\/p>\n\n\n\n
<\/span>Mapping <\/strong><\/span><\/h2>\n\n\n\n The below table represents the mapping from the UML diagram actors to actual implementation actors in “Practical Example”<\/strong> below <\/p>\n\n\n\nInvoker<\/td> button.go<\/td><\/tr> Command Interface<\/td> command.go<\/td><\/tr> Concrete Command 1<\/td> onCommand.go<\/td><\/tr> Concrete Command 2<\/td> offCommand.go<\/td><\/tr> Receiver Interface<\/td> device.go<\/td><\/tr> Concrete Receiver<\/td> tv.go<\/td><\/tr> Client<\/td> main.go<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/span>Practical Example:<\/strong><\/span><\/h2>\n\n\n\nbutton.go<\/strong><\/p>\n\n\n\npackage main\n\ntype button struct {\n command command\n}\n\nfunc (b *button) press() {\n b.command.execute()\n}<\/code><\/pre>\n\n\n\ncommand.go<\/strong><\/p>\n\n\n\npackage main\n\ntype command interface {\n execute()\n}<\/code><\/pre>\n\n\n\nonCommand.go<\/strong><\/p>\n\n\n\npackage main\n\ntype onCommand struct {\n device device\n}\n\nfunc (c *onCommand) execute() {\n c.device.on()\n}<\/code><\/pre>\n\n\n\noffCommand.go<\/strong><\/p>\n\n\n\npackage main\n\ntype offCommand struct {\n device device\n}\n\nfunc (c *offCommand) execute() {\n c.device.off()\n}<\/code><\/pre>\n\n\n\ndevice.go<\/strong><\/p>\n\n\n\npackage main\n\ntype device interface {\n on()\n off()\n}<\/code><\/pre>\n\n\n\ntv.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype tv struct {\n isRunning bool\n}\n\nfunc (t *tv) on() {\n t.isRunning = true\n fmt.Println(\"Turning tv on\")\n}\n\nfunc (t *tv) off() {\n t.isRunning = false\n fmt.Println(\"Turning tv off\")\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nfunc main() {\n tv := &tv{}\n onCommand := &onCommand{\n device: tv,\n }\n offCommand := &offCommand{\n device: tv,\n }\n onButton := &button{\n command: onCommand,\n }\n onButton.press()\n offButton := &button{\n command: offCommand,\n }\n offButton.press()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nTurning tv on\nTurning tv off<\/code><\/pre>\n\n\n\n<\/span>Full Working Code:<\/strong><\/span><\/h2>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype button struct {\n command command\n}\n\nfunc (b *button) press() {\n b.command.execute()\n}\n\ntype command interface {\n execute()\n}\n\ntype offCommand struct {\n device device\n}\n\nfunc (c *offCommand) execute() {\n c.device.off()\n}\n\ntype onCommand struct {\n device device\n}\n\nfunc (c *onCommand) execute() {\n c.device.on()\n}\n\ntype device interface {\n on()\n off()\n}\n\ntype tv struct {\n isRunning bool\n}\n\nfunc (t *tv) on() {\n t.isRunning = true\n fmt.Println(\"Turning tv on\")\n}\n\nfunc (t *tv) off() {\n t.isRunning = false\n fmt.Println(\"Turning tv off\")\n}\n\nfunc main() {\n tv := &tv{}\n onCommand := &onCommand{\n device: tv,\n }\n offCommand := &offCommand{\n device: tv,\n }\n onButton := &button{\n command: onCommand,\n }\n onButton.press()\n offButton := &button{\n command: offCommand,\n }\n offButton.press()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nTurning tv on\nTurning tv off<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Note: Interested in understanding how all other design patterns can be implemented in GO. Please see this full reference – https:\/\/golangbyexamples.com\/all-design-patterns-golang\/ Table of Contents Introduction:UML Diagram:Mapping Practical Example:Full Working Code: Introduction: Command Design…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[70,25,3,4],"class_list":["post-594","post","type-post","status-publish","format-standard","hentry","category-tech","tag-command-design-pattern-in-go","tag-design-pattern","tag-go","tag-golang"],"yoast_head":"\n
Command Design Pattern in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n