Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this quick tutorial, we’ll introduce the composite design pattern in Java.

We’re going to describe the structure and the purpose of its use.

2. The Structure

The composite pattern is meant to allow treating individual objects and compositions of objects, or “composites” in the same way. 

It can be viewed as a tree structure made up of types that inherit a base type, and it can represent a single part or a whole hierarchy of objects.

We can break the pattern down into:

  • 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.
  • leaf – implements the default behavior of the base component. It doesn’t contain a reference to the other objects.
  • composite – has leaf elements. It implements the base component methods and defines the child-related operations.
  • client – has access to the composition elements by using the base component object.

3. Practical Example

Now, let’s dive into the implementation. Let’s suppose we want to build a hierarchical structure of departments in a company.

3.1. The Base Component

As a component object we’ll define a simple Department interface:

public interface Department {
    void printDepartmentName();
}

3.2. Leafs

For the leaf components, let’s define classes for financial and sales departments:

public class FinancialDepartment implements Department {

    private Integer id;
    private String name;

    public void printDepartmentName() {
        System.out.println(getClass().getSimpleName());
    }

    // standard constructor, getters, setters
}

The second leaf class, SalesDepartment, is similar:

public class SalesDepartment implements Department {

    private Integer id;
    private String name;

    public void printDepartmentName() {
        System.out.println(getClass().getSimpleName());
    }

    // standard constructor, getters, setters
}

Both classes implement the printDepartmentName() method from the base component, where they print the class names for each of them.

Also, as they’re leaf classes, they don’t contain other Department objects.

Next, let’s see a composite class as well.

3.3. The Composite Element

As a composite class, let’s create a HeadDepartment class:

public class HeadDepartment implements Department {
    private Integer id;
    private String name;

    private List<Department> childDepartments;

    public HeadDepartment(Integer id, String name) {
        this.id = id;
        this.name = name;
        this.childDepartments = new ArrayList<>();
    }

    public void printDepartmentName() {
        childDepartments.forEach(Department::printDepartmentName);
    }

    public void addDepartment(Department department) {
        childDepartments.add(department);
    }

    public void removeDepartment(Department department) {
        childDepartments.remove(department);
    }
}

This is a composite class as it holds a collection of Department components, as well as methods for adding and removing elements from the list.

The composite printDepartmentName() method is implemented by iterating over the list of leaf elements and invoking the appropriate method for each one.

4. Testing

For testing purposes, let’s have a look at a CompositeDemo class:

public class CompositeDemo {
    public static void main(String args[]) {
        Department salesDepartment = new SalesDepartment(
          1, "Sales department");
        Department financialDepartment = new FinancialDepartment(
          2, "Financial department");

        HeadDepartment headDepartment = new HeadDepartment(
          3, "Head department");

        headDepartment.addDepartment(salesDepartment);
        headDepartment.addDepartment(financialDepartment);

        headDepartment.printDepartmentName();
    }
}

First, we create two instances for the financial and sales departments. After, we instantiate the head department and add up the previously created instances to it.

Finally, we can test the printDepartmentName() composition method. As we expect, the output contains the class names of each leaf component:

SalesDepartment
FinancialDepartment

5. Conclusion

In this article, we learned about the Composite design pattern. The write-up highlights the main structure and demonstrates the usage through the practical example.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW