Design Pattern Examples
Overview of object-oriented design patterns
Observer_NumberProducer.c
Go to the documentation of this file.
1
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <memory.h>
12
14
15//=============================================================================
16//=============================================================================
17
23{
24 if (producer != NULL)
25 {
26 uint32_t localNumber = producer->number;
27 NumberChangedFunctionList localObserverList;
28 // Copy the list so observers can change the original observers
29 // during the notification (this isn't strictly needed in this
30 // example but it is good practice for any notification system
31 // that handles multiple observers where multiple threads might
32 // be in play or observers can unsubscribe at any time, even in
33 // the event notification).
34 if (NumberChangedFunctionList_Copy(&producer->observerList, &localObserverList))
35 {
36 for (size_t index = 0; index < localObserverList.functions_count; index++)
37 {
38 NumberChangedFunction observer = localObserverList.functions[index];
39 observer(localNumber);
40 }
41
42 NumberChangedFunctionList_Clear(&localObserverList);
43 }
44 }
45}
46
47//=============================================================================
48//=============================================================================
49
50
52// NumberProducer_Create()
55{
56 NumberProducer* producer = calloc(1, sizeof(NumberProducer));
57
58 if (producer != NULL)
59 {
60 producer->number = number;
61 }
62 else
63 {
64 printf(" Error! Out of memory allocating space for the NumberProducer!\n");
65 }
66
67 return producer;
68}
69
71// NumberProducer_Destroy()
74{
75 if (producer != NULL)
76 {
78 free(producer);
79 }
80}
81
83// NumberProducer_SubscribeToNumberChanged()
86{
87 bool subscribed = false;
88
89 if (producer != NULL && observer != NULL)
90 {
91 subscribed = true;
92 int foundIndex = NumberChangedFunctionList_Find(&producer->observerList, observer);
93 if (foundIndex == -1)
94 {
95 subscribed = NumberChangedFunctionList_Add(&producer->observerList, observer);
96 }
97 }
98 return subscribed;
99}
100
102// NumberProducer_UnsubscribeFromNumberChanged()
105{
106 if (producer != NULL && observer != NULL)
107 {
108 int foundIndex = NumberChangedFunctionList_Find(&producer->observerList, observer);
109 if (foundIndex != -1)
110 {
111 NumberChangedFunctionList_Remove(&producer->observerList, foundIndex);
112 }
113 }
114}
115
117// NumberProducer_UpdateNumber()
120{
121 if (producer != NULL)
122 {
123 producer->number++;
125 }
126}
bool NumberChangedFunctionList_Add(NumberChangedFunctionList *functionList, NumberChangedFunction function)
Add a function pointer to the given function pointer list.
bool NumberChangedFunctionList_Copy(NumberChangedFunctionList *sourceList, NumberChangedFunctionList *destinationList)
Duplicate the given source function pointer list into the destination function pointer list.
void NumberChangedFunctionList_Remove(NumberChangedFunctionList *functionList, int functionIndex)
Removed the function pointer at the given index from the function pointer list.
int NumberChangedFunctionList_Find(NumberChangedFunctionList *functionList, NumberChangedFunction function)
Search the function list for the specified function pointer.
void NumberChangedFunctionList_Clear(NumberChangedFunctionList *functionList)
Clear the given function pointer list, releasing all associated memory. The function list can then be...
void(* NumberChangedFunction)(uint32_t)
Alias for a function that receives notifications about a number change.
bool NumberProducer_SubscribeToNumberChanged(NumberProducer *producer, NumberChangedFunction observer)
Subscribe to the given NumberProducer to received changes to that producer's number....
static void _NumberProducer_NotifyNumberChanged(NumberProducer *producer)
Helper function to notify all observers that the number has changed in the given NumberProducer objec...
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()...
Represents a dynamic list of function pointers of the type NumberChangedFunction.
size_t functions_count
Number of active function pointers in the functions array.
NumberChangedFunction * functions
Array of function pointers.
Represents the Subject in this example. In this case, a structure that contains a list of observers a...
NumberChangedFunctionList observerList
The list of observers subscribed to this class instance.
uint32_t number
The number being maintained.