How to implement Composite pattern in Java?

Photo by Markus Spiske on

Let’s say you are the manager of a project.

You have been asked to calculate the total cost to your company of your entire team.

You have a service development team with two members.

You have a UI development team with two members.

You have a Quality Assurance team with two members.

You have a business analyst.

Now let’s use composite pattern to calculate the total cost to company.

Using this pattern we are going to treat an employee the same way we treat a team.

We are going to fire the same method getCostToCompany() on both the employee and the team.

If it is a team , the code is going to iterate through all the employees in its team and find the sum of their costs.

If it is an employee , it is going to just return the employee’s own cost to company.

That describes the Composite pattern in action.

Putting it in a simplistic description:

If we want to treat an individual object the same way as a group of related objects we use composite pattern.

Lets start from the client code:

package structural.composite.pattern;

public class Client {

	public static void main(String a[]) {

		IndividualEmployee serviceDeveloper1 = new IndividualEmployee("Kavin");


		IndividualEmployee serviceDeveloper2 = new IndividualEmployee("Murali");


		IndividualEmployee tester1 = new IndividualEmployee("Kiran");

		IndividualEmployee tester2 = new IndividualEmployee("Ayisha");


		IndividualEmployee uiDeveloper1 = new IndividualEmployee("Vinitha");

		IndividualEmployee uiDeveloper2 = new IndividualEmployee("Agalya");


		EmployeeTeam serviceDevelopmentTeam = new EmployeeTeam("Service Development Team");


		EmployeeTeam uiDevelopmentTeam = new EmployeeTeam("UI Development Team");


		EmployeeTeam qaTeam = new EmployeeTeam("QA team");

		EmployeeTeam developmentTeam = new EmployeeTeam("Development Team");
		IndividualEmployee businessAnalyst = new IndividualEmployee("Rama");
		EmployeeTeam entireTeam = new EmployeeTeam("Entire team");
		System.out.println("Total Cost to Company: "+entireTeam.getCostToCompany());



As you can see ‘entireTeam’ represents the whole team.

It has two sub teams ‘Development team’ and ‘QA team ‘ and an individual business analyst added to it as well.

The development team in turn has two sub teams : Service Development team and UI Development team.

The Service and UI Development teams in turn have two developers in them each.

Also the QA team has two testers in them.

Both the ‘EmployeeTeam’ and ‘IndividualEmployee’ extend the same base class ‘Employee’ and overrides the method getCostToCompany() in their own way to utilize Composite Pattern,

Only a single method is fired on the entireTeam – getCostToCompany() to get the cost of the company.

Let’s look at the base class of both EmployeeTeam and IndividualEmployee:

package structural.composite.pattern;

import java.util.ArrayList;
import java.util.List;

public abstract class Employee {

	protected long costToCompany;

	protected String name;

	protected List<Employee> employees = new ArrayList<Employee>();

	public String getName() {
	void add(Employee employee) throws Exception {

		throw new UnsupportedOperationException("Only team can add an employee");

	void remove(Employee employee) throws Exception {

		throw new UnsupportedOperationException("Only team can remove an employee");

	abstract long getCostToCompany();


The Employee base class has:

  • A costToCompany field which carries the cost to company value.
  • A name attribute to represent the employee name or the team name
  • An add() operation used to add employees if it is an employee team. An employee cannot add an employee to itself though it extends this base class. Hence by default we throw an UnsupportedOperationException. The EmployeeTeam overrides this method to add the employee to its team.
  • Similarly we have a remove() operation to remove members from the team which again can be supported only by the ‘EmployeeTeam’ classes
  • We have an abstract getCostToCompany() which is the major method utilizing the Composite design pattern. In case of an employee it will return the costToCompany set in the Individual Employee class. In case of a team , it will iterate through all the employees added to the team , sum up their costs and return the value.

Here is the IndividualEmployee class:

package structural.composite.pattern;

public class IndividualEmployee extends Employee {

	public IndividualEmployee(String name) { = name;

	long getCostToCompany() {

		return this.costToCompany;

	public void setCostToCompany(long cost) {

		this.costToCompany = cost;

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IndividualEmployee other = (IndividualEmployee) obj;
		if (name == null) {
			if ( != null)
				return false;
		} else if (!name.equals(
			return false;
		return true;


Here is the EmployeeTeam class extending the same base classs:

package structural.composite.pattern;

public class EmployeeTeam extends Employee {

	public EmployeeTeam(String name) { = name;

	void add(Employee employee) {


	void remove(Employee employee) {


	long getCostToCompany() {

		long cost = 0;

		if (this.employees != null) {
			for (Employee e : this.employees) {

				if (e instanceof IndividualEmployee ind) {
					System.out.println("Cost to Company of " + ind.getName());


				cost += e.getCostToCompany();
		return cost;


Here we are overriding add() and remove() methods to add and remove employees from the list of employees respectively. Also we implement the getCostToCompany() and iterate over all the Individual Employees in the team , to sum up their costs.

Here is the output :

Cost to Company of Kavin
Cost to Company of Murali
Cost to Company of Vinitha
Cost to Company of Agalya
Cost to Company of Kiran
Cost to Company of Ayisha
Cost to Company of Rama
Total Cost to Company: 400000

An elegant way to treat a ‘team’ and ’employee’ the same way to get the total cost to company.

Quoting Gang of Four,

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and composites of objects uniformly

And they suggest to use it when :

– you want to represent part-whole hierarchies to objects (in our case Employee – Employee Team represent part-whole hierarchy)

– you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly. ( in our cases we treat both the team and the individual member the same way to get the cost to company)

Here is the code :






Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading