{"id":1152,"date":"2020-06-05T00:06:58","date_gmt":"2020-06-04T14:06:58","guid":{"rendered":"http:\/\/www.moneystock.net\/wp_e\/?p=1152"},"modified":"2020-06-05T00:37:43","modified_gmt":"2020-06-04T14:37:43","slug":"design-pattern-composite","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/","title":{"rendered":"Design Pattern &#8211; Composite"},"content":{"rendered":"<p>Let&#8217;s refactor further the previous code sample using the composite pattern.<\/p>\n<p>See the link(https:\/\/www.dofactory.com\/net\/composite-design-pattern) for the details of what is a composite pattern.<\/p>\n<p>Let&#8217;s bring back the last code here.<\/p>\n<pre class=\"prettyprint\">                var availableCommands = new ICommand[]\r\n                {\r\n                    new UpdatePrice(_stockPriceManager, stocksToRead, _appSettings.DaysToReadFromWeb),\r\n                    new UpdateStats(_stockPriceManager, stocksToAnalyze),\r\n                    new SimulateCommand(_stockPriceManager, stocksToAnalyze, _marketWatchRepository, investDay,\r\n                        _tradeOption, _logger, _appSettings)\r\n                };\r\n\r\n                if (runOption == \"all\")\r\n                {\r\n                    availableCommands.Select(async c =&gt; await c.Execute());\r\n                }\r\n                else\r\n                {\r\n                    var command = availableCommands.Single(c =&gt; c.Name == runOption);\r\n                    await command.Execute();\r\n                }<\/pre>\n<p>&nbsp;<\/p>\n<p>In the previous sample, all individual commands executed by comparing the command name. However, we had to manually compare the string &#8220;all&#8221; in the if-else statement.<\/p>\n<p>By implementing the composite pattern, we can remove if-else entirely as below. Instead, we can create an AllCommands object, which is an implementation of ICommand. and add that group of command objects (allCommands) into the availableCommands variable.<\/p>\n<pre class=\"prettyprint\">                var availableCommands = new List&lt;ICommand&gt;\r\n                {\r\n                    new UpdatePrice(_stockPriceManager, stocksToRead, _appSettings.DaysToReadFromWeb),\r\n                    new UpdateStats(_stockPriceManager, stocksToAnalyze),\r\n                    new SimulateCommand(_stockPriceManager, stocksToAnalyze, _marketWatchRepository, investDay,\r\n                        _tradeOption, _logger, _appSettings)\r\n                };\r\n\r\n                var allCommands = new AllCommands()\r\n                {\r\n                    commands = availableCommands\r\n                };\r\n\r\n                availableCommands.Add(allCommands);\r\n\r\n                var command = availableCommands.Single(c =&gt; c.Name == commandName);\r\n                await command.Execute();<\/pre>\n<p>&nbsp;<\/p>\n<p>The key concept is making a group of individual commands as the same interface to the individual command so as to treat a group of commands as a command. Potentially, we can add a group of the group easily using this composite pattern otherwise the original if-else code could have been very messy.<\/p>\n<p>The AllCommands class looks like below. As explained, the key is having a list of ICommand inside of ICommand implementation.<\/p>\n<pre class=\"prettyprint\">    public class AllCommands : ICommand\r\n    {\r\n        public List&lt;ICommand&gt; commands { get; set; }\r\n        public string Name =&gt; \"All\";\r\n\r\n        public Task Execute()\r\n        {\r\n            commands.ForEach(async c =&gt; await c.Execute());\r\n\r\n            return Task.CompletedTask;\r\n        }\r\n    }<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s refactor further the previous code sample using the composite pattern. See the link(https:\/\/www.dofactory.com\/net\/composite-design-pattern) for the details of what is a composite pattern. Let&#8217;s bring back the last code here. var availableCommands = new ICommand[] { new UpdatePrice(_stockPriceManager, stocksToRead, _appSettings.DaysToReadFromWeb), new UpdateStats(_stockPriceManager, stocksToAnalyze), new SimulateCommand(_stockPriceManager, stocksToAnalyze, _marketWatchRepository, investDay, _tradeOption, _logger, _appSettings) }; if (runOption ==&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/\">Continue reading <span class=\"screen-reader-text\">Design Pattern &#8211; Composite<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[548],"tags":[573,571],"class_list":["post-1152","post","type-post","status-publish","format-standard","hentry","category-c","tag-composite-pattern","tag-design-pattern","entry"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"rocker8942\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Joe&#039;s Happy Life | Be positive, have fun\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Design Pattern \u2013 Composite | Joe&#039;s Happy Life\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-06-04T14:06:58+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-06-04T14:37:43+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Design Pattern \u2013 Composite | Joe&#039;s Happy Life\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#article\",\"name\":\"Design Pattern \\u2013 Composite | Joe's Happy Life\",\"headline\":\"Design Pattern &#8211; Composite\",\"author\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#organization\"},\"datePublished\":\"2020-06-05T00:06:58+08:00\",\"dateModified\":\"2020-06-05T00:37:43+08:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#webpage\"},\"articleSection\":\"C#, composite pattern, design pattern, English\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/moneystock.net\\\/wp_e\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/c\\\/#listItem\",\"name\":\"C#\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/c\\\/#listItem\",\"position\":2,\"name\":\"C#\",\"item\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#listItem\",\"name\":\"Design Pattern &#8211; Composite\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#listItem\",\"position\":3,\"name\":\"Design Pattern &#8211; Composite\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/c\\\/#listItem\",\"name\":\"C#\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#organization\",\"name\":\"Joe's Happy Life\",\"description\":\"Be positive, have fun\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/\",\"name\":\"rocker8942\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d00fcc2c7b0eaab40443e28428a4429ab4a95e12086262d902988f448d76b128?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"rocker8942\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#webpage\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/\",\"name\":\"Design Pattern \\u2013 Composite | Joe's Happy Life\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/06\\\/05\\\/design-pattern-composite\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\"},\"datePublished\":\"2020-06-05T00:06:58+08:00\",\"dateModified\":\"2020-06-05T00:37:43+08:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#website\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/\",\"name\":\"Joe's Happy Life\",\"description\":\"Be positive, have fun\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Design Pattern \u2013 Composite | Joe's Happy Life","description":"","canonical_url":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#article","name":"Design Pattern \u2013 Composite | Joe's Happy Life","headline":"Design Pattern &#8211; Composite","author":{"@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author"},"publisher":{"@id":"https:\/\/moneystock.net\/wp_e\/#organization"},"datePublished":"2020-06-05T00:06:58+08:00","dateModified":"2020-06-05T00:37:43+08:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#webpage"},"isPartOf":{"@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#webpage"},"articleSection":"C#, composite pattern, design pattern, English"},{"@type":"BreadcrumbList","@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e#listItem","position":1,"name":"Home","item":"https:\/\/moneystock.net\/wp_e","nextItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/c\/#listItem","name":"C#"}},{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/c\/#listItem","position":2,"name":"C#","item":"https:\/\/moneystock.net\/wp_e\/category\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#listItem","name":"Design Pattern &#8211; Composite"},"previousItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#listItem","position":3,"name":"Design Pattern &#8211; Composite","previousItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/c\/#listItem","name":"C#"}}]},{"@type":"Organization","@id":"https:\/\/moneystock.net\/wp_e\/#organization","name":"Joe's Happy Life","description":"Be positive, have fun","url":"https:\/\/moneystock.net\/wp_e\/"},{"@type":"Person","@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author","url":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/","name":"rocker8942","image":{"@type":"ImageObject","@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d00fcc2c7b0eaab40443e28428a4429ab4a95e12086262d902988f448d76b128?s=96&d=mm&r=g","width":96,"height":96,"caption":"rocker8942"}},{"@type":"WebPage","@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#webpage","url":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/","name":"Design Pattern \u2013 Composite | Joe's Happy Life","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/moneystock.net\/wp_e\/#website"},"breadcrumb":{"@id":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/#breadcrumblist"},"author":{"@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author"},"creator":{"@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author"},"datePublished":"2020-06-05T00:06:58+08:00","dateModified":"2020-06-05T00:37:43+08:00"},{"@type":"WebSite","@id":"https:\/\/moneystock.net\/wp_e\/#website","url":"https:\/\/moneystock.net\/wp_e\/","name":"Joe's Happy Life","description":"Be positive, have fun","inLanguage":"en-US","publisher":{"@id":"https:\/\/moneystock.net\/wp_e\/#organization"}}]},"og:locale":"en_US","og:site_name":"Joe's Happy Life | Be positive, have fun","og:type":"article","og:title":"Design Pattern \u2013 Composite | Joe's Happy Life","og:url":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/","article:published_time":"2020-06-04T14:06:58+00:00","article:modified_time":"2020-06-04T14:37:43+00:00","twitter:card":"summary","twitter:title":"Design Pattern \u2013 Composite | Joe's Happy Life"},"aioseo_meta_data":{"post_id":"1152","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-04-24 04:50:01","updated":"2025-06-03 23:53:29","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/moneystock.net\/wp_e\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/moneystock.net\/wp_e\/category\/c\/\" title=\"C#\">C#<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tDesign Pattern \u2013 Composite\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/moneystock.net\/wp_e"},{"label":"C#","link":"https:\/\/moneystock.net\/wp_e\/category\/c\/"},{"label":"Design Pattern &#8211; Composite","link":"https:\/\/moneystock.net\/wp_e\/2020\/06\/05\/design-pattern-composite\/"}],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=1152"}],"version-history":[{"count":8,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1152\/revisions"}],"predecessor-version":[{"id":1160,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1152\/revisions\/1160"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=1152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=1152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=1152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}