← Back to list

Design Patterns in a Coffee Shop: Mediator, Memento, and Observer

More Staff, More Problems

Minh Le Duc in Python in Plain English · 2025-07-22 00:11 · 8 claps · 3.5 min read paywalled
#python #design-patterns #mediator-pattern #memento-pattern #observer-pattern
Open on Medium ↗
Wiki topics: CUL · Culture & Media 🍳 · Food & Cooking 😂 · Humor & Satire

Design Patterns in a Coffee Shop: Mediator, Memento, and Observer

More Staff, More Problems

Running a coffee shop is no longer a one-person job. You’ve got:

  • Baristas calling out for clean mugs
  • A customer loyalty system tracking every visit
  • An order-taking system with an “Undo” button

You’re not just managing coffee — you’re managing communication, memory, and updates.

Let’s dive into three design patterns that help: Mediator, Memento, and Observer.

You can read my previous post about Chain of Responsibility, Command, and Iterator:

[embed]Design Patterns in a Coffee Shop: Chain of Responsibility, Command, and Iterator When Your Café Gets More Organizedmedium.com

1. Mediator Pattern — Centralized Communication

The Analogy: Shift Supervisor

When the cashier needs help, they don’t shout to every staff member. They talk to the shift supervisor, who knows exactly who to assign the task to. This reduces chaos.

Image from [1]

Image from [1]

That’s the Mediator Pattern — it centralizes communication between components, so they don’t talk directly to each other.

In Code

class Mediator:
    def notify(self, sender, event): pass

class ShiftSupervisor(Mediator):
    def __init__(self, barista, cleaner):
        self.barista = barista
        self.cleaner = cleaner
        self.barista.set_mediator(self)
        self.cleaner.set_mediator(self)
    def notify(self, sender, event):
        if event == "NeedCleanCups":
            print("Supervisor: Notifying cleaner...")
            self.cleaner.clean_cups()
        elif event == "NewOrder":
            print("Supervisor: Asking barista to prepare drink...")
            self.barista.prepare_drink()

class Barista:
    def set_mediator(self, mediator):
        self.mediator = mediator
    def new_order(self):
        self.mediator.notify(self, "NewOrder")
    def prepare_drink(self):
        print("Barista: Preparing the drink")

class Cleaner:
    def set_mediator(self, mediator):
        self.mediator = mediator
    def clean_cups(self):
        print("Cleaner: Cleaning the cups")

Usage

barista = Barista()
cleaner = Cleaner()
supervisor = ShiftSupervisor(barista, cleaner)

barista.new_order()  # Supervisor manages coordination
supervisor.notify(barista, "NeedCleanCups")

Memento Pattern — Save and Restore State

The Analogy: Undo Last Customization

A customer makes a super complicated order — then changes their mind. You hit “undo” and go back to their previous drink configuration.

Image from [2]

Image from [2]

This is the Memento Pattern — it allows an object to save its state so it can be restored later, without exposing internal details.

In Code

class Drink:
    def __init__(self, name):
        self.name = name
        self.history = []

    def add_topping(self, topping):
        self.history.append(self.save_state())
        self.name += f" + {topping}"

    def undo(self):
        if self.history:
            memento = self.history.pop()
            self.restore_state(memento)

    def save_state(self):
        return DrinkMemento(self.name)

    def restore_state(self, memento):
        self.name = memento.get_state()

class DrinkMemento:
    def __init__(self, state):
        self._state = state
    def get_state(self):
        return self._state

Usage

drink = Drink("Latte")
drink.add_topping("Whipped Cream")
drink.add_topping("Caramel")
print(drink.name)  # Latte + Whipped Cream + Caramel

drink.undo()
print(drink.name)  # Latte + Whipped Cream

3. Observer Pattern — Notify Everyone Who’s Watching

The Analogy: Loyalty System Gets Updates

Every time a new order is placed, your loyalty system, inventory tracker, and order printer need to be updated.

Image from [3]

Image from [3]

You don’t want the barista to tell each one manually.

The Observer Pattern solves this. One subject (the order system) notifies all registered observers whenever something changes.

In Code

class OrderSystem:
    def __init__(self):
        self.subscribers = []

    def subscribe(self, observer):
        self.subscribers.append(observer)

    def new_order(self, order):
        print(f"OrderSystem: Received new order for {order}")
        for sub in self.subscribers:
            sub.update(order)

class LoyaltyProgram:
    def update(self, order):
        print(f"LoyaltyProgram: Adding points for {order}")

class InventorySystem:
    def update(self, order):
        print(f"InventorySystem: Deducting items for {order}")

Usage

order_sys = OrderSystem()
order_sys.subscribe(LoyaltyProgram())
order_sys.subscribe(InventorySystem())

order_sys.new_order("Matcha Latte")

Thank you for reading this article; I hope it added something to your knowledge bank! Just before you leave:

👉 Be sure to press the like button and follow me. It would be a great motivation for me.

👉 Follow me: ***LinkedIn | [GitHub](https://github.com/MinLee0210)***

Final Recap

These three patterns are all about structure and communication:

  • Mediator makes components collaborate via a central hub.
  • Memento lets you safely save and restore object state.
  • Observer ensures that multiple components can react to changes in real time.

They’re ideal when you’re managing workflows, state, or notifications in a growing application — or a growing café.

You may also like:

[embed]Design Patterns in a Coffee Shop: Adapter, Bridge and Composite A Coffee Shop with Big Ambitionspython.plainenglish.io

[embed]Design Patterns in a Coffee Shop: Decorator and Facade Brewing Code Elegance with Everyday Examplespython.plainenglish.io

[embed]Design Patterns in a Coffee Shop: Flyweight and Proxy Scaling Your Café the Smart Waymedium.com

Reference

  1. Refactoring Guru — Mediator— URL: https://refactoring.guru/design-patterns/mediator
  2. Refactoring Guru — Memento — URL: https://refactoring.guru/design-patterns/memento
  3. Refactoring Guru — Observer — URL: https://refactoring.guru/design-patterns/observer

Thank you for being a part of the community

Before you go:


메타데이터
post_id
81e3e5f8a883
slug
design-patterns-in-a-coffee-shop-mediator-memento-and-observer-81e3e5f8a883
url
https://python.plainenglish.io/design-patterns-in-a-coffee-shop-mediator-memento-and-observer-81e3e5f8a883
canonical_url
https://python.plainenglish.io/design-patterns-in-a-coffee-shop-mediator-memento-and-observer-81e3e5f8a883
author_url
https://medium.com/@minhle_0210
status
ok
fetched_at
2026-07-25 22:50:20