Let’s consider a car. Car has an engine and a tire. The engine is made up of some electrical components and valves. The electrical components would further consist of some chips and transistors. These components make a complex object called a car.First, we will classify Leaf and Composite in our complex object (car). Here car engine’s electrical components are composite objects. Here car has child objects, engine and tires and engine has child objects, components, and valves. While tires, valves, and electrical components do not consist of any child objects. So these considered as Leaf objects. Let’s suppose if you apply some operation on these child objects. For example “getPrice()” is a method which can be applied on transistor object, chips object, valves, and tires. Here the pricing is being applied on these leaf objects. Then that operation should also be applied on the composite objects which are car and engine components. So with the leaf objects, you can also get the price of the car and engine components. As all the rules are applied, this would be considered as Composite Design Pattern. There are various criteria and use cases in real life examples where you would use Composite Design Pattern to solve real-time problems.
public abstract class Employee { public abstract void printEmployeeDetails(); }
public class Manager extends Employee { protected String name; protected long empID; protected String designation; public Manager(String name, long empID) { this.name = name; this.empID = empID; } @Override public void printEmployeeDetails() { System.out.println("Name:" + name + " empID:" + empID + " Designation:Manager"); } }
public class Developer extends Employee { protected String name; protected long empID; protected String designation; public Developer(String name, long empID) { this.name = name; this.empID = empID; } @Override public void printEmployeeDetails() { System.out.println("Name:" + name + " empID:" + empID + " Designation:Developer"); } }
public class CompanyDirectory extends Employee { private List employeeList; public CompanyDirectory() { employeeList = new ArrayList<>(); } @Override public void printEmployeeDetails() { for (Employee employee : employeeList) { employee.printEmployeeDetails(); } } public void addEmployee(Employee employee) { employeeList.add(employee); } }
public class CompanyDetails extends CompanyDirectory { private String companyName; public CompanyDetails(String companyName) { this.companyName=companyName; } public void showCompanyName() { System.out.println(this.companyName); } }
public class Company { public static void main(String[] args) { Employee d1 = new Developer("Lenin", 10); Employee d2 = new Developer("Chenchu", 11); Employee d3 = new Developer("Raja", 12); Employee m1 = new Manager("Rakesh", 1); CompanyDetails companyDetails = new CompanyDetails("XYZ"); companyDetails.addEmployee(m1); companyDetails.addEmployee(d1); companyDetails.addEmployee(d2); companyDetails.addEmployee(d3); companyDetails.printEmployeeDetails(); } }