Composite Design Pattern in Java

By | July 5, 2020

In this post, We will talk and learn about the Composite Design Pattern in Java.

Key Points About Composite Design Pattern :

When we want to create a structure in a way that the objects in the structure have to be treated the same way then we can apply a composite design pattern.

We can break the pattern down into:

  1. component – is the base interface for all the objects in the composition. It should be either an interface or an abstract class with the common methods to manage the child composites.
  2. Leaf – implements the default behavior of the base component. It doesn’t contain a reference to the other objects.
  3. Composite – It has leaf elements. It implements the base component methods and defines the child-related operations.
  4. Client – It has access to the composition elements by using the base component object.

Composite Design Pattern in JDK:

Now Let’s move towards the implementation of the Composite Design Pattern.

Below is the complete source code:

Composite Design Pattern

Service.java

 

AdminService.java

 

EmployeeService.java

 

ServiceProvider.java

 

ClientTest.java

Output of this client Program:

Registration Service for Employee
Registration Service for Employee
Registration Service for Admin
——————————
Logout Service for Employee
Logout Service for Admin
Logout Service for Admin
——————————
Update Service for Employee

You May Also Like:

Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Proxy Design Pattern in Java

That’s all about the Composite Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *