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

Implementation of the Village structure's support functions, Village_Initialize(), Village_Clear(), Village_Load(), Village_AddShop(), and Village_VisitShop(), as used in the Visitor Pattern. More...

#include <string.h>
#include "Visitor_Village.h"
Include dependency graph for Visitor_Village.c:

Go to the source code of this file.

Functions

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_Shop object and is responsible for freeing up any memory associated with the shop.
 
void Village_Initialize (Village *village)
 Initialize the specified Village object.
 
void Village_Clear (Village *village)
 Clear the specified Village object, releasing any allocated memory associated with the village and its contents. The Village object is left in an newly-initialized state.
 
bool Village_Load (Village *village)
 Set up the specified Village object with all the shops that can be visited.
 
bool Village_VisitShop (Village *village, OrderVisitor *visitor)
 Visit all shops in the given Village object to find the ingredients specified in the OrderVisitor object.
 

Detailed Description

Implementation of the Village structure's support functions, Village_Initialize(), Village_Clear(), Village_Load(), Village_AddShop(), and Village_VisitShop(), as used in the Visitor Pattern.

Definition in file Visitor_Village.c.

Function Documentation

◆ Village_AddShop()

static bool Village_AddShop ( Village village,
Visitor_Shop shop 
)
static

Add a Visitor_Shop object to the specified Village. The Village object takes ownership of the Visitor_Shop object and is responsible for freeing up any memory associated with the shop.

Parameters
villageThe Village to which to add the shop.
shopThe Visitor_Shop object to add to the village.
Returns
Returns true if the shop was added successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 24 of file Visitor_Village.c.

References Village::shops, and Village::shops_count.

Referenced by Village_Load().

◆ Village_Clear()

void Village_Clear ( Village village)

Clear the specified Village object, releasing any allocated memory associated with the village and its contents. The Village object is left in an newly-initialized state.

Parameters
villageThe Village object to clear.

Definition at line 72 of file Visitor_Village.c.

References Shop_Destroy(), Village::shops, Village::shops_count, and Village_Initialize().

Referenced by Visitor_Exercise().

◆ Village_Initialize()

void Village_Initialize ( Village village)

Initialize the specified Village object.

Parameters
villageThe Village object to initialize.

Definition at line 59 of file Visitor_Village.c.

References Village::Name, Village::shops, and Village::shops_count.

Referenced by Village_Clear(), and Visitor_Exercise().

◆ Village_Load()

bool Village_Load ( Village village)

Set up the specified Village object with all the shops that can be visited.

Parameters
villageThe Village object to load.
Returns
Returns true if the Village object was loaded successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 88 of file Visitor_Village.c.

References Visitor_Shop::IngredientsForItems, MapOfStrings_AddArray(), Village::Name, Shop_Create(), Shop_Destroy(), and Village_AddShop().

Referenced by Visitor_Exercise().

◆ Village_VisitShop()

bool Village_VisitShop ( Village village,
OrderVisitor visitor 
)

Visit all shops in the given Village object to find the ingredients specified in the OrderVisitor object.

Parameters
villageThe Village object whose shops are to be visited.
visitorThe OrderVisitor object describing what is being ordered.
Returns
Returns true if the order was placed successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 338 of file Visitor_Village.c.

References OrderVisitor_VisitShop(), Village::shops, and Village::shops_count.

Referenced by Shop_PlaceOrder(), and Visitor_Exercise().