Design Pattern: Composite Pattern in Java

Composite pattern is used to handle the operation of a bunch of classes (having the same interface), to operate the same function of all items a the same time. Using Composite pattern the client can ignore the difference between the Composition and Leaf classes, and can use the composition considering the same interface. This article demonstrates Composite pattern implementations in Java. Check the following examples.

Design Pattern: Composite Pattern

When there is a bunch of classes that are of the same type (implement the same interface), and we need to use them as composition (not as individual objects), we can use Composite pattern. Composite pattern helps to use the classes in a composition, so that the client can use the composition and individual classes can be tread in the same way.

Design Pattern: Decorator Pattern

In Decorator design pattern, our target is to add some new functionality to an existing object. Say, there is an existing class that is responsible for printing a UI element (like a Button or Table). Later we need the ability to add a border or text color to that element. We can add new properties and functions to that existing class and introduce those properties. But that will violate the Open-Close principle of the SOLID principle.