Skip to content

Commit

Permalink
house construction problem statement
Browse files Browse the repository at this point in the history
  • Loading branch information
PrasanthVijayy committed Oct 17, 2024
1 parent ef73c9c commit 3853489
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 7 deletions.
1 change: 0 additions & 1 deletion Learning_1.0/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Design Patterns Learning

## Overview
Expand Down
2 changes: 1 addition & 1 deletion Learning_2.0/Interview_Questions/Questions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Design Patterns Questions

This document contains a collection of questions related to various design patterns, categorized into General, Behavioral, Structural, Creational, and Advanced Questions with ***total 50 questions***
This document contains a collection of questions related to various design patterns, categorized into General, Behavioral, Structural, Creational, and Advanced Questions with **_total 50 questions_**

## Common Design Pattern Questions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ This project implements a **Discount Calculation System** for an e-commerce plat

## Internal Structure

1. **Context (DiscountCalculator)**
- Holds a reference to the customer type discount strategy and seasonal discount strategy.
1. **Context (DiscountCalculator)**

- Holds a reference to the customer type discount strategy and seasonal discount strategy.
- Provides methods to set or change these strategies dynamically.
- Responsible for combining both discount strategies to calculate the final price.

2. **Strategy (CustomerDiscountStrategy)**
- Defines the interface for customer-type discount calculation.
2. **Strategy (CustomerDiscountStrategy)**

- Defines the interface for customer-type discount calculation.
- Includes a method for calculating the base discount for each customer type.

3. **Concrete Strategies for Customer Types (RegularCustomer, PremiumCustomer, VIPCustomer)**
3. **Concrete Strategies for Customer Types (RegularCustomer, PremiumCustomer, VIPCustomer)**
- Implements the customer discount calculation for different customer types:
- **RegularCustomer**: No discount.
- **PremiumCustomer**: 10% discount.
Expand Down
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/).
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ This project implements the Command Pattern to create a Smart Home Automation Sy
The project is organized into the following components:

1. **Command Interface**

- Defines a common interface for all command objects, ensuring they implement the `execute` and `undo` methods.

2. **Concrete Command Classes**

- **LightOnCommand**: Implements the Command interface to turn the light on.
- **LightOffCommand**: Implements the Command interface to turn the light off.
- **FanOnCommand**: Implements the Command interface to turn the fan on.
- **FanOffCommand**: Implements the Command interface to turn the fan off.
- **ThermostatSetCommand**: Implements the Command interface to set the thermostat to a specified temperature.

3. **Receiver Classes**

- Represents the actual smart devices (e.g., `Light`, `Fan`, `Thermostat`) that will be controlled by the commands.

4. **Command Invoker**

- Manages command execution, maintains a queue of commands, and handles the undo operation.

5. **Client Code**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ This project implements the Observer Pattern through a stock market simulation w
The project is organized into the following components:

1. **Subject (StockMarket)**

- Responsible for maintaining a list of observers (investors).
- Provides methods to register, unregister, and notify observers when stock prices change.

2. **Observer (Investor)**

- Defines the interface for observers that want to be notified of changes in the subject.
- Implements the update method to receive stock price updates.

3. **ConcreteSubject (ConcreteStockMarket)**

- Implements the Subject interface and holds the state (stock prices).
- Triggers notifications to observers when stock prices change.

Expand Down
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/).
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;
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;
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 };
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();

0 comments on commit 3853489

Please sign in to comment.