A comprehensive collection of Low-Level Design (LLD) patterns implemented in Java. This repository demonstrates practical implementations of Gang of Four (GoF) design patterns and SOLID principles with real-world examples.
- About
- Design Patterns
- Project Structure
- Getting Started
- How to Run
- Pattern Documentation
- Contributing
This repository contains implementations of various design patterns to help developers understand:
- When and why to use specific patterns
- How to implement them in Java
- Real-world use cases and examples
- Best practices and SOLID principles
Each pattern includes working code examples with practical scenarios like payment systems, vehicle management, weather monitoring, and more.
Patterns that deal with object creation mechanisms.
- Abstract Factory - Creates families of related objects without specifying concrete classes
- Builder - Constructs complex objects step by step
- Factory - Creates objects without specifying the exact class
- Prototype - Creates new objects by cloning existing ones
Patterns that deal with object composition and relationships.
- Adapter - Converts interface of a class into another interface
- Bridge - Decouples abstraction from implementation
- Composite - Composes objects into tree structures
- Decorator - Adds new functionality to objects dynamically
- Facade - Provides simplified interface to complex subsystems
- Flyweight - Shares common state between multiple objects
- Proxy - Provides placeholder for another object
Patterns that deal with object collaboration and responsibility.
- Chain of Responsibility - Passes request along chain of handlers
- Command - Encapsulates requests as objects
- Mediator - Centralizes complex communications
- Null Object - Provides default behavior for null references
- Observer - Notifies multiple objects about state changes
- State - Alters object behavior when state changes
- Strategy - Defines family of interchangeable algorithms
- Visitor - Separates algorithms from object structure
- Liskov Substitution Principle - Subtypes must be substitutable for their base types
design-patterns-java/
├── src/
│ └── com/
│ └── designpatterns/
│ ├── abstractfactory/ # Abstract Factory Pattern
│ ├── adapter/ # Adapter Pattern
│ ├── bridge/ # Bridge Pattern
│ ├── builder/ # Builder Pattern
│ ├── chainofresponsibility/ # Chain of Responsibility
│ ├── command/ # Command Pattern
│ ├── composite/ # Composite Pattern
│ ├── decorator/ # Decorator Pattern
│ ├── facade/ # Facade Pattern
│ ├── factory/ # Factory Pattern
│ ├── flyweight/ # Flyweight Pattern
│ ├── liskov/ # Liskov Substitution Principle
│ ├── mediator/ # Mediator Pattern
│ ├── nullobject/ # Null Object Pattern
│ ├── observer/ # Observer Pattern
│ ├── prototype/ # Prototype Pattern
│ ├── proxy/ # Proxy Pattern
│ ├── state/ # State Pattern
│ ├── strategy/ # Strategy Pattern
│ └── visitor/ # Visitor Pattern
├── docs/
│ └── patterns/ # Pattern-specific documentation
├── .gitignore
└── README.md
- Java Development Kit (JDK) 8 or higher
- IntelliJ IDEA, Eclipse, or any Java IDE (optional but recommended)
-
Clone the repository:
git clone https://github.com/yourusername/design-patterns-java.git cd design-patterns-java -
Open in IntelliJ IDEA:
File→Open→ Select the project folder- IntelliJ will automatically detect the source root
- Ready to run! Just right-click any
Main.javaand select "Run"
- Open the project in IntelliJ IDEA
- Navigate to any pattern:
src → com → designpatterns → <pattern> → <example> → Main.java - Right-click on
Main.java→ Select "Run 'Main.main()'" - See output in the console!
Example:
src/com/designpatterns/strategy/paymentprocessingsystem/Main.java
→ Right-click → Run ✅
That's it! No configuration needed!
javac -cp src src/com/designpatterns/<pattern>/<example>/*.javajava -cp src com.designpatterns.<pattern>.<example>.Main# IntelliJ: Right-click src/com/designpatterns/strategy/paymentprocessingsystem/Main.java → Run
# Command Line:
javac -cp src src/com/designpatterns/strategy/paymentprocessingsystem/*.java
java -cp src com.designpatterns.strategy.paymentprocessingsystem.Main# IntelliJ: Right-click src/com/designpatterns/observer/weatherstationnotification/Main.java → Run
# Command Line:
javac -cp src src/com/designpatterns/observer/weatherstationnotification/*.java
java -cp src com.designpatterns.observer.weatherstationnotification.Main# IntelliJ: Right-click src/com/designpatterns/factory/fetchshape/Main.java → Run
# Command Line:
javac -cp src src/com/designpatterns/factory/fetchshape/*.java
java -cp src com.designpatterns.factory.fetchshape.Main# IntelliJ: Right-click src/com/designpatterns/decorator/pizzashopscenario/Main.java → Run
# Command Line:
javac -cp src src/com/designpatterns/decorator/pizzashopscenario/*.java
java -cp src com.designpatterns.decorator.pizzashopscenario.Main| Pattern | Package | Example |
|---|---|---|
| Abstract Factory | abstractfactory.vehiclecatalog |
Vehicle manufacturing |
| Adapter | adapter.weightmachine |
Weight machine adapter |
| Bridge | bridge.breathingdesign |
Animal breathing mechanisms |
| Builder | builder.studentbuilder |
Student object construction |
| Chain of Responsibility | chainofresponsibility.loggingsystem |
Logging system |
| Command | command.smarthomesystem |
Smart home controls |
| Composite | composite.filesystem |
File system hierarchy |
| Decorator | decorator.pizzashopscenario |
Pizza with toppings |
| Facade | facade.orderprocessingsystem |
Order processing |
| Factory | factory.fetchshape |
Shape creation |
| Flyweight | flyweight.texteditor |
Text editor optimization |
| Liskov | liskov.vehiclehierarchydesign |
Vehicle hierarchy |
| Mediator | mediator.chatapplicationsystem |
Chat application |
| Null Object | nullobject.main |
Null object handling |
| Observer | observer.weatherstationnotification |
Weather updates |
| Prototype | prototype.studentmanagement |
Student cloning |
| Proxy | proxy.employeeaccesscontrol |
Employee access control |
| State | state.vendingmachine |
Vending machine states |
| Strategy | strategy.paymentprocessingsystem |
Payment methods |
| Visitor | visitor.ecommercesystem |
E-commerce operations |
Detailed documentation for each pattern is available in the docs/patterns/ directory:
- Each pattern has comprehensive documentation covering:
- Overview - What the pattern does
- Problem & Solution - Why and how to use it
- UML Diagrams - Visual structure
- Code Examples - Practical implementations
- Use Cases - Real-world applications
- Benefits & Drawbacks - Pros and cons
Example: See docs/patterns/Strategy Design Pattern.md for detailed Strategy pattern documentation.
Contributions are welcome! If you'd like to add more patterns or improve existing implementations:
- Fork the repository
- Create your feature branch (
git checkout -b feature/NewPattern) - Follow the existing package structure:
com.designpatterns.<pattern>.<example> - Commit your changes (
git commit -m 'Add NewPattern') - Push to the branch (
git push origin feature/NewPattern) - Open a Pull Request
- Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four)
- Refactoring.Guru - Design Patterns
- SOLID Principles
Praveen Singh