Concrete Implementation<\/strong><\/li><\/ul>\n\n\n\nAbstraction hierarchy is being referred to by clients without worrying about the implementation. Let’s take an example. Assume you have two types of computer mac<\/strong> and windows. <\/strong>Also, let’s say two types of printer epson<\/strong> and hp<\/strong> . Both computers and printers needs to work with each other in any combination. The client will only access the computer without worrying about how print is happening. Instead of creating four structs for the 2*2 combination, we create two hierarchies<\/p>\n\n\n\nAbstraction Hierarchy<\/li> Implementation Hierarchy<\/li><\/ul>\n\n\n\nSee the below figure. These two hierarchies communicate with each other via a bridge where Abstraction<\/strong> (computer here) contains a reference to the Implementation<\/strong>(printer here). Both the abstraction and implementation can continue to develop independently without affecting each other. Notice how win<\/strong> and mac<\/strong> embed the reference to printer<\/strong>. We can change the Abstraction’sImplementation<\/strong> (i.e., computer’s printer) at run time as abstraction refers to implementation via the interface. On calling mac.print() or windows.print()<\/strong> it dispatches the request to printer.printFile()<\/strong>. This acts as a bridge and provides a loose coupling between the two.<\/p>\n\n\n\n <\/figure><\/li><\/ul><\/figure>\n\n\n\n<\/span>UML Diagram:<\/strong><\/span><\/h2>\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\nThe below table represents the mapping from the UML diagram actors to actual implementation actors in “Practical Example”<\/strong> below <\/p>\n\n\n\nAbstraction<\/td> computer.go<\/td><\/tr> Refined Abstraction 1<\/td> win.go<\/td><\/tr> Refined Abstraction 2<\/td> mac.go<\/td><\/tr> Implementation<\/td> printer.go<\/td><\/tr> Concrete Implementation 1<\/td> epson.go<\/td><\/tr> Concrete Implementation 2<\/td> hp.go<\/td><\/tr> Client<\/td> main.go<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/p>\n\n\n\n
<\/span>Practical Example<\/strong><\/span><\/h2>\n\n\n\ncomputer.go<\/strong><\/p>\n\n\n\npackage main\n\ntype computer interface {\n print()\n setPrinter(printer)\n}<\/code><\/pre>\n\n\n\nmac.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype mac struct {\n printer printer\n}\n\nfunc (m *mac) print() {\n fmt.Println(\"Print request for mac\")\n m.printer.printFile()\n}\n\nfunc (m *mac) setPrinter(p printer) {\n m.printer = p\n}<\/code><\/pre>\n\n\n\nwindows.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype windows struct {\n printer printer\n}\n\nfunc (w *windows) print() {\n fmt.Println(\"Print request for windows\")\n w.printer.printFile()\n}\n\nfunc (w *windows) setPrinter(p printer) {\n w.printer = p\n}<\/code><\/pre>\n\n\n\nprinter.go<\/strong><\/p>\n\n\n\npackage main\n\ntype printer interface {\n printFile()\n}<\/code><\/pre>\n\n\n\nepson.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype epson struct {\n}\n\nfunc (p *epson) printFile() {\n fmt.Println(\"Printing by a EPSON Printer\")\n}<\/code><\/pre>\n\n\n\nhp.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype hp struct {\n}\n\nfunc (p *hp) printFile() {\n fmt.Println(\"Printing by a HP Printer\")\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n hpPrinter := &hp{}\n epsonPrinter := &epson{}\n macComputer := &mac{}\n macComputer.setPrinter(hpPrinter)\n macComputer.print()\n fmt.Println()\n macComputer.setPrinter(epsonPrinter)\n macComputer.print()\n fmt.Println()\n winComputer := &windows{}\n winComputer.setPrinter(hpPrinter)\n winComputer.print()\n fmt.Println()\n winComputer.setPrinter(epsonPrinter)\n winComputer.print()\n fmt.Println()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nPrint request for mac\nPrinting by a HP Printer\n\nPrint request for mac\nPrinting by a EPSON Printer\n\nPrint request for windows\nPrinting by a HP Printer\n\nPrint request for windows<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Full Working Code:<\/strong><\/span><\/h2>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype computer interface {\n print()\n setPrinter(printer)\n}\n\ntype mac struct {\n printer printer\n}\n\nfunc (m *mac) print() {\n fmt.Println(\"Print request for mac\")\n m.printer.printFile()\n}\n\nfunc (m *mac) setPrinter(p printer) {\n m.printer = p\n}\n\ntype windows struct {\n printer printer\n}\n\nfunc (w *windows) print() {\n fmt.Println(\"Print request for windows\")\n w.printer.printFile()\n}\n\nfunc (w *windows) setPrinter(p printer) {\n w.printer = p\n}\n\ntype printer interface {\n printFile()\n}\n\ntype epson struct {\n}\n\nfunc (p *epson) printFile() {\n fmt.Println(\"Printing by a EPSON Printer\")\n}\n\ntype hp struct {\n}\n\nfunc (p *hp) printFile() {\n fmt.Println(\"Printing by a HP Printer\")\n}\n\nfunc main() {\n hpPrinter := &hp{}\n epsonPrinter := &epson{}\n macComputer := &mac{}\n macComputer.setPrinter(hpPrinter)\n macComputer.print()\n fmt.Println()\n macComputer.setPrinter(epsonPrinter)\n macComputer.print()\n fmt.Println()\n winComputer := &windows{}\n winComputer.setPrinter(hpPrinter)\n winComputer.print()\n fmt.Println()\n winComputer.setPrinter(epsonPrinter)\n winComputer.print()\n fmt.Println()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nPrint request for mac\nPrinting by a HP Printer\n\nPrint request for mac\nPrinting by a EPSON Printer\n\nPrint request for windows\nPrinting by a HP Printer\n\nPrint request for windows<\/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 – All Design Patterns in Go (Golang) Table of Contents Introduction:UML Diagram:Mapping Practical ExampleFull…<\/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":[73,25,3,4],"class_list":["post-603","post","type-post","status-publish","format-standard","hentry","category-tech","tag-bridge-design-pattern","tag-design-pattern","tag-go","tag-golang"],"yoast_head":"\n
Bridge 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