πŸ”­Observer

The Observer pattern provides a way to subscribe and unsubscribe to and from these events for any object that implements a subscriber interface.

circle-info

Observer is a behavioral design pattern that allows some objects to notify other objects about changes in their state.

Usage examples

The Observer pattern is pretty common in C++ code, especially in the GUI components. It provides a way to react to events happening in other objects without coupling to their classes.

Identification

The pattern can be recognized by subscription methods, that store objects in a list, and by calls to the update method issued to objects in that list.

Pros & Cons

circle-check
circle-check
triangle-exclamation

Graphic Visual

Flow of Observer
Settimana Enigmistica Style

Example

The following example shows the structure of the Observer design pattern, focusing on answering these questions:

  • What classes does it consist of?

  • What roles do these classes play?

  • In what way elements of the pattern are related?

Conceptual example

main.cpp

Last updated