diff --git a/Learning_1.0/README.md b/Learning_1.0/README.md index 5fc8f8c..4f54d8c 100644 --- a/Learning_1.0/README.md +++ b/Learning_1.0/README.md @@ -1,4 +1,3 @@ - # Design Patterns Learning ## Overview diff --git a/Learning_2.0/Interview_Questions/Questions.md b/Learning_2.0/Interview_Questions/Questions.md index 2972093..722642f 100644 --- a/Learning_2.0/Interview_Questions/Questions.md +++ b/Learning_2.0/Interview_Questions/Questions.md @@ -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 diff --git a/Learning_2.0/Problems/Behavioural-pattern-problems/discount-calculation-system.md b/Learning_2.0/Problems/Behavioural-pattern-problems/discount-calculation-system.md index 729597f..b4a388f 100644 --- a/Learning_2.0/Problems/Behavioural-pattern-problems/discount-calculation-system.md +++ b/Learning_2.0/Problems/Behavioural-pattern-problems/discount-calculation-system.md @@ -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. diff --git a/Learning_2.0/Problems/Behavioural-pattern-problems/online-order-food-system.md b/Learning_2.0/Problems/Behavioural-pattern-problems/online-order-food-system.md new file mode 100644 index 0000000..493b1b7 --- /dev/null +++ b/Learning_2.0/Problems/Behavioural-pattern-problems/online-order-food-system.md @@ -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/). diff --git a/Learning_2.0/Problems/Behavioural-pattern-problems/smart-home-automation.md b/Learning_2.0/Problems/Behavioural-pattern-problems/smart-home-automation.md index 06a57bf..7087820 100644 --- a/Learning_2.0/Problems/Behavioural-pattern-problems/smart-home-automation.md +++ b/Learning_2.0/Problems/Behavioural-pattern-problems/smart-home-automation.md @@ -16,9 +16,11 @@ 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. @@ -26,9 +28,11 @@ The project is organized into the following components: - **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** diff --git a/Learning_2.0/Problems/Behavioural-pattern-problems/stock-mark-sitmulation.md b/Learning_2.0/Problems/Behavioural-pattern-problems/stock-mark-sitmulation.md index afb59c7..dd50aa5 100644 --- a/Learning_2.0/Problems/Behavioural-pattern-problems/stock-mark-sitmulation.md +++ b/Learning_2.0/Problems/Behavioural-pattern-problems/stock-mark-sitmulation.md @@ -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. diff --git a/Learning_2.0/Problems/Creational-pattern-problems/house-construction-system.md b/Learning_2.0/Problems/Creational-pattern-problems/house-construction-system.md new file mode 100644 index 0000000..9b44b17 --- /dev/null +++ b/Learning_2.0/Problems/Creational-pattern-problems/house-construction-system.md @@ -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/). diff --git a/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/construction.js b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/construction.js new file mode 100644 index 0000000..e05675c --- /dev/null +++ b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/construction.js @@ -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; diff --git a/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/house.js b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/house.js new file mode 100644 index 0000000..9e4cb34 --- /dev/null +++ b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/house.js @@ -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; diff --git a/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/houseBuilder.js b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/houseBuilder.js new file mode 100644 index 0000000..8b11c95 --- /dev/null +++ b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/houseBuilder.js @@ -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 }; diff --git a/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/main.js b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/main.js new file mode 100644 index 0000000..801efea --- /dev/null +++ b/Learning_2.0/Solutions/Creational-pattern-solutions/house-construction-system/main.js @@ -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();