Other Tutorials

Composite Design Pattern In Java

Introduction:

A composite design pattern is useful when we have to work with a tree-like hierarchical structure of objects.

It lets us treat the individual objects and the composition of objects uniformly. It falls under the category of a structural design pattern as it composes objects into a tree structure to represent part-whole hierarchies.

UML Representation:

The UML representation of a composite pattern looks like:Composite Design Pattern

Where:

  • Component: is either an interface or an abstract class which is a supertype for all set of classes
  • Composite class: it defines a concrete implementation of Component and holds instances of other child components
  • Leaf class: A leaf represents a non-composite type i.e. it isn’t composed of other components

Note that a composite object can contain other composite objects and this is often termed as recursive composition.

Additionally, both Leaf and Composite classes implement(extend if an abstract class) the Component interface. This enables us to deal with composite and non-composite objects uniformly through polymorphism.

The leaf and the composite objects conform to a set of shared behaviors defined in Component.

Implementing the Composite Pattern:

Let’s build a UI menu hierarchy where each menu can have sub-menus. We’ll use the composite design pattern to implement this hierarchical structure.

To begin with, let’s define our MenuComponent class:

We want the aggregated menu and the individual menu items to be treated uniformly so we have abstracted out the displayMenu() method in our MenuComponent class.

Let’s now implement our MenuItem class:

A MenuItem will be the leaf element and can’t hold any submenus.

A Menu, on the other hand, is composed of MenuComponent objects. In other words, a Menu can hold both Menu and MenuItem objects:

Testing Our Implementation:

The client code is expected to work with MenuComponent abstraction. Let’s create a hierarchy of menu items:

The above code will produce the following output:

Conclusion:

In this tutorial, we explored the composite design pattern. It’s a structural pattern which comes handy when trying to create a hierarchical structure of objects. It helps us to treat the composite and non-composite objects uniformly.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *