Composite pattern: in this post, we will try to understand the importance of the composite pattern with a real-time example. Composite Design Pattern Java is
one of the structural design patterns. As developers, we want to perform the same operation on a single object and group of objects. For example, there is a tech seminar hosted for all developers. Even a single person can give a presentation . or else the team is also eligible for a presentation. In these two things, presentation is common among single and team presentations. composite deals with trees. In programming, we have lots of trees everywhere.
a tree is a combination of branches and nodes. Let’s discuss what branch? A node consisting of another group node is referred to as a branch. the leaf is nothing but they don’t contain the other nodes or groups.
In java, there is the concept of varargs. which will be used to pass single or multiple variables to the same function.
public String formatText(String... values){
}
this formatText function having notation '...' which will accept a single variable or multiple variables.
when to use the composite design pattern?
1. when you want to treat a single or group of objects in the same manner.
2. same code that handles the single or group of objects.
Composite Design Pattern Java real time example:
import java.util.ArrayList;
import java.util.List;
interface Vehicle
{
public void vehicleInfo();
}
class Bike implements Vehicle
{
int vehicleNumber;
int speed;
public Bike(int vehicleNumber, int speed) {
this.vehicleNumber = vehicleNumber;
this.speed = speed;
}
@Override
public void vehicleInfo() {
System.out.println("Vehicle Number ::"+vehicleNumber+" speed::"+speed);
}
}
class Car implements Vehicle
{
int vehicleNumber;
int speed;
public Car(int vehicleNumber, int speed) {
this.vehicleNumber = vehicleNumber;
this.speed = speed;
}
@Override
public void vehicleInfo() {
System.out.println("Vehicle Number ::"+vehicleNumber+" speed::"+speed);
}
}
class VehicleDirectory
{
List<Vehicle> vehiclesList= new ArrayList();
public List<Vehicle> getVehiclesList() {
return vehiclesList;
}
public void setVehiclesList(List<Vehicle> vehiclesList) {
this.vehiclesList = vehiclesList;
}
public void showInfo()
{
for(Vehicle vehicleObj :vehiclesList ) {
vehicleObj.vehicleInfo();
}
}
}
public class CompositeDesignPatternDemo {
public static void main(String[] args) {
Bike bike = new Bike(1234, 100);
Car car = new Car(785, 200);
List<Vehicle> vehiclesList= new ArrayList();
vehiclesList.add(bike);
vehiclesList.add(car);
VehicleDirectory vehDirectory = new VehicleDirectory();
vehDirectory.setVehiclesList(vehiclesList);
vehDirectory.showInfo();
}
}
Expected Output:
Vehicle Number ::1234 speed::100
Vehicle Number ::785 speed::200
in the above example, the vehicle directory works for both different vehicles such as cars and bikes. there is a function called showInfo() in the VehicleDirectory class, which supports two different types of vehicles, and it displays information of both. Thanks for your support. hope you have a good understanding of Composite Design Pattern Java 🙂 We will explain in this blog.
Leave a comment