{"id":2183,"date":"2019-09-16T23:45:51","date_gmt":"2019-09-16T20:45:51","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2183"},"modified":"2024-12-16T22:32:34","modified_gmt":"2024-12-16T19:32:34","slug":"composite-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2019\/09\/16\/composite-pattern\/","title":{"rendered":"Pattern Composite"},"content":{"rendered":"<p>The Composite pattern is a structural design pattern; in domestic sources it is known as the &#8220;Composer&#8221;.<br \/>Let&#8217;s say we are developing an application &#8211; a photo album. The user can create folders, add photos there, and perform other manipulations. It is necessary to be able to show the number of files in folders, the total number of all files and folders.<br \/>It is obvious that you need to use a tree, but how to implement the tree architecture, with a simple and convenient interface? The Composite pattern comes to the rescue.<\/p>\n<p><a href=\"https:\/\/www.flickr.com\/photos\/8586177@N03\/5075169756\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2185\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/09\/tree-1.jpg\" alt=\"\" width=\"400\" height=\"423\" \/><\/a><\/p>\n<div class=\"attribution-info\">\n<p><a class=\"owner-name truncate\" title=\"Go to Sheila in Moonducks's photostream\" href=\"https:\/\/www.flickr.com\/photos\/aspis7\/\" target=\"_blank\" rel=\"noopener noreferrer\" data-track=\"attributionNameClick\">Sheila in Moonducks<\/a><\/p>\n<div id=\"yui_3_16_0_1_1568665128454_889\" class=\"view follow-view clear-float photo-attribution\">We implement the Component interface with the dataCount() method we need, through it we will return the number of files\/directories. We will create a Directory class with an interface that allows adding\/removing instances of classes implementing the Component interface, according to the pattern scheme this is Composite. We will also create a File class, where we store bytes with a photo, inherit from Component, through dataCount we return 1, like there is one photo!<\/div>\n<div>Next, in Directory we implement the dataCount() method &#8211; by going through all the elements in the component array, adding up all their dataCount&#8217;s.<\/div>\n<div>Everything is ready!<\/div>\n<div>Below is an example in Go:<\/div>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>package main\n\nimport \"fmt\"\n\ntype component interface {\n\ndataCount() int\n\n}\n\ntype file struct {\n\n}\n\ntype directory struct {\n\nc []component\n\n}\n\nfunc (f file) dataCount() int {\n\nreturn 1\n\n}\n\nfunc (d directory) dataCount() int {\n\nvar outputDataCount int = 0\n\nfor _, v := range d.c {\noutputDataCount += v.dataCount()\n}\n\nreturn outputDataCount\n\n}\n\nfunc (d *directory) addComponent(c component) {\n\nd.c = append(d.c, c)\n\n}\n\nfunc main() {\n\nvar f file\nvar rd directory\nrd.addComponent(f)\nrd.addComponent(f)\nrd.addComponent(f)\nrd.addComponent(f)\n\nfmt.Println(rd.dataCount())\n\nvar sd directory\nsd.addComponent(f)\n\nrd.addComponent(sd)\nrd.addComponent(sd)\nrd.addComponent(sd)\n\nfmt.Println(sd.dataCount())\nfmt.Println(rd.dataCount())\n\n}\n<\/code><\/pre>\n<\/div>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/composite\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/refactoring.guru\/ru\/design-patterns\/ composite<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Composite pattern is a structural design pattern; in domestic sources it is known as the &#8220;Composer&#8221;.Let&#8217;s say we are developing an application &#8211; a photo album. The user can create folders, add photos there, and perform other manipulations. It is necessary to be able to show the number of files in folders, the total<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2019\/09\/16\/composite-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Pattern Composite&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[116,95],"class_list":["post-2183","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-composite","tag-patterns","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=2183"}],"version-history":[{"count":11,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2183\/revisions"}],"predecessor-version":[{"id":3944,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2183\/revisions\/3944"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=2183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=2183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=2183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}