<\/span><\/h2>\n\n\n\nIt is a structural design pattern. This pattern is used when a large number of similar objects need to be created. These objects are called flyweight objects and are immutable. <\/p>\n\n\n\n
Let’s first see an example. Flyweight Pattern will be clear after this example.<\/p>\n\n\n\n
In a game of Counter-Strike, Terrorist and Counter-Terrorist have a different type of dress. For simplicity, let’s assume that both Terrorist and Counter-Terrorists have one dress type each. The dress object is embedded in the player object as below<\/p>\n\n\n\n
Below is the struct for a player, we can see the dress object is embedded in player struct<\/p>\n\n\n\n
type player struct {\n dress dress\n playerType string \/\/Can be T or CT\n lat int\n long int\n}<\/code><\/pre>\n\n\n\nLet’s say there are 5 Terrorists and 5 Counter-Terrorist, so a total of 10 players. Now there are two options with respect to dress<\/p>\n\n\n\n
Each of the 10 player objects creates a different dress object and embed them. Total 10 dress objects will be created<\/li> We create two dress objectSingle Terrorist Dress Object: This will be shared across 5 Terrorist<\/li> Single Counter-Terrorist Dress Object: This will be shared across 5 Counter-Terrorist<\/li><\/ul><\/li><\/ol>\n\n\n\nAs you can that in Approach 1, total of 10 dress objects are created while in approach 2 only 2 dress objects are created. The second approach is what we follow in the Flyweight design pattern. The two dress objects which we created are called the flyweight objects. Flyweight pattern takes out the common parts and creates flyweight objects. These flyweight objects (dress here) can then be shared among multiple objects (player here). This drastically reduces the number of dress objects and the good part is that even if you create more players, still only two dress objects will be sufficient. <\/p>\n\n\n\n
In the flyweight pattern, we store the flyweight objects in the map. Whenever the other objects which share the flyweight objects are created, then flyweight objects are fetched from the map.<\/p>\n\n\n\n
Intrinsic and Extrinsic States<\/strong><\/p>\n\n\n\nIntrinsic State –<\/strong>Dress in the intrinsic state as it can be shared across multiple Terrorist and Counter-Terrorist Objects<\/li>Extrinsic State – <\/strong>Player location and the player weapon is an extrinsic state as it is different for every object.<\/li><\/ul>\n\n\n\n<\/span>When to Use<\/strong>:<\/span><\/h2>\n\n\n\nWhen the objects have some intrinsic properties which can be shared. As in the above example, dress is the intrinsic property that was taken out and shared.<\/li><\/ul><\/li> Use flyweight when a large number of objects needs to be created which can cause memory issue. In case figure out all the common or intrinsic state and create flyweight objects for that. <\/li><\/ul>\n\n\n\n<\/span>UML Diagram<\/strong>:<\/span><\/h2>\n\n\n\n <\/figure>\n\n\n\nBelow is the corresponding mapping UML diagram with the example given above<\/p>\n\n\n\n
<\/p>\n\n\n\n <\/figure>\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 code.<\/p>\n\n\n\nFlyweight Factory<\/td> dressFactory.go<\/td><\/tr> Flyweight Interface<\/td> dress.go<\/td><\/tr> Concrete Flyweight Object 1<\/td> terroristDress.go<\/td><\/tr> Concrete Flyweight Object 1<\/td> counterTerroristDress.go<\/td><\/tr> Context<\/td> player.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\ndressFactory.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nconst (\n \/\/TerroristDressType terrorist dress type\n TerroristDressType = \"tDress\"\n \/\/CounterTerrroristDressType terrorist dress type\n CounterTerrroristDressType = \"ctDress\"\n)\n\nvar (\n dressFactorySingleInstance = &dressFactory{\n dressMap: make(map[string]dress),\n }\n)\n\ntype dressFactory struct {\n dressMap map[string]dress\n}\n\nfunc (d *dressFactory) getDressByType(dressType string) (dress, error) {\n if d.dressMap[dressType] != nil {\n return d.dressMap[dressType], nil\n }\n if dressType == TerroristDressType {\n d.dressMap[dressType] = newTerroristDress()\n return d.dressMap[dressType], nil\n }\n if dressType == CounterTerrroristDressType {\n d.dressMap[dressType] = newCounterTerroristDress()\n return d.dressMap[dressType], nil\n }\n return nil, fmt.Errorf(\"Wrong dress type passed\")\n}\n\nfunc getDressFactorySingleInstance() *dressFactory {\n return dressFactorySingleInstance\n}<\/code><\/pre>\n\n\n\ndress.go<\/strong><\/p>\n\n\n\npackage main\n\ntype dress interface {\n getColor() string\n}<\/code><\/pre>\n\n\n\nterroristDress.go<\/strong><\/p>\n\n\n\npackage main\n\ntype terroristDress struct {\n\tcolor string\n}\n\nfunc (t *terroristDress) getColor() string {\n\treturn t.color\n}\n\nfunc newTerroristDress() *terroristDress {\n\treturn &terroristDress{color: \"red\"}\n}\n<\/code><\/pre>\n\n\n\ncounterTerroristDress.go<\/strong><\/p>\n\n\n\npackage main\n\ntype counterTerroristDress struct {\n color string\n}\n\nfunc (c *counterTerroristDress) getColor() string {\n return c.color\n}\n\nfunc newCounterTerroristDress() *counterTerroristDress {\n return &counterTerroristDress{color: \"green\"}\n}<\/code><\/pre>\n\n\n\nplayer.go<\/strong><\/p>\n\n\n\npackage main\n\ntype player struct {\n dress dress\n playerType string\n lat int\n long int\n}\n\nfunc newPlayer(playerType, dressType string) *player {\n dress, _ := getDressFactorySingleInstance().getDressByType(dressType)\n return &player{\n playerType: playerType,\n dress: dress,\n }\n}\n\nfunc (p *player) newLocation(lat, long int) {\n p.lat = lat\n p.long = long\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n game := newGame()\n \/\/Add Terrorist\n game.addTerrorist(TerroristDressType)\n game.addTerrorist(TerroristDressType)\n game.addTerrorist(TerroristDressType)\n game.addTerrorist(TerroristDressType)\n \/\/Add CounterTerrorist\n game.addCounterTerrorist(CounterTerrroristDressType)\n game.addCounterTerrorist(CounterTerrroristDressType)\n game.addCounterTerrorist(CounterTerrroristDressType)\n dressFactoryInstance := getDressFactorySingleInstance()\n for dressType, dress := range dressFactoryInstance.dressMap {\n fmt.Printf(\"DressColorType: %s\\nDressColor: %s\\n\", dressType, dress.getColor())\n }\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nDressColorType: ctDress\nDressColor: green\nDressColorType: tDress\nDressColor: red<\/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: When to…<\/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":[25,55,3],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-tech","tag-design-pattern","tag-flyweight-design-pattern","tag-go"],"yoast_head":"\n
Flyweight 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