Facade: offer one clear entry point
Picture
Preparing a lesson session may require loading a lesson, finding a saved note, and assembling a display.
Predict
Should the facade become the implementation of every subsystem?
Walk through the small example
No, it should delegate. A simpler entry point should not erase useful boundaries.
Mental model and complete Python solution
LessonFacade combines a course lookup and notebook lookup into one view value. It translates a missing lesson into an explicit error rather than a partly valid response. Unlike an adapter, its main purpose is simplifying a multi-step workflow, not reconciling incompatible interfaces. Unlike a mediator, it primarily serves an external caller rather than coordinating peer-to-peer interactions.
What the function promises: open_lesson returns an independent dictionary containing title and the optional saved note. Missing lessons raise KeyError. No external storage is accessed.
class Course:
def __init__(self, titles):
self.titles = dict(titles)
def title(self, lesson_id):
return self.titles[lesson_id]
class Notebook:
def __init__(self, notes):
self.notes = dict(notes)
def note(self, lesson_id):
return self.notes.get(lesson_id, "")
class LessonFacade:
def __init__(self, course, notebook):
self.course, self.notebook = course, notebook
def open_lesson(self, lesson_id):
return {"title": self.course.title(lesson_id),
"note": self.notebook.note(lesson_id)}
view = LessonFacade(Course({"bfs": "BFS"}), Notebook({})).open_lesson("bfs")
assert view == {"title": "BFS", "note": ""}
Work and storage: Expected O(1) local lookups plus returned text storage; remote collaborators would add their own latency.
When to recognize it: The facade coordinates. Loading, storage, and formatting still belong to focused helpers with their own contracts.
Check before applying it: A facade simplifies calls; it does not automatically provide transactions across collaborators.
Change one rule
Both helpers become remote services. Does the facade make the pair atomic?
See what changes
No. It only coordinates calls. Timeouts, partial responses, and cross-service consistency need an explicit distributed workflow.
Connect
Adapter: translate a boundary: An adapter translates one boundary; a facade simplifies a whole workflow.
Mediator: centralize collaboration: Compare an external entry point with coordination among peers.