Composite Design Pattern in Java

1. Definition

The Composite Design Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.

2. Problem Statement

Imagine you have a graphical system where you want to treat individual shapes and groups of shapes the same way. Without a way to generalize individual and grouped objects, the client would have to distinguish and handle them separately.

3. Solution

The Composite Pattern solves this by defining a structure where individual objects (leaf nodes) and composite objects (compositions or groups) can be treated uniformly.

4. Real-World Use Cases

1. Graphics systems where shapes can be grouped and treated the same as individual shapes.

2. Organizational structures where an employee can be an individual or a manager with a team.

5. Implementation Steps

1. Define a component interface that will act as the leaf and composite object.

2. Implement concrete leaf objects that implement the component interface.

3. Implement a composite object that also implements the component interface and can hold children (leaf or composite).

4. The client interacts with the component interface, treating leaf and composite objects uniformly.

6. Implementation

// Step 1: Define the component interface
interface Graphic {
    void draw();
}

// Step 2: Implement concrete leaf objects
class Circle implements Graphic {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square implements Graphic {
    public void draw() {
        System.out.println("Drawing Square");
    }
}

// Step 3: Implement the composite object
class GraphicGroup implements Graphic {
    private List<Graphic> graphics = new ArrayList<>();

    public void addGraphic(Graphic graphic) {
        graphics.add(graphic);
    }

    public void draw() {
        for (Graphic graphic : graphics) {
            graphic.draw();
        }
    }
}

// Step 4: Demonstrate the Composite Design Pattern
public class CompositePatternDemo {
    public static void main(String[] args) {
        Circle circle1 = new Circle();
        Circle circle2 = new Circle();
        Square square = new Square();

        GraphicGroup group1 = new GraphicGroup();
        group1.addGraphic(circle1);
        group1.addGraphic(square);

        GraphicGroup group2 = new GraphicGroup();
        group2.addGraphic(circle2);
        group2.addGraphic(group1);

        group2.draw();
    }
}

Output:

Drawing Circle
Drawing Circle
Drawing Square

Explanation

In the demonstration, two circles and a square are drawn, showcasing that individual shapes and groups of shapes are treated uniformly. Even when groups contain other groups, the Composite pattern allows for seamless integration and operation.

7. When to use?

Use the Composite Design Pattern when:

1. You want to represent part-whole hierarchies of objects.

2. You want clients to ignore the difference between compositions of objects and individual objects.

3. The structure can have any level of complexity and is dynamic.

With the Composite Design Pattern, adding new components or groups becomes simpler, and the client code remains unaltered.


Comments