Composite pattern allows treating different objects in a similar fashion. Figure ‘Uniformity’ shows how different objects are called in a uniform manner.

In order to treat objects in a uniformed manner we need to inherit them from a common interface. Figure ‘Common Interface’ shows the objects inheriting from a common interface.

Figure: - Common Interface
Figure ‘Client code for composition’ shows how we added all the different kind of objects in to one collection and then called them in a uniform fashion.

Figure: - Client code for composition
Asked In: Many Interviews |
Alert Moderator