This Spring Boot application demonstrates 9 essential design patterns using a pizzeria theme. Each pattern is implemented in its own package and i tried to do examples in almost all (i promise i will update). I do it with pizza because it's one of my favorites foods in the world :D
- What: Ensures only one instance of a class exists
- Example: PizzaOven - only one oven should exist in the pizzeria
- What: Creates objects without specifying exact classes
- Example: PizzaFactory creates different types of pizzas
- Pizza Types: margherita, pepperoni, veggie
- What: Creates families of related objects
- Example: Different ingredient factories for Italian vs American style
- Styles: italian, american
- What: Constructs complex objects step by step
- Example: Building a complex pizza order with multiple options
- What: Adds features to objects dynamically
- Example: Adding toppings to pizzas without changing the base class
- What: Notifies multiple objects about state changes
- Example: Order tracking system notifying customers, kitchen, delivery
- What: Defines interchangeable algorithms
- Example: Different cooking methods (oven, wood-fired, convection)
- Methods: oven, woodfired, convection
- What: Provides simplified interface to complex subsystem
- Example: Simple pizza ordering process hiding internal complexity
- What: Allows incompatible interfaces to work together
- Example: Integrating third-party payment system with our interface
src/main/java/dev/anfeespi/designpatterns/
├── DesignPatternsApplication.java # Main class
├── singleton/
│ └── PizzaOven.java # Singleton pattern
├── factory/
│ ├── pizza/
│ │ ├── Pizza.java
│ │ ├── MargheritaPizza.java
│ │ ├── PepperoniPizza.java
│ │ └── VeggiePizza.java
│ └── PizzaFactory.java # Factory
├── abstract_factory/
│ ├── base_products/
│ │ ├── Dough.java
│ │ ├── Sauce.java
│ │ └── Cheese.java
│ ├── products/ # Concrete products
│ ├── IngredientFactory.java # Abstract factory
│ ├── ItalianIngredientFactory.java
│ └── AmericanIngredientFactory.java
├── builder/
│ └── PizzaOrder.java # Builder pattern
├── decorator/
│ ├── PizzaComponent.java # Component interface
│ ├── BasicPizza.java # Concrete component
│ ├── ToppingDecorator.java # Base decorator
│ ├── CheeseDecorator.java
│ ├── MushroomDecorator.java
│ └── PepperoniDecorator.java # Concrete decorators
├── observer/
│ ├── OrderObserver.java # Observer interface
│ ├── OrderTracker.java # Subject
│ ├── DeliveryTracker.java
│ ├── KitchenDisplay.java
│ └── CustomerNotifier.java # Concrete observers
├── strategy/
│ ├── CookingStrategy.java # Strategy interface
│ ├── *Cooking.java # Concrete strategies
│ └── PizzaChef.java # Context class
├── facade/
│ └── PizzaOrderingFacade.java # Facade pattern
└── adapter/
├── PaymentProcessor.java # Target interface
├── ThirdParty.java # The third party or class unmodifiable
├── PaymentAdapter.java #Adapter
└── PizzaPaymentService.java # Example (more or less)
src/main/resources/
└── application.properties # Configuration
- Thread-safe implementation using synchronized keyword
- Private constructor prevents external instantiation
- Shared state across all references
- Centralizes object creation logic
- Easy to extend with new pizza types
- Follows Open/Closed Principle
- Creates families of related objects
- Easy to switch between different product families
- Ensures compatibility between related products
- Handles complex object construction
- Fluent interface for readability
- Validation in build() method
- Dynamic feature addition without inheritance
- Follows Single Responsibility Principle
- Flexible combination of features
- Loose coupling between subject and observers
- Dynamic subscription/unsubscription
- Broadcast communication pattern
- Runtime algorithm selection
- Easy to add new strategies
- Eliminates conditional statements
- Simplifies complex subsystem interactions
- Provides unified interface
- Hides implementation details
- Integrates incompatible interfaces
- Reuses existing functionality
- Bridge between different systems
🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