Akif Islam
Department of CSE, University of Rajshahi
iamakifislam@gmail.com
Write a Java program to demonstrate the implementation of a composite design pattern by using an example with a building composed of generic housing structures.
This design pattern follows the basic component-composite-leaf model of Composite Design Pattern.
$javac CompositePatternDemo.java
$java CompositePatternDemo
// Creating Leaf Node : Bathroom, Kitchen and Bedroom
Bathroom bathroom = new Bathroom();
Kitchen kitchen = new Kitchen();
Bedroom bedroom = new Bedroom();
// Sending Bathroom, Kitchen, Bedroom to its Composite Class ---> Living Room
LivingRoom livingRoom = new LivingRoom();
livingRoom.addRoom(bathroom);
livingRoom.addRoom(kitchen);
livingRoom.addRoom(bedroom);
livingRoom.getSubordinates();
// Creating Leaf Node : Elevator, Staircase and Swimming Pool
Elevator elevator = new Elevator();
Staircase staircase = new Staircase();
SwimmingPool swimmingPool = new SwimmingPool();
// Sending Elevator, Staircase, Swimming Pool to its Composite Class ---> Common Area
CommonArea commonArea = new CommonArea();
commonArea.addRoom(elevator);
commonArea.addRoom(staircase);
commonArea.addRoom(swimmingPool);
commonArea.getSubordinates();
Appartment appartment = new Appartment();
appartment.addRoom(commonArea);
appartment.addRoom(livingRoom);
appartment.roomInfo();