Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Design Patterns Demo

Java Spring Boot License

A comprehensive demonstration of 16 Gang of Four (GoF) Design Patterns implemented in a modern Spring Boot application. This project showcases both traditional pattern implementations and their real-world usage through REST APIs, making it an excellent resource for interviews and learning.

Author: Shivam Srivastav

🏗️ Architecture Overview

Architecture Diagram

✨ Features

  • ✅ 16 Design Patterns - Complete implementation of Creational, Structural, and Behavioral patterns
  • ✅ REST API Layer - Real-world pattern usage via HTTP endpoints
  • ✅ Spring AOP Integration - Advanced Proxy pattern demonstration
  • ✅ Comprehensive Javadocs - Every class includes detailed documentation with "Why this pattern?" explanations
  • ✅ Console Demo - Automatic demonstration on application startup
  • ✅ Production-Ready Code - Following Spring Boot best practices

Implemented Patterns

Creational

  1. Singleton: LoggerService (Spring Bean)
  2. Factory Method: NotificationFactory
  3. Builder: Order (Lombok @Builder)
  4. Abstract Factory: UIFactory (Light/Dark Themes)
  5. Prototype: Shape (Cloneable)

Structural

  1. Adapter: PaymentAdapter
  2. Facade: ShopFacade
  3. Decorator: UrgentMessageDecorator
  4. Proxy: DatabaseProxy (and Spring AOP LoggingAspect)
  5. Composite: Department (Employee Hierarchy)

Behavioral

  1. Strategy: CheckoutService (Payment Strategies)
  2. Observer: EmailNotificationListener (Spring Events)
  3. Template Method: OrderProcessor
  4. Command: RemoteControl (Smart Home)
  5. Chain of Responsibility: SupportHandler
  6. State: OrderContext (Order Lifecycle)

How to Run

./mvnw spring-boot:run

On startup, the DesignPatternDemoRunner will execute and print a demonstration of all patterns to the console.

REST API Endpoints (Interview Enhancements)

You can interact with the application using the following endpoints to see the patterns in action.

1. Create Order (Builder & Observer)

Uses the Builder Pattern to construct the order and the Observer Pattern to notify listeners.

curl -X POST "http://localhost:8080/api/orders?productName=Phone&price=999"

2. Process Payment (Strategy)

Uses the Strategy Pattern to switch between payment methods (PayPal vs Credit Card).

# Pay with PayPal
curl -X POST "http://localhost:8080/api/orders/ORD-123/pay?method=paypal&amount=999"

# Pay with Credit Card
curl -X POST "http://localhost:8080/api/orders/ORD-123/pay?method=card&amount=999"

3. Check Order Status (State)

Uses the State Pattern to simulate order lifecycle transitions.

curl "http://localhost:8080/api/orders/ORD-123/status"

4. Legacy Payment (Adapter)

Uses the Adapter Pattern to process payments via a legacy gateway.

curl -X POST "http://localhost:8080/api/payment/legacy?amount=50"

5. AOP Logging (Proxy)

Check the application logs after making any API call. You will see logs from LoggingAspect, demonstrating the Proxy Pattern via Spring AOP.

[INFO] ... [AOP Proxy] Executed ... in 0 ms

About

🎯 Master 16 GoF Design Patterns with Spring Boot 3 | REST APIs + AOP + Comprehensive Javadocs | Interview-Ready Reference

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages