Skip to content

A comprehensive collection of low-level design patterns in C++, showcasing practical implementations of creational, structural, and behavioral patterns to enhance software design and architecture.

Notifications You must be signed in to change notification settings

ranamak/Low-Level-Design-Patterns

 
 

Repository files navigation

LOW-LEVEL-DESIGN-PATTERNS

Crafting brilliance through low-level design mastery.

license last-commit repo-top-language repo-language-count


Table of Contents


Overview

The Low-Level-Design-Patterns project offers a collection of practical code examples showcasing various design patterns like Builder, Singleton, and Factory. It simplifies creating complex objects, managing database connections efficiently, and enabling dynamic behaviors in software systems. Ideal for developers seeking hands-on guidance in designing robust and scalable applications.


Features

Folder Summary
⚙️ Creational Patterns
  • Builder: Separates the construction of a complex object from its representation.
  • Singleton: Ensures a class has only one instance and provides a global point of access to it.
  • Factory: Defines an interface for creating an object, but lets subclasses alter the type of objects that will be created.
🔩 Structural Patterns
  • Adapter: Allows incompatible interfaces to work together.
  • Bridge: Decouples an abstraction from its implementation so that the two can vary independently.
  • Composite: Composes objects into tree structures to represent part-whole hierarchies.
  • Decorator: Adds additional responsibilities to an object dynamically.
  • Facade: Provides a simplified interface to a complex subsystem.
  • Flyweight: Reduces the cost of creating and manipulating a large number of similar objects.
  • Proxy: Provides a surrogate or placeholder for another object to control access to it.
📄 Behavioral Patterns
  • Chain of Responsibility: Passes a request along a chain of handlers.
  • Command: Encapsulates a request as an object, thereby allowing for parameterization and queuing of requests.
  • Interpreter: Defines a grammatical representation for a language and an interpreter to interpret the grammar.
  • Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • Mediator: Defines an object that encapsulates how a set of objects interact.
  • Memento: Captures and externalizes an object's internal state without violating encapsulation.
  • Observer: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • State: Allows an object to alter its behavior when its internal state changes.
  • Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
  • Template Method: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.
  • Visitor: Represents an operation to be performed on the elements of an object structure.

Project Structure

└── Low-Level-Design-Patterns/
    ├── README.md
    ├── behavioral-design-pattern
    │   ├── chain-of-responsibility-design-pattern.cpp
    │   ├── command-design-pattern.cpp
    │   ├── interpreter-design-pattern.cpp
    │   ├── iterator-design-pattern.cpp
    │   ├── mediator-design-pattern.cpp
    │   ├── momento-design-pattern.cpp
    │   ├── observer-design-pattern.cpp
    │   ├── state-design-pattern.png
    │   ├── strategy-design-pattern.cpp
    │   ├── template-design-pattern.cpp
    │   └── visitor-design-pattern.cpp
    ├── creational-design-pattern
    │   ├── builder-design-pattern.cpp
    │   ├── factory-design-pattern.cpp
    │   └── singleton-design-pattern.cpp
    └── structural-design-pattern
        ├── adapter-design-pattern.cpp
        ├── bridge-design-pattern.cpp
        ├── composite-design-pattern.cpp
        ├── decorator-design-pattern.cpp
        ├── facade-design-pattern.png
        ├── flyweight-design-pattern-with-singleton-concept.cpp
        ├── flyweight-design-pattern.cpp
        └── proxy-design-pattern.cpp

Project Index

