Design Pattern Examples
Overview of object-oriented design patterns
Observer_NumberProducer.h File Reference

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 dependency graph for Observer_NumberProducer.h:
This graph shows which files directly or indirectly include this file:

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

NumberProducerNumberProducer_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.
 

Detailed Description

Macro Definition Documentation

◆ __OBSERVER_NUMBERPRODUCER_H__

#define __OBSERVER_NUMBERPRODUCER_H__

Definition at line 11 of file Observer_NumberProducer.h.

Function Documentation

◆ NumberProducer_Create()

NumberProducer * NumberProducer_Create ( uint32_t  number)

Create an instance of the NumberProducer structure and initialize the structure with the specified number.

Parameters
numberThe number to start off with.
Returns
Returns a pointer to a NumberProducer object if successful; otherwise, returns NULL, indicating an out of memory condition (or a NULL argument).

Definition at line 54 of file Observer_NumberProducer.c.

References NumberProducer::number.

Referenced by Observer_Exercise().

◆ NumberProducer_Destroy()

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.

Parameters
producerThe NumberProducer object to destroy.

Definition at line 73 of file Observer_NumberProducer.c.

References NumberChangedFunctionList_Clear(), and NumberProducer::observerList.

Referenced by Observer_Exercise().

◆ NumberProducer_SubscribeToNumberChanged()

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.

Parameters
producerThe NumberProducer object to subscribe to.
observerA function to be called when the producer's number changes.
Returns
Returns true if the subscribe operation succeeded; otherwise, returns false, indicating an out of memory condition (or a NULL argument was passed in).

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().

◆ NumberProducer_UnsubscribeFromNumberChanged()

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.

Parameters
producerThe NumberProducer object to unsubscribe from.
observerThe 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().

◆ NumberProducer_UpdateNumber()

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().