• Albert Bennett

Design Patterns: Composite

Updated: Jul 12, 2021

Here is the link to the GitHub repo :)

Feel free to like, comment and share this post.

So... trees essentially. Using this pattern we structure our object like a... tree. Where our composite is the root node and leafs are child nodes. It's great for when you need to define a hierarchy of objects. Here is a diagram of how it works:

Component: This class defines the basic behavior of all elements in the structure. Components can have many (children). The primary function of a Component is to manage it's children.

Leaf: This class inherits from component. It signifies the end of a branch, as in it can't have any children. The leaf class is where the actions would usually occur.

Composition: This is our implementation of the structure. It is the concrete implementation of our component class.

This is an example of how the structure can be implemented. As you can see a Composite is used for out root and it is added as a child later on.

I hope that this helps you to understand how the Composite pattern works.

<< More Design Patterns

4 views0 comments

Recent Posts

See All