Composite Pattern

Composite pattern is a structural pattern which is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.

The intent of this pattern, according to Design Patterns by Gamma et al, is to

  • compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
  • create Recursive composition e.g. "Directories contain entries, each of which could be a directory."

This pattern should be used when clients ignore the difference between compositions of objects and individual objects. If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous.

 

Structure

In composite pattern, an abstract base class (Component class) that specifies the behavior that needs to be exercised uniformly across all its childs. Then, Component class is subclassed to the Leafs and Composite classes. Each Composite object "couples" itself only to the abstract type Component as it manages its childs.

The structure of the composite pattern is shown below:

composite pattern structure

Figure: Composite Pattern Structure

Participants:

Component

  • is the abstraction for all components, including composite ones
  • declares the interface for objects in the composition

Composite

  • represents a composite Component i.e. component having children
  • implements child management methods [e.g. add()remove()] to manipulate children
  • implements all Component methods, generally by delegating them to its children

 Leaf

  • represents leaf objects in the composition
  • implements all Component methods

 

Example

Consider a system in which a picture is composed with different graphics like circle, rectange etc.

The UML class diagram below depicts the system required.

composite pattern example

Figure: Composite Pattern Example

Here, Graphic interface is Component, which can be either an Circle or Rectangle or a composition of several graphics. Every graphic can be printed. 

Child management methods [e.g. addC()remove()] are defined in composite class Picture.

 

Java Implementation

Here is Java implementation of the example described above.

Let us create a component class Graphic.

/**
 * Component
 */
public interface Graphic {
    void draw();
}

Now, lets create composite class Picture.

/**
 * Composite
 */
public class Picture implements Graphic {
    //Collection of child graphic
    private Collection<Graphic> graphics = new ArrayList<Graphic>();

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

    //Adds the graphic to the composition.
    public void add(Graphic graphic){
        graphics.add(graphic);
    }

    //Removes the child graphic from the composition.
    public void remove(Graphic graphic) {
        graphics.remove(graphic);
    }
}

Following are the leaf ( subclasses) of component class Graphic.

/**
 * Leaf
 */
public class Circle implements Graphic {

    //Draw Circle
    @Override
    public void draw() {
        System.out.println("Circle Drawn.");
    }
}
/**
 * Leaf
 */
public class Rectangle implements Graphic {
    @Override
    public void draw() {
        System.out.println("Rectangle drawn.");
    }
}

The following code is for client.

/**
 * Client
 */
public class Application {
    public static void main(String[] args) {
        //creates leaf objects
        Graphic circle = new Circle();
        Graphic rectangleA = new Rectangle();
        Graphic rectangleB = new Rectangle();

        //Initialize composite graphic i.e. picture
        Picture picture = new Picture();
        picture.add(circle);
        picture.add(rectangleA);
        picture.add(rectangleB);
        //prints complete picture
        picture.draw();
        System.out.println("Remove one leaf --> Rectangle object");
        //removes a graphic from picture
        picture.remove(rectangleA);
        //prints new picture
        picture.draw();
    }
}

The output of the program is:

Circle Drawn.
Rectangle drawn.
Rectangle drawn.
Remove one leaf --> Rectangle object
Circle Drawn.
Rectangle drawn.

The output of the program illustrates the creation of composite objects and manipulation of child objects of Component.

Template Method Pattern
Command Pattern