Design Pattern Examples
Overview of object-oriented design patterns
Observer_NumberProducer.c 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 <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include "Observer_NumberProducer.h"
Include dependency graph for Observer_NumberProducer.c:

Go to the source code of this file.

Functions

static void _NumberProducer_NotifyNumberChanged (NumberProducer *producer)
 Helper function to notify all observers that the number has changed in the given NumberProducer object.
 
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

Function Documentation

◆ _NumberProducer_NotifyNumberChanged()

static void _NumberProducer_NotifyNumberChanged ( NumberProducer producer)
static

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