Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_Exercise.c
Go to the documentation of this file.
1
6
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
15
16//=============================================================================
17//=============================================================================
18
26static void _HandlerChain_ConstructWindowChain(int windowIds[3])
27{
28 windowIds[0] = MessageWindow_Create("Window 1", 0, 0, 10, 10);
29 windowIds[1] = MessageWindow_Create("Window 2", 20, 0, 5, 5);
30 windowIds[2] = MessageWindow_Create("Window 3", 30, 10, 15, 15);
31}
32
33
38static void _HandleChain_DestroyWindows(int windowIds[3])
39{
40 size_t numIds = 3;
41 Message destroyMessage;
42 Message_Initialize(&destroyMessage, Destroy, 0, 0);
43 for (size_t index = 0; index < numIds; index++)
44 {
45 HandlerChain_SendMessage(windowIds[index], &destroyMessage);
46 }
47}
48
53static void _ShowHandlerChain(const char* prompt)
54{
55 if (prompt == NULL)
56 {
57 prompt = "<NO PROMPT>";
58 }
59 printf(" %s\n", prompt);
60 DynamicString output = { 0 };
61 HandlerChain_ToString(&output);
62 printf("%s\n", output.string);
63 DynamicString_Clear(&output);
64}
65
66//=============================================================================
67//=============================================================================
68
87// ! [Using HandlerChain in C]
89{
90 printf("\nHandlerChain Exercise\n");
91
92 // Construct several windows that can handle messages. These are
93 // automatically handled to the handler chain list during construction.
94 int windowIds[3] = { 0 };
96
97 _ShowHandlerChain("Handler Chain at start:");
98
99 // Now pass messages to the windows.
100
101 printf(" Select Window 2\n");
102 Message buttonDownMessage;
103 Message_Initialize(&buttonDownMessage, ButtonDown, 22, 1);
104 HandlerChain_SendMessage(-1, &buttonDownMessage);
105
106 Message buttonUpMessage;
107 Message_Initialize(&buttonUpMessage, ButtonUp, 22, 1);
108 HandlerChain_SendMessage(-1, &buttonUpMessage);
109 _ShowHandlerChain("Current handler chain:");
110
111 printf(" Select Window 3\n");
112 Message_Initialize(&buttonDownMessage, ButtonDown, 35, 11);
113 HandlerChain_SendMessage(-1, &buttonDownMessage);
114
115 Message_Initialize(&buttonUpMessage, ButtonUp, 35, 11);
116 HandlerChain_SendMessage(-1, &buttonUpMessage);
117 _ShowHandlerChain("Current handler chain:");
118
119 printf(" Select Window 1\n");
120 Message_Initialize(&buttonDownMessage, ButtonDown, 4, 4);
121 HandlerChain_SendMessage(-1, &buttonDownMessage);
122
123 Message_Initialize(&buttonUpMessage, ButtonUp, 4, 4);
124 HandlerChain_SendMessage(-1, &buttonUpMessage);
125 _ShowHandlerChain("Current handler chain:");
126
127 printf(" Close Window 2\n");
128 Message_Initialize(&buttonDownMessage, ButtonDown, 24, 0);
129 HandlerChain_SendMessage(-1, &buttonDownMessage);
130
131 Message_Initialize(&buttonUpMessage, ButtonUp, 24, 0);
132 HandlerChain_SendMessage(-1, &buttonUpMessage);
133 _ShowHandlerChain("Current handler chain:");
134
135 printf(" Removing all windows as part of clean-up.\n");
137
138 printf(" Done.\n");
139}
140// ! [Using HandlerChain in C]
void HandlerChain_Exercise(void)
Example of using the HandlerChain Pattern or Chain of Responsibility design pattern.
static void _HandlerChain_ConstructWindowChain(int windowIds[3])
Helper function to construct a list of windows. Messages will be passed to these windows via the Hand...
static void _ShowHandlerChain(const char *prompt)
Helper function to display the current handler chain.
static void _HandleChain_DestroyWindows(int windowIds[3])
Helper function to destroy all windows that have been created.
bool HandlerChain_ToString(DynamicString *output)
Convert the Handler Chain to a string, protected by a multi-threading lock.
void HandlerChain_SendMessage(int windowId, Message *message)
Send a message to each of the handlers in the list, protected by a multi-threading lock.
Declaration of the Handler Chain functions, HandlerChain_SendMessage(), HandlerChain_AddWindow(),...
void Message_Initialize(Message *message, MessageType type, int x, int y)
Initialize a Message structure.
@ ButtonUp
Take an action on the currently selected window.
@ ButtonDown
Selects a window based on position.
@ Destroy
Window is being told to destroy itself. This is sent in response to seeing the Close message.
int MessageWindow_Create(const char *title, int x, int y, int w, int h)
Create a MessageWindow object with the given properties and return the ID of the object.
Declaration of the MessageWindow support functions, MessageWindow_Create(), MessageWindow_Destroy(),...
Declaration of the HandlerChain_Exercise() function as used in the HandlerChain Pattern.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17
Represents a message sent to the windows. A message contains a type and a position.