Composite Pattern is to compose set of objects into tree structure to represent a part of hierarchies. This structure for clients is a single unit uniformly. In software engineering, the composite pattern come under the structural design pattern of 23 GoF Design Pattern, according to this pattern, a group of same type of objects treated as single object by client.
According to the Gang of Four:
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Motivation behind the Composite Design pattern is that objects grouped into Tree Structure, this structure actually is combination of the Node-Leaf and Branches. In tree structure, Nodes have the number of leaves and another nodes and Leaf doesn’t has anything, no child of leaf, so leaf treated as end point of tree structured data.
Let’s see the following UML class diagram for this pattern.
Let’s have a look into following terms used in this design pattern.
also read:
This pattern we can use to solve following common problems:
In this real-world example, we demonstrate the Composite pattern used in building a graphical tree structure made up of primitive nodes (lines, circles, etc) and composite nodes (groups of drawing elements that make up more complex elements).
/** * */ package com.doj.patterns.structural.composite; /** * @author Dinesh.Rajput * */ public abstract class DrawingElement { protected String name; // Constructor public DrawingElement(String name){ this.name = name; } public abstract void add(DrawingElement d); public abstract void remove(DrawingElement d); public abstract void display(int indent); }
/** * */ package com.doj.patterns.structural.composite; /** * @author Dinesh.Rajput * */ public class PrimitiveElement extends DrawingElement { public PrimitiveElement(String name) { super(name); } @Override public void add(DrawingElement d) { System.out.println("Cannot add to a PrimitiveElement"); } @Override public void remove(DrawingElement d) { System.out.println("Cannot remove from a PrimitiveElement"); } @Override public void display(int indent) { System.out.println(indent + "-" + name); } }
/** * */ package com.doj.patterns.structural.composite; import java.util.ArrayList; import java.util.List; /** * @author Dinesh.Rajput * */ public class CompositeElement extends DrawingElement { private List elements = new ArrayList<>(); public CompositeElement(String name) { super(name); } @Override public void add(DrawingElement d) { elements.add(d); } @Override public void remove(DrawingElement d) { elements.remove(d); } @Override public void display(int indent) { System.out.println("----"+ name+"----"); // Display each child element on this node for(DrawingElement d : elements){ d.display(indent + 2); } } }
/** * */ package com.doj.patterns.structural.composite; /** * @author Dinesh.Rajput * */ public class CompositePatternDemo { /** * @param args */ public static void main(String[] args) { // Create a tree structure CompositeElement root = new CompositeElement("Picture"); root.add(new PrimitiveElement("Red Line")); root.add(new PrimitiveElement("Blue Circle")); root.add(new PrimitiveElement("Green Box")); // Create a branch CompositeElement comp = new CompositeElement("Two Circles"); comp.add(new PrimitiveElement("Black Circle")); comp.add(new PrimitiveElement("White Circle")); root.add(comp); // Add and remove a PrimitiveElement PrimitiveElement pe = new PrimitiveElement("Yellow Line"); root.add(pe); root.remove(pe); // Recursively display nodes root.display(1); } }
----Picture---- 3-Red Line 3-Blue Circle 3-Green Box ----Two Circles---- 5-Black Circle 5-White Circle
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…