-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
house construction problem statement
- Loading branch information
1 parent
ef73c9c
commit 3853489
Showing
11 changed files
with
262 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
# Design Patterns Learning | ||
|
||
## Overview | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Learning_2.0/Problems/Behavioural-pattern-problems/online-order-food-system.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Command Pattern: Online Food Ordering System | ||
|
||
## Overview | ||
|
||
This project implements an **Online Food Ordering System** using the **Command Pattern**. The system encapsulates user requests such as adding, removing, processing, and canceling orders as commands, allowing for easy modification, undo, and redo operations. The system is flexible and scalable, enabling new actions to be added seamlessly without altering the core logic. | ||
|
||
## Features | ||
|
||
- **Command-based Order Management**: Encapsulates actions such as adding items, removing items, processing the order, and canceling it. | ||
- **Undo/Redo Functionality**: Users can undo or redo their actions, providing flexibility during the order process. | ||
- **Dynamic Command Execution**: Supports dynamic execution and modification of commands without affecting the system's core. | ||
- **Extensible System**: Easily extendable to accommodate new commands for managing orders. | ||
|
||
## Internal Structure | ||
|
||
1. **Command Interface (OrderCommand)** | ||
|
||
- Provides a unified interface for executing, undoing, and redoing commands. | ||
|
||
2. **Concrete Commands** | ||
|
||
- **AddItemCommand**: Adds an item to the current order. | ||
- **RemoveItemCommand**: Removes an item from the current order. | ||
- **ProcessOrderCommand**: Finalizes and processes the order for checkout. | ||
- **CancelOrderCommand**: Cancels the current order entirely. | ||
|
||
3. **Receiver (Order)** | ||
|
||
- Handles the logic for managing items in the order, including adding, removing, and resetting items. | ||
|
||
4. **Invoker (OrderManager)** | ||
|
||
- Manages the execution of commands and stores history to allow for undo and redo actions. | ||
|
||
5. **Client Application** | ||
- Allows users to interact with the system by adding or removing items, processing orders, canceling them, or undoing/redoing recent actions. | ||
|
||
## How to Use | ||
|
||
1. **Create an Order**: Start by initializing a new order. | ||
2. **Add/Remove Items**: Use commands to add or remove items from the order. | ||
3. **Process or Cancel Orders**: Once the items are finalized, process the order for checkout or cancel it. | ||
4. **Undo/Redo Actions**: At any point, undo or redo any previous actions to modify the order. | ||
|
||
## Solution | ||
|
||
You can find the solution to this problem in the [solution folder](/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Learning_2.0/Problems/Creational-pattern-problems/house-construction-system.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Builder Pattern: House Construction System | ||
|
||
## Overview | ||
|
||
This project demonstrates a **House Construction System** using the **Builder Pattern**. The system constructs various types of houses (e.g., Wooden, Brick, or Glass) by allowing step-by-step construction, giving the client control over the process. The builder pattern separates the construction of complex objects from their representation, making the system flexible and extendable. | ||
|
||
## Features | ||
|
||
- **Customizable House Construction**: The system supports building houses with various components like walls, doors, windows, and roofs. | ||
- **Different House Types**: It supports building Wooden, Brick, and Glass houses with different material properties and design features. | ||
- **Modular Construction**: The house construction can be customized step-by-step, allowing dynamic modifications during the build process. | ||
- **Director for Construction Process**: A `Director` orchestrates the construction process for creating predefined house types. | ||
|
||
## Internal Structure | ||
|
||
1. **Builder (HouseBuilder)** | ||
|
||
- Abstract interface defining the steps required to construct a house. | ||
- Provides methods to build each component of the house (walls, doors, windows, and roof). | ||
|
||
2. **Concrete Builders (WoodenHouseBuilder, BrickHouseBuilder, GlassHouseBuilder)** | ||
|
||
- Implement the specific details of constructing different house types (Wooden, Brick, Glass). | ||
- Each builder provides a method for creating a house with different materials and layouts. | ||
|
||
3. **Product (House)** | ||
|
||
- Represents the final house object constructed by the builder. | ||
- Contains properties like the number of walls, doors, windows, and roof material. | ||
|
||
4. **Director (ConstructionDirector)** | ||
- Responsible for managing the building process. | ||
- Provides methods for constructing specific types of houses in a predefined sequence. | ||
|
||
## How to Use | ||
|
||
1. **Choose a House Type**: Select the type of house (Wooden, Brick, Glass) using the respective builder. | ||
2. **Step-by-Step Construction**: Use the builder methods to add walls, doors, windows, and roofs. | ||
3. **Build the House**: The director orchestrates the process to create a complete house. | ||
4. **View the Result**: After construction is complete, retrieve the final house object with its details. | ||
|
||
## Solution | ||
|
||
You can find the solution to this problem in the [solution folder](/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/). |
14 changes: 14 additions & 0 deletions
14
...ning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/construction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Director class (Construction Director) | ||
class ConstructionDirector { | ||
setBuilder(builder) { | ||
this.builder = builder; | ||
} | ||
constructHouse() { | ||
this.builder.buildWalls(); | ||
this.builder.buildDoors(); | ||
this.builder.buildWindows(); | ||
this.builder.buildRoof(); | ||
} | ||
} | ||
|
||
module.exports = ConstructionDirector; |
18 changes: 18 additions & 0 deletions
18
Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/house.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class House { | ||
constructor() { | ||
this.walls = 0; | ||
this.doors = 0; | ||
this.windows = 0; | ||
this.roof = ""; | ||
this.material = ""; | ||
} | ||
|
||
displayHouse() { | ||
console.log("Congratulations! Your house is ready."); | ||
console.log( | ||
`The materials count are :\n -Walls: ${this.walls} \n -Roof: ${this.roof} \n -Windows: ${this.windows} \n -Doors: ${this.doors} \n -Material: ${this.material}` | ||
); | ||
} | ||
} | ||
|
||
module.exports = House; |
94 changes: 94 additions & 0 deletions
94
...ning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/houseBuilder.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const House = require("./house"); | ||
|
||
// HouseBuilder interface (Builder) | ||
class HouseBuilder { | ||
buildWalls() { | ||
throw new Error("Method buildWalls() must be implemented"); | ||
} | ||
buildDoors() { | ||
throw new Error("Method buildDoors() must be implemented"); | ||
} | ||
buildWindows() { | ||
throw new Error("Method buildWindows() must be implemented"); | ||
} | ||
buildRoof() { | ||
throw new Error("Method buildRoof() must be implemented"); | ||
} | ||
getHouse() { | ||
throw new Error("Method getHouse() must be implemented"); | ||
} | ||
} | ||
|
||
// Concrete Builder for Wooden House | ||
class WoodenHouseBuilder extends HouseBuilder { | ||
constructor() { | ||
super(); | ||
this.house = new House(); | ||
this.house.material = "Wood"; | ||
} | ||
buildWalls() { | ||
this.house.walls = 4; | ||
} | ||
buildDoors() { | ||
this.house.doors = 2; | ||
} | ||
buildWindows() { | ||
this.house.windows = 4; | ||
} | ||
buildRoof() { | ||
this.house.roof = "Wooden Roof"; | ||
} | ||
getHouse() { | ||
return this.house; | ||
} | ||
} | ||
|
||
// Concrete Builder for Brick House | ||
class BrickHouseBuilder extends HouseBuilder { | ||
constructor() { | ||
super(); | ||
this.house = new House(); | ||
this.house.material = "Brick"; | ||
} | ||
buildWalls() { | ||
this.house.walls = 6; | ||
} | ||
buildDoors() { | ||
this.house.doors = 3; | ||
} | ||
buildWindows() { | ||
this.house.windows = 6; | ||
} | ||
buildRoof() { | ||
this.house.roof = "Concrete Roof"; | ||
} | ||
getHouse() { | ||
return this.house; | ||
} | ||
} | ||
|
||
// Concrete Builder for Glass House | ||
class GlassHouseBuilder extends HouseBuilder { | ||
constructor() { | ||
super(); | ||
this.house = new House(); | ||
this.house.material = "Glass"; | ||
} | ||
buildWalls() { | ||
this.house.walls = 4; | ||
} | ||
buildDoors() { | ||
this.house.doors = 1; | ||
} | ||
buildWindows() { | ||
this.house.windows = 8; | ||
} | ||
buildRoof() { | ||
this.house.roof = "Glass Roof"; | ||
} | ||
getHouse() { | ||
return this.house; | ||
} | ||
} | ||
|
||
module.exports = { WoodenHouseBuilder, BrickHouseBuilder, GlassHouseBuilder }; |
30 changes: 30 additions & 0 deletions
30
Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { | ||
WoodenHouseBuilder, | ||
BrickHouseBuilder, | ||
GlassHouseBuilder, | ||
} = require("./houseBuilder"); | ||
const ConstructionDirector = require("./construction"); | ||
|
||
// Initialize Director | ||
const director = new ConstructionDirector(); | ||
|
||
// Build a Wooden House | ||
const woodenHouseBuilder = new WoodenHouseBuilder(); | ||
director.setBuilder(woodenHouseBuilder); | ||
director.constructHouse(); | ||
const woodenHouse = woodenHouseBuilder.getHouse(); | ||
woodenHouse.displayHouse(); | ||
|
||
// Build a Brick House | ||
const brickHouseBuilder = new BrickHouseBuilder(); | ||
director.setBuilder(brickHouseBuilder); | ||
director.constructHouse(); | ||
const brickHouse = brickHouseBuilder.getHouse(); | ||
brickHouse.displayHouse(); | ||
|
||
// Build a Glass House | ||
const glassHouseBuilder = new GlassHouseBuilder(); | ||
director.setBuilder(glassHouseBuilder); | ||
director.constructHouse(); | ||
const glassHouse = glassHouseBuilder.getHouse(); | ||
glassHouse.displayHouse(); |