Composite Design Pattern

Sachin Kumar
2 min readOct 26, 2020

In this article, we are going to discuss the Composite Design Pattern. This is the pattern in which we compose objects into a tree structure to represent part-whole hierarchies.

Let’s take an example, suppose we have a company in which we have developers and founder members which include founder and co-founder. Now, our job is to group them into two categories i.e. developer structure and founder structure. And, finally grouping these two structures into a single structure named Composite Structure.

Let’s understand this by implementing:

First, we are gonna create an interface Component having function showMemberDetails() in our package named component:

Now, let’s create another package named leaves in this package we are creating the following three classes, implementing the above TeamMember interface and these will print the name and salary of each member:

--

--

Sachin Kumar

Senior Java Backend Dev | Expertise in Java Microservices, Spring Boot Framework & Android apps development.