LOW-LEVEL-DESIGN-PATTERNS/
creational-design-pattern
builder-design-pattern.cpp - Demonstrates the Builder design pattern for creating complex objects step by step
- Encapsulates object construction, enabling easy management and extension
- Concrete builders like EngineeringStudentBuilder and MBAStudentBuilder set specific attributes for different types of students
- Main function showcases building and printing details of Engineering and MBA students.
singleton-design-pattern.cpp - Implements singleton design patterns for database connections with eager, lazy, and double-checked locking initialization methods
- Provides global access to single instances of classes, ensuring thread safety and efficient resource usage
- Demonstrates different strategies for creating and accessing singleton objects in the codebase architecture.
factory-design-pattern.cpp - Implements Factory Pattern to create various shapes like Circle, Square, and Rectangle
- Defines Shape superclass with draw method and concrete classes for each shape
- ShapeFactory class generates shape objects based on input strings
- Demonstrates pattern usage by creating a Rectangle object and calling its draw method.
behavioral-design-pattern
strategy-design-pattern.cpp - Demonstrates the Strategy Design Pattern for dynamic driving behaviors in vehicles
- Encapsulates various driving strategies (e.g., Sport, Normal, Passenger) as separate classes
- The Vehicle class can change strategies dynamically, promoting flexibility and scalability without modifying the Vehicle class itself.
template-design-pattern.cpp - Implements a Template Method Design Pattern for payment processing
- Defines a base class with common steps and abstract methods for subclasses to implement specific payment types
- Demonstrates sending money to a friend and a merchant by overriding the template methods
- Executed through instances of PayToFriend and PayToMerch classes.
observer-design-pattern.cpp - Implements observer design pattern for stock updates
- Defines observables for iPhone and Samsung stocks, with mobile and email alert observers
- Demonstrates notifications to observers based on stock count changes.
chain-of-responsibility-design-pattern.cpp - Implements a Chain of Responsibility design pattern for logging messages based on their severity levels
- The code defines different log processors for handling INFO, DEBUG, and ERROR messages in a chain
- By passing messages through the chain, it ensures that each message is processed by the appropriate log processor without the sender needing to know the specific handler.
mediator-design-pattern.cpp - Demonstrates the Mediator Design Pattern, fostering loose coupling between objects by centralizing communication through a mediator
- Objects interact indirectly via the mediator, enhancing system maintainability and flexibility
- The code showcases how bidders in an auction place bids and receive notifications through the mediator, streamlining communication and reducing dependencies.
interpreter-design-pattern.cpp - Demonstrates the Interpreter Design Pattern by evaluating complex mathematical expressions using terminal and non-terminal expressions within a defined context
- The code creates a context to store variable values, constructs expressions for multiplication and addition, and outputs the result of the expression evaluation.
command-design-pattern.cpp - Implements the Command Design Pattern to decouple command execution from command producers
- Defines commands for an Air Conditioner, allowing operations like turning on/off and setting temperature
- Utilizes an invoker class to manage and execute commands, showcasing features like undo/redo functionality.
visitor-design-pattern.cpp - Implements Visitor Design Pattern to separate operations (pricing, maintenance) from room classes
- Defines visitors for different room types to perform specific operations without modifying room classes
- Main function demonstrates pricing and maintenance operations on single, double, and deluxe rooms using respective visitor instances.
iterator-design-pattern.cpp - Implements the Iterator Design Pattern to enable sequential access to a collection of books without exposing its structure
- The code defines classes for books, iterators, and libraries, showcasing how to iterate over and print book names in a library using an iterator.
momento-design-pattern.cpp - Demonstrates the Memento Design Pattern by enabling objects to be restored to previous states without revealing implementation details
- The code showcases saving and restoring object states using Mementos managed by a CareTaker, facilitating undo/redo functionality within the project's architecture.
structural-design-pattern
flyweight-design-pattern.cpp - Demonstrates the Flyweight Design Pattern to optimize memory usage by sharing common data among similar objects
- The code defines interfaces for letters, concrete letter implementations, and a factory to manage object creation and reuse
- By creating and displaying letters using the factory, it showcases efficient memory utilization through object sharing.
bridge-design-pattern.cpp - Demonstrates the Bridge Design Pattern by decoupling abstractions from implementations, allowing independent variation
- Defines breathing processes for land and water entities, showcasing how Dogs and Fish can use different implementors
- This promotes extensibility and flexibility in managing diverse living things.
flyweight-design-pattern-with-singleton-concept.cpp - Demonstrates the Flyweight Design Pattern by efficiently managing memory usage through object sharing
- The code showcases the creation and reuse of letter objects using a Singleton Factory, emphasizing memory optimization for large quantities of similar objects.
decorator-design-pattern.cpp - Implements the Decorator Design Pattern for pizzas, allowing dynamic addition of toppings to base pizza objects
- Demonstrates how to enhance pizza objects with Mushroom and Cheese toppings while maintaining scalability and reusability in the codebase architecture.
adapter-design-pattern.cpp - Implements the Adapter Design Pattern to bridge incompatible interfaces, converting weight from pounds to kilograms
- The code creates classes for weighing machines in pounds and kilograms, demonstrating adaptation between the two units.
proxy-design-pattern.cpp - Implements a Proxy Design Pattern to control access to an Employee Database
- The code defines classes for Employee, EmployeeDB, EmployeeDBImpl, and EmployeeDBProxy
- The proxy restricts access based on client permissions, allowing creation and retrieval of employee records
- The main function demonstrates creating an employee record and retrieving it based on client roles.
composite-design-pattern.cpp - Demonstrates the Composite Design Pattern by creating a hierarchical structure of directories and files
- Allows uniform treatment of individual objects and compositions
- Enables representing part-whole hierarchies where files and directories are handled uniformly
- The code showcases how directories can contain files and subdirectories in a tree-like manner.

Getting Started

Prerequisites

Before getting started with Low-Level-Design-Patterns, ensure your runtime environment meets the following requirements:

  • Programming Language: CPP

Installation

Install Low-Level-Design-Patterns using one of the following methods:

Build from source:

  1. Clone the Low-Level-Design-Patterns repository:
❯ git clone https://github.com/samyakmehta28/Low-Level-Design-Patterns
  1. Navigate to the project directory:
cd Low-Level-Design-Patterns

Contributing

Contributing Guidelines
  1. Fork the Repository: Start by forking the project repository to your github account.
  2. Clone Locally: Clone the forked repository to your local machine using a git client.
    git clone https://github.com/samyakmehta28/Low-Level-Design-Patterns
  3. Create a New Branch: Always work on a new branch, giving it a descriptive name.
    git checkout -b new-feature-x
  4. Make Your Changes: Develop and test your changes locally.
  5. Commit Your Changes: Commit with a clear message describing your updates.
    git commit -m 'Implemented new feature x.'
  6. Push to github: Push the changes to your forked repository.
    git push origin new-feature-x
  7. Submit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
  8. Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
Contributor Graph


License

This project is protected under the SELECT-A-LICENSE License. For more details, refer to the LICENSE file.


Acknowledgments

  • I would like to express my sincere gratitude to Shreyansh Jain for his invaluable contributions through his YouTube playlist on Low-Level System Design. This collection of C++ implementations of various design patterns was primarily inspired by the examples presented in his videos. While I have closely followed the examples provided in his content, I have adapted and implemented them in C++.

  • You can find Shreyansh Jain's YouTube playlist here: Low-Level System Design Playlist.


About

A comprehensive collection of low-level design patterns in C++, showcasing practical implementations of creational, structural, and behavioral patterns to enhance software design and architecture.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%