2IRR00 · Topic 11
Architecture: MVC & Observer
Model-View-Controller and the Observer pattern (push vs pull)
Model-View-Controller
MVC is an architectural pattern for GUI software that separates the Model (data + business logic), the View (presentation/IO) and the Controller (links view and model) — it says WHAT to separate, not HOW.
Model: internal representation of data; manages data, logic, rules, and requests on state.
View: a user interface that displays the model and accepts input. There can be several views of the same model.
Controller: links model and view; translates inputs into commands for the model/view.
MVC makes presentation independent of data, allows composing/swapping views (even at runtime), and separates concerns.
MVC can incorporate other patterns: Command (pass info), Decorator (add to a view), Facade (interfaces), Factory (create controllers), Observer (update views on model changes), Strategy (choose behaviour).
Common mistakes
- Saying MVC 'must' use the factory pattern (false) or that the MODEL manages view↔controller interactions (false — it's the controller that links them).
Exam tips
- Correct MVC MCQ: it separates model/view/controller but not HOW ✔; it can use the decorator to modify views ✔. Wrong: must use factory; model manages view↔controller.
Model-View-Controller practice
1 questions
Observer (push vs pull)
Observer defines a one-to-many dependency: when the subject changes, all registered observers are notified and updated automatically, with loose coupling.
Subject: addObserver / removeObserver / notifyObservers + holds a list of observers. Observer: update().
Push method: the subject SENDS the (necessary) data in update(). Subjects must know what observers need → more coupling, often too much data sent.
Pull method: the subject only NOTIFIES that something changed; each observer PULLS the data it needs via the subject's getters. Less coupling, more independent/reusable, but can be less efficient (extra calls; observers must figure out what changed).
In both cases observers need some knowledge of the subject; coupling is reduced if they know it via an abstract class/interface.
Used in MVC/GUIs to update views, distributed event handling, notifications.
public void notifyObservers() {
for (Observer o : observers) o.update(this); // push (sends data)
}
// Pull alternative: o.update(); then observer calls subject.getState()Common mistakes
- Describing pull as 'subject sends required data' — that's push. Pull = notify only, observer fetches.
- Forgetting to trigger notifyObservers() after a state change.
Exam tips
- Pull (3-point open Q): subject only notifies; each observer decides what data to retrieve via getters → lower coupling, possibly less efficient.
- Push increases coupling and often sends too much data.
Observer (push vs pull) practice
1 questions