Composite Design Pattern in Java

The composite pattern is a structural design pattern. This pattern is also called a partitioning pattern. This pattern explains that a group of objects will be treated as a same way as a single instance of an object. The intention of the composite pattern is to compose objects into tree hierarchies to represent the part-whole relationship.

In composite pattern, the tree structure exists where identical operations can be performed on leaves and nodes. A node in a tree is a class that can have children. A leaf in a tree does not have children. The children of a composite can be leaves or other composites.

The main benefit of composite pattern is it defines class hierarchies consisting of primitive and composite objects. It also makes provision for us to add new kind of components.

Usage

When clients use to ignore the difference between composition of objects and individual objects.
When the structure can have any level of complexity and is dynamic. Continue reading “Composite Design Pattern in Java”