Composite Design Pattern Using Java

Introduction

 
A composite design pattern is a structural design pattern. It allows developers to create multiple nested objects of the same type to complete one single system hierarchy.
 
Players in this pattern:
  • Component: this player defines a base contract with the composite class to allow to create nested objects.
  • Composite: this player, is one who will implement the contract defined by the component.
  • Client: this player, with the help of a composite player, will complete the system hierarchy for a particular requirement
We will see with an example.
 
Requirement: Build a smart city with smart block features.
 
We will define 3 players:
  • AbstractBlock – Component
  • Block – Composite
  • Client
We will build an Abstract block with Add, Remove block features. If we want we can implement Add, Remove features explicitly. In my case, I am using List, by default I will be having Add, and Remove features available for adding/removing blocks.
  1. import java.util.List;  
  2.   
  3. public abstract class AbstractBlock {  
  4.     public String name;  
  5.     public double size;  
  6.   
  7.     public abstract void buildBlock();  
  8.   
  9.     public List<AbstractBlock> subBlocks;  
  10. }  
Now implement a Composite object i.e. Block by implementing/inheriting component (i.e. in our code AbstractBlock)
  1. import java.util.*;  
  2.   
  3. public class Block extends  AbstractBlock {  
  4.     public  Block(){  
  5.        subBlocks = new ArrayList<>();  
  6.     }  
  7.     @Override  
  8.     public void buildBlock() {  
  9.         System.out.println("Block built with name " + name + " with size " + size + "unit");  
  10.     }  
  11. }  
 In the above code we can see that, we provided the implementation for buildBlock method. Based on our need we can make concrete or non-concrete methods as well.
 
Below the client class code:
  1. public class Client {  
  2.   
  3.     public static void main(String[] args) {  
  4.         AbstractBlock smartCity = new Block();  
  5.         smartCity.name = "SmartCity";  
  6.         smartCity.size = 100000;  
  7.   
  8.         AbstractBlock smartlayOut = new Block();  
  9.         smartlayOut.name = "SmartlayOut";  
  10.         smartlayOut.size = 10000;  
  11.         smartCity.subBlocks.add(smartlayOut);  
  12.   
  13.         AbstractBlock smartHome = new Block();  
  14.         smartHome.name = "smartHome";  
  15.         smartHome.size = 1000;  
  16.         smartlayOut.subBlocks.add(smartHome);  
  17.   
  18.         AbstractBlock smartRoom = new Block();  
  19.         smartRoom.name = "smartRoom";  
  20.         smartRoom.size = 1000;  
  21.         smartHome.subBlocks.add(smartRoom);  
  22.   
  23.         AbstractBlock smartLocker = new Block();  
  24.         smartLocker.name = "smartLocker";  
  25.         smartLocker.size = 20;  
  26.         smartRoom.subBlocks.add(smartLocker);  
  27.   
  28.         AbstractBlock smartFolder = new Block();  
  29.         smartFolder.name = "smartFolder";  
  30.         smartFolder.size = 10;  
  31.         smartLocker.subBlocks.add(smartFolder);  
  32.   
  33.         AbstractBlock smartFile = new Block();  
  34.         smartFile.name = "smartFile";  
  35.         smartFile.size = 5;  
  36.         smartFolder.subBlocks.add(smartFile);  
  37.   
  38.         Build(smartCity);  
  39.         System.out.println();  
  40.     }  
  41.   
  42.     private static void Build(AbstractBlock block) {  
  43.         block.buildBlock();  
  44.         block.subBlocks.forEach(a -> Build(a));  
  45.     }  
  46. }  
In the above code, we can see that the client has a unitized composite object and built first:
  • Smart City followed by Smart Layout
  • Smart Layout followed by Smart Home
  • Smart Home followed by Smart Room
  • Smart Room followed by Smart Locker
  • Smart Locker followed by Smart Folder
  • Smart Folder followed by Smart File
After executing this code we can same hierarchy in tree format. For easy understating, I put snap ob object hierarchy in tree format.
 
Composite Design Pattern Using Java
 
Below is the output snap:
 
Composite Design Pattern Using Java
 
When we have a requirement of implementing some nested objects with the same type, then we can go for this design pattern.
 
We can extend this composite object to any extent with N numbers of blocks inside blocks.
 

Summary

 
Above is a simple example of using a composite design pattern, you can download the uploaded source code and try adding more blocks. I hope it helps.