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"
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. | |
Implementation of the Observer_Exercise() function as used in the Observer Pattern.
Definition in file Observer_Exercise.c.
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().
|
static |
Represents an observer that prints out the specified number from the Subject in binary.
number | The number that was changed. |
Definition at line 46 of file Observer_Exercise.c.
References uint32_to_binary().
Referenced by Observer_Exercise().
|
static |
Represents an observer that prints out the specified number from the Subject in decimal.
number | The number that was changed. |
Definition at line 26 of file Observer_Exercise.c.
Referenced by Observer_Exercise().
|
static |
Represents an observer that prints out the specified number from the Subject in hexadecimal.
number | The number that was changed. |
Definition at line 36 of file Observer_Exercise.c.
Referenced by Observer_Exercise().