avatarSebastian Streng

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1884

Abstract

f whether it is a single leaf or a composite node. This makes it easier to process tree structures and reduces the code required for different actions performed on the tree structures.</p><h1 id="7204">Structure</h1><p id="254f">In a Composite pattern, <b>composites can also be used as leaf components.</b> This allows for the creation of a hierarchy of arbitrary depth and complexity, where a leaf node can consist of either a simple leaf component or a composite component.</p><figure id="c48d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*19XasfnhHt7obDCgX-CsfA.png"><figcaption></figcaption></figure><p id="bc6e">Usually, a leaf component is considered a simple component that has no children and cannot add or remove any further components. A composite component, on the other hand, can contain both leaf components and other composite components. This creates a tree structure that can contain both simple and complex components.</p><p id="fe4b"><b>So Yes: </b>A Composite node can exclusively have other Composite components as children. It is quite common for Composite components to consist only of other Composite components and not directly contain Leaf components.</p><p id="e81a"><b>And Yes: </b> A composite node can have any number of other composites or leafs.</p><h1 id="7080">UML Example</h1><figure id="f5b0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0HTFdXb0r9czcgkXIna7Yw.png"><figcaption></figcaption></figure><h1 id="b075">Implementation-Example</h1><figure id="2da6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ahcCkwffyLUgJsXBu96pdg.png"><figcaption></figcaption></figure><h1 id="c91d">C#-Example</h1> <figure id="e44f"> <div> <div>

            <iframe class="gist-iframe" src="/gist/SebastianStreng/e6fb82cf6edb99a4ca0fe11a507dccb5.js"

Options

allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><h1 id="9994">Other articles:</h1><p id="5c52">Thanks for reading and if you like this Article, follow me for more ! :)</p><p id="2516">Please check out my other articles:</p><ul><li><a href="https://readmedium.com/linq-how-to-avoid-nested-loops-in-c-ed4ae19886e4">LINQ — How to avoid nested loops in C#</a></li><li><a href="https://readmedium.com/net-c-clean-architecture-dependency-inversion-principle-7ea64f586c58">.Net C# — Clean Architecture & Dependency-Inversion-Principle</a></li><li><a href="https://readmedium.com/extension-methods-in-c-explained-1553bad918f3">Extension Methods in C# explained</a></li><li><a href="https://readmedium.com/programming-paradigms-a-very-short-brief-5324908640bd">Programming paradigms — a brief introduction</a></li><li><a href="https://readmedium.com/c-object-oriented-programming-oop-2d92a5cd336f">C# — Object-Oriented Programming (OOP)</a></li><li><a href="https://readmedium.com/c-single-responsibility-easily-explained-e3fabbf0d877">C# — Single-Responsibility easily explained</a></li><li><a href="https://readmedium.com/ocp-what-really-matters-610159d600dc">OCP — What really matters</a></li><li><a href="https://readmedium.com/top-8-tips-to-improve-your-motivation-as-programmer-be63b2baaf7e">Top 8 tips to improve your motivation as programmer</a></li><li><a href="https://readmedium.com/a-brief-insight-into-networks-2171f1e9aea1">A brief insight into Networks</a></li><li><a href="https://readmedium.com/send-receive-the-7-layer-osi-model-e475829b999">Send & Receive — The 7-Layer-OSI-Model</a></li><li><a href="https://readmedium.com/7-layer-network-protocols-easily-explained-e11e3e09f34d">7-Layer Network Protocols easily explained</a></li></ul></article></body>

Wizards´ Design Patterns — The Composite Pattern in C#

This article is about the Composite Pattern and explained with examples from the wonderful world of wizards and witches.

https://unsplash.com/de/fotos/HAl6CKxM3xU

Table of Contents

  • Basic Information about Composite Pattern
  • When to use Composite Pattern
  • Structure
  • UML-Example
  • Implementation-Example
  • C# Example
  • Other Articles

Basic Information about Composite Pattern

First off all: the goal with this pattern is for a class to treat all subclasses and itself exactly the same.

If one wants to create part-whole hierarchies and generate objects in a tree structure, the Composite Pattern is just the right thing. This basically involves inheritance, with the addition that a class that inherits from the superclass also stands higher in the hierarchy by calling the parent class. Thus, there are 3 different areas:

  • The Component Class (Parent; abstract class or Interface)
  • The Composite Class (inherits from Parent)
  • The Leaf Class (inherits from Parent)

A typical example of this is the representation of hierarchical structures such as file systems, directories, menus and submenus, organizational structures, or the hierarchical representation of categories. The Composite Pattern allows organizing objects in a tree structure and accessing them uniformly. It defines a common interface for all elements in the tree, regardless of whether it is a single leaf or a composite node. This makes it easier to process tree structures and reduces the code required for different actions performed on the tree structures.

Structure

In a Composite pattern, composites can also be used as leaf components. This allows for the creation of a hierarchy of arbitrary depth and complexity, where a leaf node can consist of either a simple leaf component or a composite component.

Usually, a leaf component is considered a simple component that has no children and cannot add or remove any further components. A composite component, on the other hand, can contain both leaf components and other composite components. This creates a tree structure that can contain both simple and complex components.

So Yes: A Composite node can exclusively have other Composite components as children. It is quite common for Composite components to consist only of other Composite components and not directly contain Leaf components.

And Yes: A composite node can have any number of other composites or leafs.

UML Example

Implementation-Example

C#-Example

Other articles:

Thanks for reading and if you like this Article, follow me for more ! :)

Please check out my other articles:

Dotnet
Programming
Coding
Software Development
Software Engineering
Recommended from ReadMedium