Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_Exercise.c File Reference

Implementation of the HandlerChain_Exercise() function as used in the HandlerChain Pattern. More...

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "HandlerChain_MessageWindow.h"
#include "HandlerChain_HandlerFunctions.h"
#include "HandlerChain_Exercise.h"
Include dependency graph for HandlerChain_Exercise.c:

Go to the source code of this file.

Functions

static void _HandlerChain_ConstructWindowChain (int windowIds[3])
 Helper function to construct a list of windows. Messages will be passed to these windows via the HandlerChain_SendMessage() function.
 
static void _HandleChain_DestroyWindows (int windowIds[3])
 Helper function to destroy all windows that have been created.
 
static void _ShowHandlerChain (const char *prompt)
 Helper function to display the current handler chain.
 
void HandlerChain_Exercise (void)
 Example of using the HandlerChain Pattern or Chain of Responsibility design pattern.
 

Detailed Description

Implementation of the HandlerChain_Exercise() function as used in the HandlerChain Pattern.

Definition in file HandlerChain_Exercise.c.

Function Documentation

◆ _HandleChain_DestroyWindows()

static void _HandleChain_DestroyWindows ( int  windowIds[3])
static

Helper function to destroy all windows that have been created.

Parameters
windowIdsList of window IDs to destroy.

Definition at line 38 of file HandlerChain_Exercise.c.

References Destroy, HandlerChain_SendMessage(), and Message_Initialize().

Referenced by HandlerChain_Exercise().

◆ _HandlerChain_ConstructWindowChain()

static void _HandlerChain_ConstructWindowChain ( int  windowIds[3])
static

Helper function to construct a list of windows. Messages will be passed to these windows via the HandlerChain_SendMessage() function.

Parameters
windowIdsArray of ints that will be filled in with the IDs of each window that is created so they can later be destroyed. Only 3 windows are created, hence the fixed size of the array.

Definition at line 26 of file HandlerChain_Exercise.c.

References MessageWindow_Create().

Referenced by DesignPatternExamples_cpp::HandlerChain_Exercise(), and HandlerChain_Exercise().

◆ _ShowHandlerChain()

static void _ShowHandlerChain ( const char *  prompt)
static

Helper function to display the current handler chain.

Parameters
promptA prompt to display before the handler chain.

Definition at line 53 of file HandlerChain_Exercise.c.

References DynamicString_Clear(), HandlerChain_ToString(), and DynamicString::string.

Referenced by HandlerChain_Exercise().

◆ HandlerChain_Exercise()

void HandlerChain_Exercise ( void  )

Example of using the HandlerChain Pattern or Chain of Responsibility design pattern.

The Handler Chain pattern is used to support a dynamic list of handlers that are passed the same arguments. It is kind of the inverse of the Visitor pattern, where the Visitor pattern is a handler that is passed to all objects in a list and the Handler Chain pattern is an object passed to handlers in a list.

In this exercise, multiple rectangular regions represented by the MessageWindow structure are represented by window IDs. The IDs are kept in a list to which messages can be passed, using the HandlerChain_SendMessage() function. That function iterates through the list of window IDs and calls MessageWindow_ProcessMessage() for each ID with the desired message. MessageWindow_ProcessMessage() passes control to one of several handler functions based on the type of the message.

Definition at line 88 of file HandlerChain_Exercise.c.

References _HandleChain_DestroyWindows(), _HandlerChain_ConstructWindowChain(), _ShowHandlerChain(), ButtonDown, ButtonUp, HandlerChain_SendMessage(), and Message_Initialize().