25-Comp-B11 Advanced Software Design · December 2018
Question 16 of 28: Observer vs. Mediator Design Patterns
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Notes on this paper
17-Comp-B11 Advanced Software Design — National Exams, December 2018. 3 hours, closed book exam with up to 2 aid sheets allowed (written on both sides), no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any five (5) questions in Part I, any three (3) in Part II, any four (4) in Part III, any two (2) in Part IV, and any five (5) in Part V — only the first questions answered, in each part, as they appear in the answer book are marked. All questions carry equal weight, so the 19 questions actually marked (5+3+4+2+5 of 28) each count for 100/19 ≈ 5.26% of the paper. All 28 questions are answered below for completeness.
Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, design principles, dependability; Pressman, Software Engineering: A Practitioner's Approach (9th ed.) — supplementary process and quality coverage; Gamma, Helm, Johnson & Vlissides (GoF), Design Patterns: Elements of Reusable Object-Oriented Software — creational/structural/behavioural pattern catalogue and the "program to an interface, not an implementation" / "favor object composition over class inheritance" principles; Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, inheritance and language-level object semantics; Bertrand Meyer, Object-Oriented Software Construction — design by contract, preconditions/postconditions/invariants; Barbara Liskov's 1987 substitutability paper for Question 11; Karl Wiegers, Software Requirements (3rd ed.) — the functional/quality/process/implementation/business requirements taxonomy of Question 2; Kruchten, The Rational Unified Process: An Introduction, for Question 1; Myers, The Art of Software Testing, for Questions 6 and 28.
PART I — General Principles (answer any 5 of 7)
Question 16: Observer vs. Mediator Design Patterns (Part III)
Both are BEHAVIOURAL patterns (Question 14) concerned with communication between objects, but they solve different communication SHAPES.
Observer establishes a ONE-TO-MANY, publish/subscribe relationship: a single subject notifies an open-ended, dynamically-registered set of observers whenever its own state changes; the subject knows only that it has observers, never any observer's internal logic, and observers do not talk to each other at all. This is exactly the mechanism behind MVC's Model→View notification (Question 18) and Question 13's worked pattern-language example: a spreadsheet cell (subject) notifies a chart and a summary total (observers), and the chart and the summary total never communicate with one another.
Mediator centralizes MANY-TO-MANY communication among a fixed set of "colleague" objects that would otherwise need direct references to one another. Instead of every colleague knowing about every other colleague (an N² web of dependencies), each colleague talks only to a single Mediator object, which encapsulates and coordinates the interaction logic between them. A dialog box with several interdependent widgets (a checkbox that, when checked, must enable a previously-disabled text field, which in turn must revalidate a submit button) is a classic Mediator use: each widget notifies only the dialog's Mediator of its own change, and the Mediator decides which OTHER widgets must react, rather than each widget holding direct references to every other widget it might need to affect.
Key difference. Observer is a ONE-directional broadcast from a single subject to many independent observers who never interact with each other; Mediator is a hub coordinating potentially MANY colleagues, each of which may need to affect any of the others, replacing what would otherwise be a dense mesh of direct colleague-to-colleague references with one central coordinator. Put differently, Observer removes the SUBJECT's need to know its observers' concrete types; Mediator removes every COLLEAGUE's need to know every other colleague's concrete type, by concentrating the many-to-many interaction logic in one place.