Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Design Patterns in Java

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.

Java Design Patterns

Table of Contents

About

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.

Design Patterns

Creational Patterns (4)

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

Structural Patterns (7)

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

Behavioral Patterns (8)

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

SOLID Principles (1)

  • Liskov Substitution Principle - Subtypes must be substitutable for their base types

Project Structure

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

Getting Started

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • IntelliJ IDEA, Eclipse, or any Java IDE (optional but recommended)

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/design-patterns-java.git
    cd design-patterns-java
  2. Open in IntelliJ IDEA:

    • FileOpen → Select the project folder
    • IntelliJ will automatically detect the source root
    • Ready to run! Just right-click any Main.java and select "Run"

How to Run

✨ Method 1: IntelliJ IDEA (Recommended - One Click!)

  1. Open the project in IntelliJ IDEA
  2. Navigate to any pattern:
    src → com → designpatterns → <pattern> → <example> → Main.java
    
  3. Right-click on Main.java → Select "Run 'Main.main()'"
  4. See output in the console!

Example:

src/com/designpatterns/strategy/paymentprocessingsystem/Main.java
→ Right-click → Run ✅

That's it! No configuration needed!


Method 2: Command Line

Compile a Pattern:

javac -cp src src/com/designpatterns/<pattern>/<example>/*.java

Run a Pattern:

java -cp src com.designpatterns.<pattern>.<example>.Main

Quick Examples:

Strategy Pattern (Payment Processing)

# 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

Observer Pattern (Weather Station)

# 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

Factory Pattern (Shapes)

# 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

Decorator Pattern (Pizza Shop)

# 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

All Patterns Quick Reference:

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

Pattern Documentation

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.

Contributing

Contributions are welcome! If you'd like to add more patterns or improve existing implementations:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/NewPattern)
  3. Follow the existing package structure: com.designpatterns.<pattern>.<example>
  4. Commit your changes (git commit -m 'Add NewPattern')
  5. Push to the branch (git push origin feature/NewPattern)
  6. Open a Pull Request

Learning Resources

Author

Praveen Singh

About

Comprehensive collection of 20+ Gang of Four design patterns implemented in Java with practical examples

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages