When not to use Composite Design Pattern?
Composite Design Pattern makes it harder to restrict the type of components of a composite. So it should not be used when you don’t want to represent a full or partial hierarchy of objects.
- Composite Design Pattern can make the design overly general.
- It makes harder to restrict the components of a composite.
- Sometimes you want a composite to have only certain components. With Composite, you can’t rely on the type system to enforce those constraints for you.
- Instead you’ll have to use run-time checks.
Composite Design Pattern in Java
The Composite Design Pattern is a structural design pattern that lets you compose objects into tree-like structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly. In other words, whether dealing with a single object or a group of objects (composite), clients can use them interchangeably.
As described by the Gang of four, “Compose objects into tree structure to represent part-whole hierarchies. Composite lets client treat individual objects and compositions of objects uniformly”.
The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have the least common denominator relationship.
Important Topics for the Composite Design Pattern in Java
- Components of Composite Design Pattern
- Composite Design Pattern example in Java
- Why do we need Composite Design Pattern?
- When to use Composite Design Pattern?
- When not to use Composite Design Pattern?