Tree - Composite Type (Design Pattern)

Data System Architecture


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.


Composite Uml Class Diagram


  • 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

Discover More
Data System Architecture
(Tree|Nested Set|Hierarchy) Data Structure

A tree is a node that may have children. Tree's are inherently recursive by definition as each child of a node is a Tree itself, with or without children nodes. A tree is a special case of a graph structure...
Card Puncher Data Processing
Data Type - Complex Data Type

A complex data type is a data type that shows a structure. All complex data type are based on a class concept. They are complex because they shows a structure primitive A complex data type is created...
Relational Data Model
Functional Programming - Algebraic Data Type

An algebraic data type (Algebraic_data_type) is a data type that is the inputand the output of its own operations. An algebraic structure can be composed before being executed. This is a composite...
Card Puncher Data Processing
GUI - ( Widget | Graphical element)

A widget is a user interface object in a graphical user interface (GUI), such as a button or a scroll bar. Widgets range: from simple objects that have one value or operation (e.g., check boxes and...
Java Conceptuel Diagram
Java - (Inner|Nested) Class

The Java programming language allows you to define a class within another class. Nested classes that are declared static are called static nested classes. Non-static nested classes are called...
Eclipse Oepe Junit Library
Java - JUnit

Written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) A simple framework to write and run repeatable tests JUnit features include: Assertions for testing expected...
Uml Composite Aggregation
Logical Data Modeling - Composition Relationship

composition is a containment relationship of physical type where the content entity is a part of a container entity This relationship is also known as: a whole/part relationship (ie whole/component)...
Data System Architecture
Tree - Visitor (Design Pattern)

Even if the visitor pattern can be applied to a list, it's usually associated to a Tree structure (Composites). It separates the operation on the nodes of a tree from its structure. The node are then...

Share this page:
Follow us:
Task Runner