The Composite pattern is intended to allow you to compose tree structures to represent whole-part hierarchies so that clients can treat individual objects and compositions of objects uniformly.

image

When I hear "composite pattern", immediately XML pops up in my mind. A composite is an element and a leaf is an attribute.

The client creates the root of the tree and adds composites and leafs. Next an operation can be performed on the complete tree by calling the method on the root. Thereafter the operation is ‘automagically’ called recursively.

The key of this pattern is the abstract class Component, which is the parent of the composite object and leaf. It defines a property and an operation. It also has methods for adding and removing objects from the tree. These methods are not necessary, but – indeed – very convenient.

The composite is the concrete class which implements all methods of the abstract parent class, thus can contain another composite or leaf.

The leaf, on the other hand, is a crippled implementation of the abstract parent class. It cannot contain other parts, it is an endpoint.

In this example, a xml-structure is created and the method Operation is use to display itself. Therefore, Display() would be a better name. Maybe we should refactor it.

This post is part of a series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern by looking at a minimalistic example to reveal its structure and then look at more concrete and useful real-world C# code that implements the pattern.