Composite Design Pattern in Java

Filed Under: Design Patterns

Composite pattern is one of the Structural design pattern. Composite design pattern is used when we have to represent a part-whole hierarchy.

Composite Design Pattern

composite pattern, composite design pattern

When we need to create a structure in a way that the objects in the structure has to be treated the same way, we can apply composite design pattern.

Lets understand it with a real life example – A diagram is a structure that consists of Objects such as Circle, Lines, Triangle etc. When we fill the drawing with color (say Red), the same color also gets applied to the Objects in the drawing. Here drawing is made up of different parts and they all have same operations.

Composite Pattern consists of following objects.

  1. Base Component – Base component is the interface for all objects in the composition, client program uses base component to work with the objects in the composition. It can be an interface or an abstract class with some methods common to all the objects.
  2. Leaf – Defines the behaviour for the elements in the composition. It is the building block for the composition and implements base component. It doesn’t have references to other Components.
  3. Composite – It consists of leaf elements and implements the operations in base component.

Here I am applying composite design pattern for the drawing scenario.

Composite Pattern Base Component

Composite pattern base component defines the common methods for leaf and composites. We can create a class Shape with a method draw(String fillColor) to draw the shape with given color.


public interface Shape {
	public void draw(String fillColor);

Composite Design Pattern Leaf Objects

Composite design pattern leaf implements base component and these are the building block for the composite. We can create multiple leaf objects such as Triangle, Circle etc.


public class Triangle implements Shape {

	public void draw(String fillColor) {
		System.out.println("Drawing Triangle with color "+fillColor);



public class Circle implements Shape {

	public void draw(String fillColor) {
		System.out.println("Drawing Circle with color "+fillColor);


Composite object

A composite object contains group of leaf objects and we should provide some helper methods to add or delete leafs from the group. We can also provide a method to remove all the elements from the group.


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

public class Drawing implements Shape{

	//collection of Shapes
	private List<Shape> shapes = new ArrayList<Shape>();
	public void draw(String fillColor) {
		for(Shape sh : shapes)
	//adding shape to drawing
	public void add(Shape s){
	//removing shape from drawing
	public void remove(Shape s){
	//removing all the shapes
	public void clear(){
		System.out.println("Clearing all the shapes from drawing");

Notice that composite also implements component and behaves similar to leaf except that it can contain group of leaf elements.

Composite Pattern, Composite Design Pattern Java

Our composite pattern implementation is ready and we can test it with a client program.

Composite Design Pattern Client Program



public class TestCompositePattern {

	public static void main(String[] args) {
		Shape tri = new Triangle();
		Shape tri1 = new Triangle();
		Shape cir = new Circle();
		Drawing drawing = new Drawing();


Output of the above composite pattern client program is:

Drawing Triangle with color Red
Drawing Triangle with color Red
Drawing Circle with color Red
Clearing all the shapes from drawing
Drawing Triangle with color Green
Drawing Circle with color Green

Composite Pattern Important Points

  • Composite pattern should be applied only when the group of objects should behave as the single object.
  • Composite design pattern can be used to create a tree like structure.

java.awt.Container#add(Component) is a great example of Composite pattern in java and used a lot in Swing.

Earlier structural design pattern articles:


  1. Stephane Tremblay says:

    Great example.

    The Shape interface name should be Drawable to follow naming conventions and to describe what the interface adds to a class. Basically a drawing is a composition of drawable elements such as shapes and lines.

    The class name Shape would be better suited for an Abstract Class.

  2. taotao says:

    Hi Pankaj
    your blog is really helpful to me. I just want to say thanks to you.

  3. ravi says:

    This is not composite pattern. check this for right explanation
    in composite pattern there have to be nodes and leaves to form a tree. Just aggregating objects into another object is not a composite pattern.

    1. avmohan says:

      This is a composite pattern. The main idea in composite pattern is that the client shouldn’t need to know whether it’s an atomic object or a composite.

      Using the terminology of the composite pattern (from itself, here, the drawing is a Composite and triangle, & circle are Leaves and Shape is the Component.

      1. Anil Chowdhury says:

        Hi avmohan,
        I agree with Ravi, this is not composite pattern. What pankaj has shown in the above example is aggregation of objects.
        A composite pattern should treat each object hierarchically either as leaf or parent. The wiki link itself says “The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to “compose” objects into tree structures”

        Without parent-child relationship composite pattern cannot exist.
        A good example is Filesystem..whether each node could be parent (sub-folder) or leaf (i.e. files)
        please refer to this link :

        1. Akash says:

          It is indeed composite pattern. If you want more level hierarchy just create another composite instance such as ‘drawing1’ and add it in original composite instance ‘drawing’ and see the result.

  4. Manoj Singh says:

    Nice article pankaj, you are the champ.

  5. Nabil Amrouche says:

    Thanks a lot !
    Your tutorials were so useful for me !!! and i’m sure, i’m not the only one !
    Design Patterns especially, That got me a good mark in my exams :))))
    Thanks; another time !!!
    Good Continuation !

  6. perminder singh says:

    Really nice post. help me a lot to get the concept.

  7. Zhacantal says:

    It really help me a lot..
    Can you give an example of composite pattern that has a GUI
    Thank you!!!

  8. Amit says:

    I like the example you have provided.

  9. Yogesh Kharode says:

    Hi Pankaj,

    Thanks for sharing best core java tutorial for freshers and experience.
    Its really helpful for java developer.
    kindly request to you please share Spring MVC+Hibernate application as per industry stranded and useful for better performance.

    Thanks and Regards
    Yogesh Kharode

    1. Pankaj says:

      Thanks Yogesh for the kind words, please do check out the design pattern articles. I am planning on starting on Spring tutorial in coming month.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content