
Episode 9 · Season 5
Software Design - The Composite Pattern
About this episode
The Composite Pattern is one that is not taken advantage of as often as it might outside of frameworks. This is an approach to design that allows you to treat a family of objects the same. It is a quintessential reason for and use of an interface. The flexibility is critical to being able to handle groups of different objects the same way any individual one would be treated.
The Composite Pattern Defined
As always, we will start with the "Gang of Four" intent to set the stage for our discussion.
"Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly."
I think it may be most comfortable to focus on the second sentence in defining this pattern. The tree structure feels a bit like an implementation detail rather than an abstract pattern. The tree structure reference is often (if not always) seen in utilizing this pattern. It usually breaks down to a concept of a container that holds other containers or primitives. Graphics are a perfect example. You have pixels, but then those can be combined into shapes or other container structures.
Applying The Pattern
The key to this pattern is an interface or top-level class. It is more flexible to use an interface for this implementation, but you may find cases where a top-level class reduces code and allows for re-use. The objects that implement the interface will either perform the action on themselves or have a way to execute the function on a collection of objects.
For example, a square can be seen as a collection of four lines. When you "draw" the square it will tell each of the four lines to draw themselves. Each line is likely a collection of points. Therefore, each line will instruct its points to draw themselves. A point ends up doing the real work; everything else is a delegation. This simple example can be extended to consider a square that also draws its center point. That would be a collection of lines and a point. Although it seems simple, the ability to mix and match primitives and collections has a broad range of uses.
Java, PHP, C#, etc.
All modern languages support an interface and thus look very similar in implementation. You create an interface that contains the methods you want to be shared and then create the classes that implement it. The implementation of a method in a class can either be handled within the class or passed through to objects it contains or controls. We will see some other patterns that are similar in that they make it easy to apply functions across a heterogeneous collection.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…