Skip to content

Hu-Weiming/java-design-patterns-practice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Java Design Patterns Practice

A collection of design pattern implementations in Java for learning and reference.

Patterns Implemented

Creational Patterns

  • Singleton - Ensures a class has only one instance

    • DatabaseConnection: Double-checked locking implementation
    • AppConfig: Enum-based singleton (recommended approach)
  • Factory Method - Creates objects without specifying exact class

    • NotificationFactory: Creates Email, SMS, or Push notifications
  • Abstract Factory - Creates families of related objects

    • UIFactory: Creates themed UI components (Dark/Light themes)
  • Builder - Constructs complex objects step by step

    • HttpRequest.Builder: Fluent API for building HTTP requests

Behavioral Patterns

  • Observer - Notifies multiple objects about state changes

    • EventManager: Pub/sub event system with logging and alerting
  • Strategy - Defines a family of interchangeable algorithms

    • Sorter: Swappable sorting strategies (Bubble Sort, Quick Sort)
  • Template Method - Defines algorithm skeleton in base class

    • DataParser: CSV and JSON parsing with shared validation logic
  • Chain of Responsibility - Passes requests along a handler chain

    • RequestHandler: Authentication, authorization, and logging chain
  • Command - Encapsulates requests as objects

    • CommandHistory: Text editor with undo support

Structural Patterns

  • Decorator - Adds behavior to objects dynamically

    • DataSourceDecorator: Encryption and compression wrappers
  • Proxy - Controls access to another object

    • ImageProxy: Lazy-loading image proxy
  • Adapter - Makes incompatible interfaces work together

  • State - Allows object to change behavior when internal state changes

    • Order: Order lifecycle transitions (New -> Paid -> Shipped -> Delivered)
    • VlcPlayerAdapter: Adapts VLC player to MediaPlayer interface

Project Structure

src/
├── main/java/com/huweiming/patterns/
│   ├── singleton/
│   ├── factory/
│   ├── abstractfactory/
│   ├── builder/
│   ├── observer/
│   ├── strategy/
│   ├── templatemethod/
│   ├── chain/
│   ├── command/
│   ├── decorator/
│   ├── proxy/
│   └── adapter/
└── test/java/com/huweiming/patterns/
    └── (mirrors main structure)

Getting Started

# Compile
mvn compile

# Run tests
mvn test

# Package
mvn package

Requirements

  • Java 17+
  • Maven 3.8+

About

Design patterns implemented in Java with practical examples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages