Composite pattern is used to handle the operation of a bunch of classes (having the same interface), to operate the same function of all items a the same time. Using Composite pattern the client can ignore the difference between the Composition and Leaf classes, and can use the composition considering the same interface.
Check complete details and explanations about the Composite pattern in the article: Composite Design Pattern
This article demonstrates Composite pattern implementations in Java. Check the following examples.
Example #1: Transport
In the example below we are implementing the Composite pattern for a transport system.
Take a look at the class diagram.

Transport Interface
package com.bigboxcode.composite;
public interface Transport {
void start();
void operate();
void stop();
}
Bike Class
package com.bigboxcode.composite;
public class Bike implements Transport{
@Override
public void start() {
System.out.println("Starting Bike...");
}
@Override
public void operate() {
System.out.println("Riding Bike");
}
@Override
public void stop() {
System.out.println("Stopping Bike...");
}
}
Plane Class
package com.bigboxcode.composite;
public class Plane implements Transport {
@Override
public void start() {
System.out.println("Starting Plane...");
}
@Override
public void operate() {
System.out.println("Flying Plane");
}
@Override
public void stop() {
System.out.println("Stopping Plane...");
}
}
Car Class
package com.bigboxcode.composite;
public class Car implements Transport {
@Override
public void start() {
System.out.println("Starting Car...");
}
@Override
public void operate() {
System.out.println("Driving Car");
}
@Override
public void stop() {
System.out.println("Stopping Car...");
}
}
Composite Class
package com.bigboxcode.composite;
import java.util.ArrayList;
import java.util.List;
public class TransportGroup implements Transport {
private List<Transport> transportList = new ArrayList<Transport>();
@Override
public void start() {
for (Transport transport: transportList) {
transport.start();
}
}
@Override
public void operate() {
for (Transport transport: transportList) {
transport.operate();
}
}
@Override
public void stop() {
for (Transport transport: transportList) {
transport.stop();
}
}
public void addTransport(Transport transport) {
transportList.add(transport);
}
public void removeTransport(Transport transport) {
transportList.remove(transport);
}
}
Demo
package com.bigboxcode.composite;
public class Main {
public static void main(String[] args) {
Bike bike = new Bike();
Plane plane = new Plane();
Car car = new Car();
Car secondCar = new Car();
TransportGroup transports = new TransportGroup();
transports.addTransport(bike);
transports.addTransport(plane);
transports.addTransport(car);
transports.addTransport(secondCar);
System.out.println("-----------------Output with 4 transports------------------\n");
transports.start();
transports.operate();
transports.stop();
System.out.println("\n-----------------Output when plane is removed------------------\n");
transports.removeTransport(plane);
transports.start();
transports.operate();
transports.stop();
}
}
Output
The output of the demo above will be like below.
-----------------Output with 4 transports------------------
Starting Bike...
Starting Plane...
Starting Car...
Starting Car...
Riding Bike
Flying Plane
Driving Car
Driving Car
Stopping Bike...
Stopping Plane...
Stopping Car...
Stopping Car...
-----------------Output when plane is removed------------------
Starting Bike...
Starting Car...
Starting Car...
Riding Bike
Driving Car
Driving Car
Stopping Bike...
Stopping Car...
Stopping Car...
Source Code
Use the following link to get the source code:
Example | Source Code Link |
---|---|
Example #1: Transport | ![]() |