Skip to content

Latest commit

 

History

History

observer

Observer

Observer Pattern allows observers to be notified on changes in an observable object.

Code

CourseWebsite (an Obserable object) allows Students to register as an Observer. When CourseWebsite has a new state update, e.g. PutAnnoucement() is called, it notifies all registered Observer by calling its Update().

Example

CourseWebsite course_website;

Student olivia("Olivia");
Student liam("Liam");
course_website.AddObserver(olivia);
course_website.AddObserver(liam);

course_website.PutAnnoucement("New assignment is released!");

Output:

This is Olivia. I just received an update from the course website saying: New
assignment is released!
This is Liam. I just received an update from the course website saying: New
assignment is released!

Reference