Initially, we would need separate classes for each pizza-topping combination:
class MargheritaWithCheese(Pizza):
def get_cost(self): return 6.0
class MargheritaWithOlives(Pizza):
def get_cost(self): return 5.5The Decorator pattern allows dynamic addition of toppings:
pizza = MargheritaPizza()
pizza = CheeseTopping(pizza)
pizza = OlivesTopping(pizza)- Flexible composition of toppings
- Easy to add new toppings
- Maintains Single Responsibility Principle
Multiple inventory instances could lead to inconsistent state:
inventory1 = InventoryManager()
inventory2 = InventoryManager()class InventoryManager:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance- Ensures single source of truth for inventory
- Prevents resource conflicts
- Centralizes inventory management
Direct instantiation with complex logic:
if type == "margherita":
pizza = MargheritaPizza()
elif type == "pepperoni":
pizza = PepperoniPizza()pizza = PizzaFactory.create_pizza(choice)- Encapsulates creation logic
- Easy to add new pizza types
- Centralized creation point
Direct coupling between components:
class Order:
def process(self):
notify_kitchen()
notify_customer()order_subject = Subject()
order_subject.attach(kitchen_display)
order_subject.attach(order_tracker)
order_subject.notify(message)- Loose coupling
- Easy to add new observers
- Flexible notification system
class OverengineeredPizzaSystem:
def __init__(self):
self.observers = {}
self.notification_queue = []
self.event_handlers = {}
self.pizza_cache = {}
self.topping_validators = {}
self.price_calculators = {}
self.ingredient_proxies = {}
self.order_state_machine = {}
self.payment_middleware = []
def process_order(self, order):
# Unnecessary complexity
self.validate_order_state()
self.check_ingredient_availability()
self.calculate_optimal_preparation_sequence()
self.notify_all_subsystems()
self.update_various_caches()
# More unnecessary steps...This is overengineered because:
- Too many abstraction layers
- Unnecessary caching
- Complex state management
- Over-modularization
- Excessive use of design patterns