Skip to content

A clean, real-world implementation of the Decorator Design Pattern using Spring Boot. This project simulates customizable product addons (like pizza toppings, coffee add-ons, etc.) where each addon wraps and enhances the base product dynamically at runtime.

Notifications You must be signed in to change notification settings

pratham1singh/springboot-decorator-pattern-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

☕ Decorator Pattern - Coffee System (Spring Boot)

This project demonstrates the Decorator Design Pattern using a simple coffee system implemented in Spring Boot. It allows dynamic addition of addons like Milk, Sugar, Cream, and Choco to a base coffee.


🔧 Features

  • Implements Decorator pattern in a clean and understandable way.
  • Exposes two REST endpoints:
    • GET /coffee - View available product and addons.
    • POST /coffee - Send desired addons and receive the final decorated coffee with price and description.

📦 API Endpoints

GET /coffee

Returns:

{
  "productName": "Coffee",
  "addons": [
    "MILK",
    "CHOCO",
    "CREAM",
    "SUGAR"
  ]
}

POST /coffee

Body

["MILK", "SUGAR", "CREAM", "SUGAR"]

Returns:

{
  "totalPrice": 200,
  "desc": "It is a coffee! It contains SUGAR, CREAM, MILK.",
  "decorators": [
    {
      "price": 10,
      "count": 2,
      "desc": "I am sugar decorated coffee",
      "type": "SUGAR"
    },
    {
      "price": 30,
      "count": 1,
      "desc": "I am cream decorated coffee",
      "type": "CREAM"
    },
    {
      "price": 50,
      "count": 1,
      "desc": "I am milk decorated coffee",
      "type": "MILK"
    }
  ]
}

🚀 Getting Started

1. Clone the repository

git clone git@github.com:pratham1singh/springboot-decorator-pattern-demo.git
cd springboot-decorator-pattern-demo

2.Run the Spring Boot Application

Make sure you have Java and Maven installed.

./mvnw spring-boot:run

📚 What You’ll Learn

  • How to use the Decorator Design Pattern effectively.

  • Dynamic object construction using decorators.

  • Spring Boot setup for a RESTful API.

🧠 Inspiration

This was created to explain how you can structure real-world solutions (like adding toppings to coffee) using decorators without modifying the base class.

About

A clean, real-world implementation of the Decorator Design Pattern using Spring Boot. This project simulates customizable product addons (like pizza toppings, coffee add-ons, etc.) where each addon wraps and enhances the base product dynamically at runtime.

Topics

Resources

Stars

Watchers

Forks

Languages