Composite Design Pattern in Java

Composite Design Pattern is a very useful structural design pattern that allows users to look at a group of objects in composition that will cater to a particular problem. A certain code might have several objects with their hierarchies well-defined. Implementing a composite pattern in such cases will bind those objects in a tree-like manner respecting hierarchies. Lower the priority of an object, lower down the tree will it reside. These objects thereafter work like a single-entity object.

Definition

Composite Design Pattern binds complex objects together to form a tree-like hierarchy pattern. Each object on the tree inherits from its base type or leaf. The clients can thereafter access the composite element by calling or instantiating the base object of that composition. A composite object is made of smaller and simpler base objects that exhibit the same attributes. In object-oriented programming terminology, this is generally referred to as a ‘has-a’ relationship between the base and composite objects. This functionality particularly helps when you have to make changes. Edits done in the composite pattern will equally affect all the base objects.

Example and Code

Let us consider a restaurant has two tertiary branches and a main branch. The leaf components will be the two branches and the main branch will be a composition of the two.

//Defining a general interface

public interface Restaurant {
void ResBranchName();
}

//Coding the leaf components

public class KolkataBranch implements Restaurant {
private String ManagerName;
private double ResCode;
public void ResBranchName() {
System.out.println (getClass().getBranchName());
}
}

public class Delhi Branch implements Restaurant {
private String managerName;
private double resCode;
public void ResBranchName() {
System.out.println (getClass().getBranchName());
}
}

public class MumbaiBranch implements Restaurant {
private String managerName;
private double resCode;
public void ResBranchName() {
System.out.println (getClass().getBranchName());
}
}

//The composite class for the head quarter

public class BangaloreHeadbranch implements Department {
private String managerName;
private double resCode;
private HashMap<Restaurant> branch;

public BangaloreHeadBranch (String managerName, double resCode ) {
this.managerName = managerName;
this.branch = new ArrayList<>();
}

……. /* Functions like tax reports of different branches, staff capacity, and maintenance cost can be listed below. The class above is a composite class as the list contains objects of the type restaurant. */

When to use the Composite Design Pattern

When clients are not concerned about the origin of an object and a lot of objects show the same kind of behaviour, the composite design pattern becomes an efficient choice in such cases. This pattern takes away a lot of complexity from codes and makes it readable.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.