Design Pattern Examples
Overview of object-oriented design patterns
Observer_Exercise.c
Go to the documentation of this file.
1
5
6#include <stdio.h>
7#include <stdlib.h>
8
9#include "helpers/uint32_to_binary.h"
10
12#include "Observer_Exercise.h"
13
14
15//=============================================================================
16//=============================================================================
17
18//=============================================================================
19//=============================================================================
20
26static void ObserverForDecimal_NumberChanged(uint32_t number)
27{
28 printf(" Decimal : %u\n", number);
29}
30
36static void ObserverForHexadecimal_NumberChanged(uint32_t number)
37{
38 printf(" Hexadecimal: 0X%08X\n", number);
39}
40
46static void ObserverForBinary_NumberChanged(uint32_t number)
47{
48 char buffer[(sizeof(uint32_t) * 8) + 1];
49 uint32_to_binary(number, buffer, sizeof(buffer));
50 printf(" Binary : %s\n", buffer);
51}
52
53
54//=============================================================================
55//=============================================================================
56
73// ! [Using Observer in C]
75{
76 printf("\nObserver_Exercise\n");
77
79
80 if (producer != NULL)
81 {
82 // Tell the number producer about the observers who are notified
83 // whenever the value changes.
85 if (subscribed)
86 {
88 }
89 if (subscribed)
90 {
92 }
93
94 // If everyone subscribed, trigger the observers
95 if (subscribed)
96 {
97 // Call the number producer's Update() method a number of times.
98 // The observers automatically print out the current value in
99 // different bases.
100 for (int index = 0; index < 10; ++index)
101 {
102 printf(" Update %d on number producer. Results from observers:\n", index);
104 }
105 }
106
107 // When done, remove the observers from the number producer.
108 // It's always good to clean up after ourselves.
112
113 NumberProducer_Destroy(producer);
114 producer = NULL;
115 }
116
117 printf(" Done.\n");
118}
119// ! [Using Observer in C]
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.
bool NumberProducer_SubscribeToNumberChanged(NumberProducer *producer, NumberChangedFunction observer)
Subscribe to the given NumberProducer to received changes to that producer's number....
void NumberProducer_UpdateNumber(NumberProducer *producer)
Update the number in the given NumberProducer object, triggering a call to all observer in the produc...
void NumberProducer_UnsubscribeFromNumberChanged(NumberProducer *producer, NumberChangedFunction observer)
Unsubscribe from the Given NumberProducer so the given observer will no longer be called when the pro...
void NumberProducer_Destroy(NumberProducer *producer)
Destroy the given instance of a NumberProducer object. After this function returns,...
NumberProducer * NumberProducer_Create(uint32_t number)
Create an instance of the NumberProducer structure and initialize the structure with the specified nu...
Declaration of the NumberProducer structure along with its support functions, NumberProducer_Create()...
Declaration of the Observer_Exercise() function as used in the Observer Pattern.
Represents the Subject in this example. In this case, a structure that contains a list of observers a...
void uint32_to_binary(uint32_t number, char *buffer, size_t bufferSize)
Function to convert a 32-bit unsigned integer into a string representation containing all 32 bits.