adidas<\/strong><\/li><\/ul>\n\n\n\nImagine you need to buy a sports kit which has a shoe<\/strong> and short<\/strong>. Preferably most of the time you would want to buy a full sports kit of a similar factory i.e either nike <\/strong>or adidas. <\/strong>This is where the abstract factory comes into the picture as concrete products that you want is shoe <\/strong>and a short <\/strong>and these products will be created by the abstract factory of nike <\/strong>and adidas.<\/strong>
Both these two factories – nike <\/strong>and adidas<\/strong> implement iSportsFactory<\/strong> interface.
We have two product interfaces.<\/p>\n\n\n\n- iShoe<\/strong> – this interface is implemented by nikeShoe<\/strong> and adidasShoe<\/strong> concrete product.<\/li>
- iShort<\/strong> – this interface is implemented by nikeShort<\/strong> and adidasShort<\/strong> concrete product.<\/li><\/ul>\n\n\n\n
Now let’s look at code<\/p>\n\n\n\n
<\/span>Code:<\/strong><\/span><\/h2>\n\n\n\niSportsFactory.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype iSportsFactory interface {\n makeShoe() iShoe\n makeShort() iShort\n}\n\nfunc getSportsFactory(brand string) (iSportsFactory, error) {\n if brand == \"adidas\" {\n return &adidas{}, nil\n }\n if brand == \"nike\" {\n return &nike{}, nil\n }\n return nil, fmt.Errorf(\"Wrong brand type passed\")\n}<\/code><\/pre>\n\n\n\nadidas.go<\/strong><\/p>\n\n\n\npackage main\n\ntype adidas struct {\n}\n\nfunc (a *adidas) makeShoe() iShoe {\n return &adidasShoe{\n shoe: shoe{\n logo: \"adidas\",\n size: 14,\n },\n }\n}\n\nfunc (a *adidas) makeShort() iShort {\n return &adidasShort{\n short: short{\n logo: \"adidas\",\n size: 14,\n },\n }\n}\n<\/code><\/pre>\n\n\n\nnike.go<\/strong><\/p>\n\n\n\npackage main\n\ntype nike struct {\n}\n\nfunc (n *nike) makeShoe() iShoe {\n return &nikeShoe{\n shoe: shoe{\n logo: \"nike\",\n size: 14,\n },\n }\n}\n\nfunc (n *nike) makeShort() iShort {\n return &nikeShort{\n short: short{\n logo: \"nike\",\n size: 14,\n },\n }\n}<\/code><\/pre>\n\n\n\niShoe.go<\/strong><\/p>\n\n\n\npackage main\n\ntype iShoe interface {\n setLogo(logo string)\n setSize(size int)\n getLogo() string\n getSize() int\n}\n\ntype shoe struct {\n logo string\n size int\n}\n\nfunc (s *shoe) setLogo(logo string) {\n s.logo = logo\n}\n\nfunc (s *shoe) getLogo() string {\n return s.logo\n}\n\nfunc (s *shoe) setSize(size int) {\n s.size = size\n}\n\nfunc (s *shoe) getSize() int {\n return s.size\n}\n<\/code><\/pre>\n\n\n\nadidasShoe.go<\/strong><\/p>\n\n\n\npackage main\n\ntype adidasShoe struct {\n\tshoe\n}\n<\/code><\/pre>\n\n\n\nnikeShoe.go<\/strong><\/p>\n\n\n\npackage main\n\ntype nikeShoe struct {\n shoe\n}<\/code><\/pre>\n\n\n\niShort.go<\/strong><\/p>\n\n\n\npackage main\n\ntype iShort interface {\n setLogo(logo string)\n setSize(size int)\n getLogo() string\n getSize() int\n}\n\ntype short struct {\n logo string\n size int\n}\n\nfunc (s *short) setLogo(logo string) {\n s.logo = logo\n}\n\nfunc (s *short) getLogo() string {\n return s.logo\n}\n\nfunc (s *short) setSize(size int) {\n s.size = size\n}\n\nfunc (s *short) getSize() int {\n return s.size\n}<\/code><\/pre>\n\n\n\nadidasShort.go<\/strong><\/p>\n\n\n\npackage main\n\ntype adidasShort struct {\n short\n}<\/code><\/pre>\n\n\n\nnikeShort.go<\/strong><\/p>\n\n\n\npackage main\n\ntype nikeShort struct {\n short\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n adidasFactory, _ := getSportsFactory(\"adidas\")\n nikeFactory, _ := getSportsFactory(\"nike\")\n nikeShoe := nikeFactory.makeShoe()\n nikeShort := nikeFactory.makeShort()\n adidasShoe := adidasFactory.makeShoe()\n adidasShort := adidasFactory.makeShort()\n printShoeDetails(nikeShoe)\n printShortDetails(nikeShort)\n printShoeDetails(adidasShoe)\n printShortDetails(adidasShort)\n}\n\nfunc printShoeDetails(s iShoe) {\n fmt.Printf(\"Logo: %s\", s.getLogo())\n fmt.Println()\n fmt.Printf(\"Size: %d\", s.getSize())\n fmt.Println()\n}\n\nfunc printShortDetails(s iShort) {\n fmt.Printf(\"Logo: %s\", s.getLogo())\n fmt.Println()\n fmt.Printf(\"Size: %d\", s.getSize())\n fmt.Println()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nLogo: nike\nSize: 14\nLogo: nike\nSize: 14\nLogo: adidas\nSize: 14\nLogo: adidas\nSize: 14<\/code><\/pre>\n\n\n\n<\/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 Definition:Code: Definition: Abstract…<\/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":[],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\n
Abstract Factory Design Pattern in Go - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n