Design Pattern Examples
Overview of object-oriented design patterns
Observer_Exercise.c File Reference

Implementation of the Observer_Exercise() function as used in the Observer Pattern. More...

#include <stdio.h>
#include <stdlib.h>
#include "helpers/uint32_to_binary.h"
#include "Observer_NumberProducer.h"
#include "Observer_Exercise.h"
Include dependency graph for Observer_Exercise.c:

Go to the source code of this file.

Functions

static void ObserverForDecimal_NumberChanged (uint32_t number)
 Represents an observer that prints out the specified number from the Subject in decimal.
 
static void ObserverForHexadecimal_NumberChanged (uint32_t number)
 Represents an observer that prints out the specified number from the Subject in hexadecimal.
 
static void ObserverForBinary_NumberChanged (uint32_t number)
 Represents an observer that prints out the specified number from the Subject in binary.
 
void Observer_Exercise (void)
 Example of using the Observer Pattern.
 

Detailed Description

Implementation of the Observer_Exercise() function as used in the Observer Pattern.

Definition in file Observer_Exercise.c.

Function Documentation

◆ Observer_Exercise()

void Observer_Exercise ( void  )

Example of using the Observer Pattern.

The Observer pattern allows for one or more observers to react to changes in a Subject entity.

In this exercise, a number producer (the Subject) updates an internal value every time the NumberProducer_UpdateNumber() function is called. Three different observers are attached to the number producer and print out the current value in different formats whenever the number is changed.

Since functions are used as the observers of the NumberProducer object, the "push" model is used to push the updated number to each observer. Otherwise, the observers would have to be given the NumberProducer object so as to call a function to fetch the number from that object.

Definition at line 74 of file Observer_Exercise.c.

References NumberProducer_Create(), NumberProducer_Destroy(), NumberProducer_SubscribeToNumberChanged(), NumberProducer_UnsubscribeFromNumberChanged(), NumberProducer_UpdateNumber(), ObserverForBinary_NumberChanged(), ObserverForDecimal_NumberChanged(), and ObserverForHexadecimal_NumberChanged().

◆ ObserverForBinary_NumberChanged()

static void ObserverForBinary_NumberChanged ( uint32_t  number)
static

Represents an observer that prints out the specified number from the Subject in binary.

Parameters
numberThe number that was changed.

Definition at line 46 of file Observer_Exercise.c.

References uint32_to_binary().

Referenced by Observer_Exercise().

◆ ObserverForDecimal_NumberChanged()

static void ObserverForDecimal_NumberChanged ( uint32_t  number)
static

Represents an observer that prints out the specified number from the Subject in decimal.

Parameters
numberThe number that was changed.

Definition at line 26 of file Observer_Exercise.c.

Referenced by Observer_Exercise().

◆ ObserverForHexadecimal_NumberChanged()

static void ObserverForHexadecimal_NumberChanged ( uint32_t  number)
static

Represents an observer that prints out the specified number from the Subject in hexadecimal.

Parameters
numberThe number that was changed.

Definition at line 36 of file Observer_Exercise.c.

Referenced by Observer_Exercise().