Problem

Large design doesn't scale well as new graphics and monitoring requirements are added.

Solution

Establish a one-to-many dependency between objects so that when a state changes, all dependents are notified and updated automatically. A "Model" object maintains data and business logic. It can be connected to an external storage device or other implementation of data storage. Observers register themselves with the Model (Subject) when they are created. Any changes to the Subject are broadcast to all Observers.

Related Patterns

Discussion

Essentially, we are separating the data from its representation. This allows the view to be pluggable and dynamic: the number and type of view objects can be configured on the fly.

Examples

One of the most common examples of the Observer pattern is the View part of Model-View-Controller systems. One central Model is designated the Subject for all Observer objects, which query for relevant data when they receive an update() broadcast.

A real-world example would be an auction: The auctioneer is the Subject and all bidders are Observers. When the auctioneer accepts a bid, all bidders are notified vocally.

Code

Here, two Observers are created on a Subject subj in C++. When the value of subj changes to 20, obs1 and obs2 are notified, which calls their update() method.

class Observer{
  int value;
public:
  Observer(Subject *model, int value){
    model->attach(this);
    this.value = value;
  }
  void update(int value){
    cout << value << " mod "<< this.value << " = " << value % this.value << endl;
  }
}
class Subject{
  int value;
  vector views;
public:
  notify(){
    for(int i = 0; i < views.size(); i++) views[i]->update(value);
  }
  setVal(int value){
    this.value = value;
  }
  attach(Observer *obs){
    views.push(obs);
  }
}
int main(){
  Subject subj;
  Observer obs1(&subj, 3);
  Observer obs2($subj, 4);
  subj.setVal(20);
}
// output:
20 mod 3 = 2
20 mod 4 = 0