Declaration of the NumberProducer structure along with its support functions, NumberProducer_Create(), NumberProducer_Destroy(), NumberProducer_SubscribeToNumberChanged(), NumberProducer_UnsubscribeFromNumberChanged(), and NumberProducer_UpdateNumber(), as used in the Observer Pattern. More...
#include "Observer_NumberChangedFunctionList.h"
Go to the source code of this file.
Classes | |
struct | NumberProducer |
Represents the Subject in this example. In this case, a structure that contains a list of observers and a single number that is updated. When the NumberProducer_UpdateNumber() function is called, the number is incremented and all observers are notified. The observers are passed the changed number. More... | |
Macros | |
#define | __OBSERVER_NUMBERPRODUCER_H__ |
Functions | |
NumberProducer * | NumberProducer_Create (uint32_t number) |
Create an instance of the NumberProducer structure and initialize the structure with the specified number. | |
void | NumberProducer_Destroy (NumberProducer *producer) |
Destroy the given instance of a NumberProducer object. After this function returns, the pointer to the NumberProducer instance is no longer valid. | |
bool | NumberProducer_SubscribeToNumberChanged (NumberProducer *producer, NumberChangedFunction observer) |
Subscribe to the given NumberProducer to received changes to that producer's number. Does nothing if the given observer is already subscribed. | |
void | NumberProducer_UnsubscribeFromNumberChanged (NumberProducer *producer, NumberChangedFunction observer) |
Unsubscribe from the Given NumberProducer so the given observer will no longer be called when the producer's number is changed. | |
void | NumberProducer_UpdateNumber (NumberProducer *producer) |
Update the number in the given NumberProducer object, triggering a call to all observer in the producer. | |
Declaration of the NumberProducer structure along with its support functions, NumberProducer_Create(), NumberProducer_Destroy(), NumberProducer_SubscribeToNumberChanged(), NumberProducer_UnsubscribeFromNumberChanged(), and NumberProducer_UpdateNumber(), as used in the Observer Pattern.
Definition in file Observer_NumberProducer.h.
#define __OBSERVER_NUMBERPRODUCER_H__ |
Definition at line 11 of file Observer_NumberProducer.h.
NumberProducer * NumberProducer_Create | ( | uint32_t | number | ) |
Create an instance of the NumberProducer structure and initialize the structure with the specified number.
number | The number to start off with. |
Definition at line 54 of file Observer_NumberProducer.c.
References NumberProducer::number.
Referenced by Observer_Exercise().
void NumberProducer_Destroy | ( | NumberProducer * | producer | ) |
Destroy the given instance of a NumberProducer object. After this function returns, the pointer to the NumberProducer instance is no longer valid.
producer | The NumberProducer object to destroy. |
Definition at line 73 of file Observer_NumberProducer.c.
References NumberChangedFunctionList_Clear(), and NumberProducer::observerList.
Referenced by Observer_Exercise().
bool NumberProducer_SubscribeToNumberChanged | ( | NumberProducer * | producer, |
NumberChangedFunction | observer | ||
) |
Subscribe to the given NumberProducer to received changes to that producer's number. Does nothing if the given observer is already subscribed.
producer | The NumberProducer object to subscribe to. |
observer | A function to be called when the producer's number changes. |
In a multi-threaded environment, this method would use a lock of some form. This example doesn't use multiple threads so no lock is needed. See the HandlerChain::SendMessage() method in HandlerChain_Class.cpp for an example of such a lock.
Definition at line 85 of file Observer_NumberProducer.c.
References NumberChangedFunctionList_Add(), NumberChangedFunctionList_Find(), and NumberProducer::observerList.
Referenced by Observer_Exercise().
void NumberProducer_UnsubscribeFromNumberChanged | ( | NumberProducer * | producer, |
NumberChangedFunction | observer | ||
) |
Unsubscribe from the Given NumberProducer so the given observer will no longer be called when the producer's number is changed.
producer | The NumberProducer object to unsubscribe from. |
observer | The function that was being called as the observer. |
Definition at line 104 of file Observer_NumberProducer.c.
References NumberChangedFunctionList_Find(), NumberChangedFunctionList_Remove(), and NumberProducer::observerList.
Referenced by Observer_Exercise().
void NumberProducer_UpdateNumber | ( | NumberProducer * | producer | ) |
Update the number in the given NumberProducer object, triggering a call to all observer in the producer.
Definition at line 119 of file Observer_NumberProducer.c.
References _NumberProducer_NotifyNumberChanged(), and NumberProducer::number.
Referenced by Observer_Exercise().