Design Pattern Examples
Overview of object-oriented design patterns
Visitor_Exercise.c
Go to the documentation of this file.
1
5
6#include <stdio.h>
7
8#include "Visitor_Village.h"
9
10#include "Visitor_Exercise.h"
11
12//=============================================================================
13//=============================================================================
14
15
16
17//=============================================================================
18//=============================================================================
19
36// ! [Using Visitor in C]
38{
39 printf("\nVisitor Exercise\n");
40
41 printf(" Creating Village\n");
42
43 Village village;
44 Village_Initialize(&village);
45 bool success = Village_Load(&village);
46 if (success)
47 {
48 OrderVisitor visitor;
50 success = ConstStringList_AddString(&visitor.ItemsToOrder, "hamburger");
51 if (success)
52 {
53 printf(" Ordering a hamburger from a shop in the %s\n", village.Name);
54 // Visit all shops and place an order for a hamburger at the shop
55 // that sells them. We don't know which shop it is and we don't
56 // need to know until we receive the order.
57 success = Village_VisitShop(&village, &visitor);
58 if (success)
59 {
60 if (visitor.ItemsReceived.strings_count != 0)
61 {
62 // We are expecting only a single item
63 printf(" We received a %s from %s.\n",
64 visitor.ItemsReceived.strings[0],
65 visitor.ShopNameReceivedFrom);
66 }
67 else
68 {
69 printf(" Failed to receive a hamburger\n");
70 }
71
72 }
73 }
74 else
75 {
76 printf(" Error! Out of memory condition adding item to list of things to order!\n");
77 }
78 OrderVisitor_Clear(&visitor);
79
80 }
81 else
82 {
83 printf(" Error! Out of memory loading the village!\n");
84 }
85
86 Village_Clear(&village);
87
88 printf(" Done.\n");
89}
90// ! [Using Visitor in C]
void Visitor_Exercise(void)
Example of using the Visitor Pattern.
void OrderVisitor_Initialize(OrderVisitor *visitor)
Initialize the specified OrderVisitor object.
void OrderVisitor_Clear(OrderVisitor *visitor)
Clear the specified OrderVisitor object, freeing up any memory that it was using. The resulting objec...
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.
bool ConstStringList_AddString(ConstStringList *stringList, const char *string)
Add a string to the given string list.
Declaration of the Visitor_Exercise() function as used in the Visitor Pattern.
Declaration of the Visitor_Village class used in the Visitor Pattern.
size_t strings_count
Number of strings in the strings list.
const char ** strings
Pointer to an array of zero-terminated string pointers. These strings are constant and will not be du...
Represents a visitor used for ordering items from various shops. The user starts with an instance of ...
ConstStringList ItemsToOrder
Items to be ordered from any shop that sells the item.
ConstStringList ItemsReceived
List of items received from an order/pickup process.
const char * ShopNameReceivedFrom
Name of the shop that provided the item(s). Borrowed pointer.
Represents a collection of shops that can be visited.
const char * Name
Name of the village.