Tree - Composite Type (Design Pattern)


A composite type is a complexe type that models a composition relationship between types.

A composite object is an object with many components

  • a folder consists of a set of documents,
  • a book consists of a set of chapters,
  • a company consists of a set of departments.

The intent of a composite is to compose objects into tree structures to represent part-whole hierarchies.

A composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a “has-a” relationship between objects. The operations you can perform on all the composite objects often have a least common denominator relationship.



A naive modeling of composite objects leads to many similar classes specialized primarily by the types of their components.


Consider a text editing system with “chapter”, “section”, “subsection”, “paragraph”, “word”, “item” etc. We could have a large number of classes, each of which was really just a collection of its components. Some have titles, some always start a new page, but in general they are quite similar. On the one hand, the composite object is “obviously” different from its components. On the other hand, it is easier to reuse and understand a design that minimizes the number of classes by making its classes be polymorphic and composable in any combination. We don't want to make every “text-element” have a list of components; some will be atomic. But we would like a scheme that lets us avoid having to duplicate code and to design a lot of classes.



  • Component is an interface defining the common objects methods
  • Leaf represents leaf objects in the composition (hierarchy) and implements all Component methods
  • Composite represents a composite Component (a parent component having children) implements methods to manipulate children implements all Component methods, generally by delegating them to its children


  • Component
interface Graphic {
    //Prints the graphic.
    public void print();
  • Leaf
class Ellipse implements Graphic {
    //Prints the graphic.
    public void print() {
  • Composite
import java.util.List;
import java.util.ArrayList;
class CompositeGraphic implements Graphic {
    //Collection of child graphics.
    private List<Graphic> childGraphics = new ArrayList<Graphic>();
    //Prints the graphic.
    public void print() {
        for (Graphic graphic : childGraphics) {
    //Adds the graphic to the composition.
    public void add(Graphic graphic) {
    //Removes the graphic from the composition.
    public void remove(Graphic graphic) {
  • Client
public class Program {
    public static void main(String[] args) {
        //Initialize four ellipses
        Ellipse ellipse1 = new Ellipse();
        Ellipse ellipse2 = new Ellipse();
        Ellipse ellipse3 = new Ellipse();
        Ellipse ellipse4 = new Ellipse();
        //Initialize three composite graphics
        CompositeGraphic graphic = new CompositeGraphic();
        CompositeGraphic graphic1 = new CompositeGraphic();
        CompositeGraphic graphic2 = new CompositeGraphic();
        //Composes the graphics
        //Prints the complete graphic (four times the string "Ellipse").

Documentation / Refrerence

Powered by ComboStrap