<\/span><\/h2>\n\n\n\nThis is a structural design pattern. Composition design pattern is used when we want a Group of objects called ‘composite’ is treated in a similar way as a single object. It comes under structural design pattern as it allows you to compose objects into a tree structure. Each of the individual objects in the tree structure can be treated in the same way irrespective of whether they are Complex or Primitive. Let’s try to understand it with an example of a file system of OS. In the file system, there are two types of objects File<\/strong> and Folder. <\/strong>There are cases when File and Folder are treated to be the same way. It will be more clear as we go along.<\/p>\n\n\n\n<\/span>When to Use<\/strong><\/span><\/h2>\n\n\n\nComposite Design pattern makes sense to use in cases when the composite and individual object needs to be treated in the same way from a client perspective. <\/li><\/ul>\n\n\n\n – In our example above of the file system, let’s say search operation of a particular keyword needs to be executed. Now, this search operation applies to both File <\/strong>and Folder. <\/strong>For a File,<\/strong> it will just look into the contents of the file and for a Folder,<\/strong> it will go through all files in the hierarchy in that folder to find that keyword<\/p>\n\n\n\nUse this pattern when the composite and individual object form a tree-like structure<\/li><\/ul>\n\n\n\n – In our example, File <\/strong>and Folder <\/strong>do form a tree structure <\/p>\n\n\n\n<\/span>UML Diagram<\/strong><\/span><\/h2>\n\n\n\nComponent<\/strong> – It is the interface which defines the common operations for both the Composite<\/strong> and Leaf<\/strong> objects<\/li>Composite – <\/strong>It implements Component<\/strong> interface and embeds an array of child Components<\/strong><\/li>Leaf – <\/strong>it is the primitive object in the tree. It also implements the Component<\/strong> Interface<\/li><\/ul>\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 <\/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\nComponent interface<\/td> component.go<\/td><\/tr> Composite<\/td> folder.go<\/td><\/tr> Leaf<\/td> file.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\nIn the <\/strong>example below component<\/strong> is the interface and file<\/strong> and folder<\/strong> implement this interface.<\/strong><\/p>\n\n\n\ncomponent.go<\/strong><\/p>\n\n\n\npackage main\n\ntype component interface {\n search(string)\n}<\/code><\/pre>\n\n\n\nfolder.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype folder struct {\n components []component\n name string\n}\n\nfunc (f *folder) search(keyword string) {\n fmt.Printf(\"Serching recursively for keyword %s in folder %s\\n\", keyword, f.name)\n for _, composite := range f.components {\n composite.search(keyword)\n }\n}\n\nfunc (f *folder) add(c component) {\n f.components = append(f.components, c)\n}<\/code><\/pre>\n\n\n\nfile.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype file struct {\n name string\n}\n\nfunc (f *file) search(keyword string) {\n fmt.Printf(\"Searching for keyword %s in file %s\\n\", keyword, f.name)\n}\n\nfunc (f *file) getName() string {\n return f.name\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nfunc main() {\n file1 := &file{name: \"File1\"}\n file2 := &file{name: \"File2\"}\n file3 := &file{name: \"File3\"}\n folder1 := &folder{\n name: \"Folder1\",\n }\n folder1.add(file1)\n folder2 := &folder{\n name: \"Folder2\",\n }\n folder2.add(file2)\n folder2.add(file3)\n folder2.add(folder1)\n folder2.search(\"rose\")\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nSerching recursively for keyword rose in folder Folder2\nSearching for keyword rose in file File2\nSearching for keyword rose in file File3\nSerching recursively for keyword rose in folder Folder1\nSearching for keyword rose in file File1<\/code><\/pre>\n\n\n\n<\/p>\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":[52,53,54,25,3,4],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-tech","tag-composite-design-pattern","tag-composite-design-pattern-in-go","tag-composite-design-pattern-in-golang","tag-design-pattern","tag-go","tag-golang"],"yoast_head":"\n
Composite 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