Design Pattern Examples
Overview of object-oriented design patterns
Visitor_Village.c
Go to the documentation of this file.
1
7
8#include <string.h>
9
10#include "Visitor_Village.h"
11
12
13//-----------------------------------------------------------------------------
14
24static bool Village_AddShop(Village* village, Visitor_Shop* shop)
25{
26 bool added = false;
27
28 if (village != NULL && shop != NULL)
29 {
30 Visitor_Shop** new_list = NULL;
31 if (village->shops == NULL)
32 {
33 new_list = calloc(1, sizeof(Visitor_Shop*));
34 }
35 else
36 {
37 size_t new_count = village->shops_count + 1;
38 size_t new_size = new_count * sizeof(Visitor_Shop*);
39 new_list = realloc(village->shops, new_size);
40 }
41 if (new_list != NULL)
42 {
43 village->shops = new_list;
44 village->shops[village->shops_count] = shop;
45 village->shops_count++;
46 added = true;
47 }
48 }
49
50 return added;
51}
52
53//-----------------------------------------------------------------------------
54
55
57// Village_Initialize()
60{
61 if (village != NULL)
62 {
63 village->Name = NULL;
64 village->shops = NULL;
65 village->shops_count = 0;
66 }
67}
68
70// Village_Clear()
72void Village_Clear(Village* village)
73{
74 if (village != NULL)
75 {
76 for (size_t index = 0; index < village->shops_count; index++)
77 {
78 Shop_Destroy(village->shops[index]);
79 }
80 free(village->shops);
81 Village_Initialize(village);
82 }
83}
84
86// Village_Load()
88bool Village_Load(Village* village)
89{
90 static const char* empty_ingredients[] = {NULL};
91 bool success = false;
92
93 Visitor_Shop* shop = NULL;
94 if (village != NULL)
95 {
96 village->Name = "Village of Self-Sufficiency";
97
98 shop = Shop_Create("Joe's Burger Joint", "47 Millings Rd.", village);
99 if (shop != NULL)
100 {
101 static const char* hamburger_ingredients[] = {"ground beef", "hamburger buns", "ketchup", "mustard", "mayonnaise", "lettuce", "tomato", "onion", "pickles", NULL};
102
103 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "hamburger", hamburger_ingredients);
104 if (success)
105 {
106 success = Village_AddShop(village, shop);
107 }
108 if (!success)
109 {
110 Shop_Destroy(shop);
111 shop = NULL;
112 }
113 }
114 }
115
116 if (success)
117 {
118 shop = Shop_Create("Amelia's Butcher Shop", "12 Klaxon Ave.", village);
119 if (shop != NULL)
120 {
121 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "ground beef", empty_ingredients);
122 if (success)
123 {
124 success = Village_AddShop(village, shop);
125 }
126 if (!success)
127 {
128 Shop_Destroy(shop);
129 shop = NULL;
130 }
131 }
132 }
133
134 if (success)
135 {
136 shop = Shop_Create("Oxel's Breads and Buns Bakery", "131 Worthington Dr.", village);
137 if (shop != NULL)
138 {
139 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "hamburger buns", empty_ingredients);
140 if (success)
141 {
142 success = Village_AddShop(village, shop);
143 }
144 if (!success)
145 {
146 Shop_Destroy(shop);
147 shop = NULL;
148 }
149 }
150 }
151
152 if (success)
153 {
154 shop = Shop_Create("Connie's Condiments", "83 Millings Rd.", village);
155 if (shop != NULL)
156 {
157 static const char* ketchup_ingredients[] = {"fresh ketchup", NULL};
158 static const char* mustard_ingredients[] = {"fresh mustard", NULL};
159 static const char* mayonnaise_ingredients[] = {"fresh mayonnaise", NULL};
160
161 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "ketchup", ketchup_ingredients);
162 if (success)
163 {
164 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "mustard", mustard_ingredients);
165 }
166 if (success)
167 {
168 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "mayonnaise", mayonnaise_ingredients);
169 }
170 if (success)
171 {
172 success = Village_AddShop(village, shop);
173 }
174 if (!success)
175 {
176 Shop_Destroy(shop);
177 shop = NULL;
178 }
179 }
180 }
181
182 if (success)
183 {
184 shop = Shop_Create("Florence's Vegetables", "32 Main St.", village);
185 if (shop != NULL)
186 {
187 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "lettuce", empty_ingredients);
188 if (success)
189 {
190 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "tomato", empty_ingredients);
191 }
192 if (success)
193 {
194 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "lettuce", empty_ingredients);
195 }
196 if (success)
197 {
198 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "onion", empty_ingredients);
199 }
200 if (success)
201 {
202 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "cucumber", empty_ingredients);
203 }
204 if (success)
205 {
206 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "mustard seed", empty_ingredients);
207 }
208 if (success)
209 {
210 success = Village_AddShop(village, shop);
211 }
212 if (!success)
213 {
214 Shop_Destroy(shop);
215 shop = NULL;
216 }
217 }
218 }
219
220 if (success)
221 {
222 shop = Shop_Create("Larry's Pickle Emporium", "34 Main St.", village);
223 if (shop != NULL)
224 {
225 static const char* pickles_ingredients[] = { "vinegar", "cucumber", "salt", NULL };
226
227 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "pickles", pickles_ingredients);
228 if (success)
229 {
230 success = Village_AddShop(village, shop);
231 }
232 if (!success)
233 {
234 Shop_Destroy(shop);
235 shop = NULL;
236 }
237 }
238 }
239
240 if (success)
241 {
242 shop = Shop_Create("Klyde and Sons Ketchup Makers", "800 Overtown Rd.", village);
243 if (shop != NULL)
244 {
245 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "fresh ketchup", empty_ingredients);
246 if (success)
247 {
248 success = Village_AddShop(village, shop);
249 }
250 if (!success)
251 {
252 Shop_Destroy(shop);
253 shop = NULL;
254 }
255 }
256 }
257
258 if (success)
259 {
260 shop = Shop_Create("Molly's Mustard Mart", "810 Overtown Rd.", village);
261 if (shop != NULL)
262 {
263 static const char* fresh_mustard_ingredients[] = { "vinegar", "mustard seed", NULL };
264
265 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "fresh mustard", fresh_mustard_ingredients);
266 if (success)
267 {
268 success = Village_AddShop(village, shop);
269 }
270 if (!success)
271 {
272 Shop_Destroy(shop);
273 shop = NULL;
274 }
275 }
276 }
277
278 if (success)
279 {
280 shop = Shop_Create("Turk's Mayo Supply", "820 Overtown Rd.", village);
281 if (shop != NULL)
282 {
283 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "fresh mayonnaise", empty_ingredients);
284 if (success)
285 {
286 success = Village_AddShop(village, shop);
287 }
288 if (!success)
289 {
290 Shop_Destroy(shop);
291 shop = NULL;
292 }
293 }
294 }
295
296 if (success)
297 {
298 shop = Shop_Create("Vinnies' Sour Flavors", "830 Overtown Rd.", village);
299 if (shop != NULL)
300 {
301 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "vinegar", empty_ingredients);
302 if (success)
303 {
304 success = Village_AddShop(village, shop);
305 }
306 if (!success)
307 {
308 Shop_Destroy(shop);
309 shop = NULL;
310 }
311 }
312 }
313
314 if (success)
315 {
316 shop = Shop_Create("Jessie's Salt Works", "920 Overtown Rd.", village);
317 if (shop != NULL)
318 {
319 success = MapOfStrings_AddArray(&shop->IngredientsForItems, "salt", empty_ingredients);
320 if (success)
321 {
322 success = Village_AddShop(village, shop);
323 }
324 if (!success)
325 {
326 Shop_Destroy(shop);
327 shop = NULL;
328 }
329 }
330 }
331
332 return success;
333}
334
336// Village_VisitShop()
338bool Village_VisitShop(Village* village, OrderVisitor* visitor)
339{
340 bool success = false;
341
342 if (village != NULL && visitor != NULL)
343 {
344 success = true;
345 for (size_t index = 0; index < village->shops_count; index++)
346 {
347 success = OrderVisitor_VisitShop(visitor, village->shops[index]);
348 if (!success)
349 {
350 break;
351 }
352 }
353 }
354
355 return success;
356}
bool OrderVisitor_VisitShop(OrderVisitor *order, Visitor_Shop *shop)
Represents a visit to the specified Visitor_Shop with the specified order.
void Shop_Destroy(Visitor_Shop *shop)
Destroy an instance of the Visitor_Shop structure, freeing up any memory that may have been allocated...
Definition: Visitor_Shop.c:163
Visitor_Shop * Shop_Create(const char *name, const char *address, struct Village *village)
Creates a new instance of a Visitor_Shop structure, and initializes it with the given name,...
Definition: Visitor_Shop.c:140
static bool Village_AddShop(Village *village, Visitor_Shop *shop)
Add a Visitor_Shop object to the specified Village. The Village object takes ownership of the Visitor...
void Village_Clear(Village *village)
Clear the specified Village object, releasing any allocated memory associated with the village and it...
bool Village_VisitShop(Village *village, OrderVisitor *visitor)
Visit all shops in the given Village object to find the ingredients specified in the OrderVisitor obj...
bool Village_Load(Village *village)
Set up the specified Village object with all the shops that can be visited.
void Village_Initialize(Village *village)
Initialize the specified Village object.
Declaration of the Visitor_Village class used in the Visitor Pattern.
bool MapOfStrings_AddArray(MapOfStrings *map, const char *key, const char **value)
Add a key/value association to the given MapOfStrings object, where the value is provided as a NULL-t...
Definition: mapofstrings.c:77
Represents a visitor used for ordering items from various shops. The user starts with an instance of ...
Represents a collection of shops that can be visited.
const char * Name
Name of the village.
size_t shops_count
Number of shops in the shops list.
Visitor_Shop ** shops
List of shops in this village.
Represents a shop in the village that can be visited.
MapOfStrings IngredientsForItems
Maps ingredient to list of items need for ingredient.