Design Patterns — A quick guide to Observer pattern.

Andreas Poyias
Jan 7, 2019 · 7 min read
Image for post
Image for post
Image for post
Image for post
#include <iostream>
#include <vector>
using namespace std;
class Subject {
vector < class Observer * > observers;
bool scored; // trigger, event
public:
// register observers
void attach(Observer *obs) {
observers.push_back(obs);
}

// This is the EVENT
// set the if scored and notify ALL observers

void setScored(bool Score) {
scored = Score;
notify();
}
bool getScored() {
return scored;
}
// notify implementaion is further down
// so that the script compiles and runs

void notify();
};
class Observer 
{
Subject *subj;
int excitementLevel; // state
public:
Observer(Subject *mod, int excLevel)
{
subj = mod;
excitementLevel = excLevel;
// Observers register/attach themselves with the Subject
subj->attach(this);
}
virtual void update() = 0; protected:
Subject *getSubject() {
return subj;
}
void setExcitementLevel(int excLevel) {
excitementLevel = excLevel;
}
int getExcitementLevel() {
return excitementLevel;
}
};
void Subject::notify() {
for (int i = 0; i < observers.size(); i++)
observers[i]->update();
}
class Old_ConcreteObserver: public Observer 
{
public:
// Calls parent constructor to register with subject
Old_ConcreteObserver(Subject *mod, int div)
:Observer(mod, div){}
// For older people, if the excitement level
// is over 150 they run risk of heart attack
void update()
{
bool scored = getSubject()->getScored();
setExcitementLevel(getExcitementLevel() + 1);
if (scored && getExcitementLevel() > 150)
{
cout << "Old Observer's team scored!!"
<< " His excitement level is "
<< getExcitementLevel()
<< " watch out of heart attacks!" << endl;
}else{
cout << "Team didn't score. Yeeeih nothing to worry about"
<< endl;
}
} // end update()
};
class Young_ConcreteObserver: public Observer
{
public:
// Calls parent constructor to register with subject
Young_ConcreteObserver(Subject *mod, int div)
: Observer(mod, div){}
// For older people, if the excitement level
// is over 100 they run risk of heart attack
void update()
{
bool scored = getSubject()->getScored();
setExcitementLevel(getExcitementLevel() + 1);
if (scored && getExcitementLevel() > 100)
{
cout << "Young Observer's team scored!!"
<< " His excitement level is "
<< getExcitementLevel()
<< " dont't drink and drive!!" << endl;
}else{
cout << "Team didn't score. Yeeh nothing to worry about"
<< endl;
}
} // end update()
};
int main() {
Subject subj;
Young_ConcreteObserver youngObs1(&subj, 100);
Old_ConcreteObserver oldObs1(&subj, 150);
Young_ConcreteObserver youngObs2(&subj, 52);
subj.setScored(true);
}
// Output
// Young Observer's team scored!! His excitement level is 101
// dont't drink and drive!!
// Old Observer's team scored!! His excitement level is 151 watch
// out of heart attacks! Team didn't score.
// Yeeh nothing to worry about

Data Driven Investor

from confusion to clarity not insanity

By Data Driven Investor

In each issue we cover all things awesome in the markets, economy, crypto, tech, and more! Take a look

Create a free Medium account to get DDI Highlights in your inbox.

Andreas Poyias

Written by

C++ software developer. PhD in the development of highly compressed data structures. I am a proud nerd that enjoys programming!

Data Driven Investor

from confusion to clarity not insanity

Andreas Poyias

Written by

C++ software developer. PhD in the development of highly compressed data structures. I am a proud nerd that enjoys programming!

Data Driven Investor

from confusion to clarity not insanity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store