Composite Design Pattern

Composite Design Pattern

The Composite Pattern is a Structural Pattern that allows us to treat a group of objects the same way as a single instance of the object.

The Composite Pattern is made of the following objects:

Component

The Component is the abstraction for all Components, including Composite ones. It declares the interface for objects in the composition. It can be an interface or an abstract class with some methods common to all the objects. It also declares an interface for accessing and managing its child Components.

Leaf

A Leaf has no children. So, it represents Leaf objects in the composition and implements all Component methods.

Composite

It represents Components that have children and it implements methods to manipulate children and all Component methods, generally by delegating them to its children. It also provides additional methods for adding, removing and getting Components.

Client

The Client manipulates objects in the hierarchy through the Component interface.

Why do we need this design pattern?

Consider a building block kind of application. Here, complex components can be built out of simple components which in turn could be used to build further complex components. This kind of architecture can be seen in many real world applications.

The Composite pattern explains how to use recursive composition so that clients do not need to differentiate between primitive and container objects.

Example

Component:

public interface Component{
    public void showPrice();
}

Leaf:

public class Leaf implements Component{
    String name;
    Double price;
    public Leaf(){

    }
    public Leaf(String name,double price)
    {
        this.name = name;
        this.price = price;
    }

    @Override
    public void showPrice() {
        System.out.println(this.price);
    }
}

Composite Object:

import javax.management.AttributeList;
import java.util.ArrayList;

public class Composite implements Component {
    String name;

    ArrayList<Component> components;

    public Composite()
    {

    }

    public Composite(String name)
    {
        this.name = name;
        components = new ArrayList<>();
    }

    @Override
    public void showPrice()
    {
        for (Component c : components)
        {
            c.showPrice();
        }
    }

    public void add(Component subComponent)
    {
        components.add(subComponent);
    }

}

Client:

public class Main
{
    public static void main(String[] args)
    {
        Component hdd       = new Leaf("hdd" , 4000);
        Component keyboard  = new Leaf("keyboard",1000);
        Component mouse     = new Leaf("mouse",500);
        Component ram       = new Leaf("ram",3000);
        Component processor = new Leaf("Processor",10000);


        Composite computer = new Composite("computer");

        Composite motherboard = new Composite("motherboard");
        motherboard.add(ram);
        motherboard.add(processor);


        Composite cabinet  = new Composite("cabinet");
        cabinet.add(hdd);
        cabinet.add(motherboard);

        Composite peripherals     = new Composite("peripherals");
        peripherals.add(keyboard);
        peripherals.add(mouse);

        computer.add(cabinet);
        computer.add(peripherals);

        computer.showPrice();
    }
}

The Composite Design Pattern is a powerful pattern for creating tree-like structures of objects. It allows you to treat individual objects and groups of objects uniformly.