Composite Pattern

Category : Design Patterns | Sub Category : Structural design patterns | By Prasad Bonam Last updated: 2023-07-09 08:55:25 Viewed : 495

Composite Pattern

The Composite pattern is a structural design pattern that allows you to compose objects into tree-like structures and work with them as if they were individual objects. It lets clients treat individual objects and compositions of objects uniformly. The Composite pattern is useful when you need to represent part-whole hierarchies and want to apply operations recursively over the hierarchy. Here is an example of implementing the Composite pattern in Java:

import java.util.ArrayList; import java.util.List; // Component interface interface Component { void operation(); } // Leaf class class Leaf implements Component { private String name; public Leaf(String name) { = name; } @Override public void operation() { System.out.println("Leaf: " + name); } } // Composite class class Composite implements Component { private List<Component> children = new ArrayList<>(); public void add(Component component) { children.add(component); } public void remove(Component component) { children.remove(component); } @Override public void operation() { System.out.println("Composite:"); for (Component component : children) { component.operation(); } } } // Client code public class Client { public static void main(String[] args) { Component leaf1 = new Leaf("Leaf 1"); Component leaf2 = new Leaf("Leaf 2"); Component leaf3 = new Leaf("Leaf 3"); Composite composite1 = new Composite(); composite1.add(leaf1); composite1.add(leaf2); Composite composite2 = new Composite(); composite2.add(composite1); composite2.add(leaf3); composite2.operation(); } }

In this example:

  • The Component interface represents the component interface for both leaf and composite objects. It declares the operation() method that is implemented by leaf and composite classes.
  • The Leaf class represents the leaf objects, which are the indivisible building blocks of the composition. They implement the operation() method to perform their specific functionality.
  • The Composite class represents the composite objects, which are the containers that can hold leaf objects or other composite objects. They maintain a list of child components and implement the operation() method to perform operations on themselves and delegate the operation to their child components.
  • The client code in the Client class demonstrates the usage of the composite objects. It creates instances of leaf objects (Leaf) and composite objects (Composite) and adds them to the appropriate composites. Finally, it calls the operation() method on the top-level composite object, which recursively invokes the operation on all the child components.

By using the Composite pattern, you can create hierarchical structures where individual objects and groups of objects are treated uniformly. It simplifies the client code by allowing it to work with the complex structures without distinguishing between leaf and composite objects. It enables recursive operations and provides a convenient way to traverse and manipulate the tree-like structures.

Related Articles

Leave a Comment: