Skip to content

the Composite pattern facilitates the creation of structures where both individual objects and compositions of objects can be handled uniformly, streamlining the management of intricate hierarchies and tree-like structures.

Notifications You must be signed in to change notification settings

akifislam/Composite-Design-Pattern-with-Generic-Housing

Repository files navigation

A demonstration of Composite Design Pattern

Course Title : Object Oriented Design and Design Patterns Lab(CSE 4122)

Author

Akif Islam
Department of CSE, University of Rajshahi
iamakifislam@gmail.com


Experiment 04

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.

Story of the Program

This design pattern follows the basic component-composite-leaf model of Composite Design Pattern.

Class Diagram

How to Run?

  $javac CompositePatternDemo.java
  $java CompositePatternDemo

Composite Pattern Demo Program

// 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();

Output

About

the Composite pattern facilitates the creation of structures where both individual objects and compositions of objects can be handled uniformly, streamlining the management of intricate hierarchies and tree-like structures.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages