Design Pattern Examples
Overview of object-oriented design patterns
c/Visitor_Shop.h File Reference

Declaration of the Visitor_Shop structure and its supporting functions, Shop_Create(), Shop_Destroy(), Shop_PlaceOrder(), and Shop_PickupOrder(), as used in the Visitor Pattern. More...

Include dependency graph for c/Visitor_Shop.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Visitor_Shop
 Represents a shop in the village that can be visited. More...
 

Macros

#define __VISITOR_SHOP_H__
 

Enumerations

enum  PlaceOrderReponse { PlaceOrderResponse_OrderAccepted , PlaceOrderResponse_OrderIgnored , PlaceOrderResponse_Error }
 Represents the possible responses from the Shop_PlaceOrder() function. More...
 

Functions

Visitor_ShopShop_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, address, and Village object.
 
void Shop_Destroy (Visitor_Shop *shop)
 Destroy an instance of the Visitor_Shop structure, freeing up any memory that may have been allocated to the shop. After this function returns, the pointer to the Visitor_Shop is no longer valid.
 
PlaceOrderReponse Shop_PlaceOrder (Visitor_Shop *shop, ConstStringList *itemsToOrder)
 Visit the specified Visitor_Shop object to try to place an order as described in the OrderVisitor object.
 
bool Shop_PickupOrder (Visitor_Shop *shop, ConstStringList *items, ConstStringList *itemsToBePickedUp)
 Pick up the items sold by this shop (assumes the items were ordered already). Basically, this reduces the inventory for the given items that are sold by this shop.
 

Detailed Description

Declaration of the Visitor_Shop structure and its supporting functions, Shop_Create(), Shop_Destroy(), Shop_PlaceOrder(), and Shop_PickupOrder(), as used in the Visitor Pattern.

Definition in file c/Visitor_Shop.h.

Macro Definition Documentation

◆ __VISITOR_SHOP_H__

#define __VISITOR_SHOP_H__

Definition at line 9 of file c/Visitor_Shop.h.

Enumeration Type Documentation

◆ PlaceOrderReponse

Represents the possible responses from the Shop_PlaceOrder() function.

Enumerator
PlaceOrderResponse_OrderAccepted 

Order was accepted.

PlaceOrderResponse_OrderIgnored 

Order was ignored.

PlaceOrderResponse_Error 

There was an error placing the order, likely an out of memory condition.

Definition at line 19 of file c/Visitor_Shop.h.

Function Documentation

◆ Shop_Create()

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, address, and Village object.

Parameters
nameThe name of the shop.
addressThe address of the shop.
villageThe Village in which this shop exists.
Returns
Returns a Visitor_Shop object if successful; otherwise, returns NULL, indicating an out of memory condition (or NULL arguments).

Definition at line 140 of file Visitor_Shop.c.

References Visitor_Shop::Address, Visitor_Shop::IngredientsForItems, Visitor_Shop::Inventory, MapOfInt_Initialize(), MapOfStrings_Initialize(), Visitor_Shop::Name, and Visitor_Shop::Village.

Referenced by Village_Load().

◆ Shop_Destroy()

void Shop_Destroy ( Visitor_Shop shop)

Destroy an instance of the Visitor_Shop structure, freeing up any memory that may have been allocated to the shop. After this function returns, the pointer to the Visitor_Shop is no longer valid.

Parameters
shopThe Visitor_Shop object to destroy.

Definition at line 163 of file Visitor_Shop.c.

References Visitor_Shop::IngredientsForItems, Visitor_Shop::Inventory, MapOfInt_Clear(), and MapOfStrings_Clear().

Referenced by Village_Clear(), and Village_Load().

◆ Shop_PickupOrder()

bool Shop_PickupOrder ( Visitor_Shop shop,
ConstStringList items,
ConstStringList itemsToBePickedUp 
)

Pick up the items sold by this shop (assumes the items were ordered already). Basically, this reduces the inventory for the given items that are sold by this shop.

Parameters
shopThe Visitor_Shop object to visit. The shop might not carry the requested ingredients, in which case, the visit is ignored.
itemsItems that were ordered, some of which may be sold by this shop.
itemsToBePickedUpList to be filled in with the item names that were picked up.
Returns
Returns true if the order was picked up successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 331 of file Visitor_Shop.c.

References _StringizeStringList(), ConstStringList_AddString(), ConstStringList_Clear(), ConstStringList_Initialize(), DynamicString_Clear(), MapOfInt::entries, Visitor_Shop::Inventory, MapOfInt_Find(), Visitor_Shop::Name, Shop_DoesShopSellItem(), Shop_IsItemInStock(), DynamicString::string, _ConstStringList::strings, _ConstStringList::strings_count, and MapOfIntEntry::value.

Referenced by OrderVisitor_VisitShop().

◆ Shop_PlaceOrder()

PlaceOrderReponse Shop_PlaceOrder ( Visitor_Shop shop,
ConstStringList itemsToOrder 
)

Visit the specified Visitor_Shop object to try to place an order as described in the OrderVisitor object.

Parameters
shopThe Visitor_Shop object to visit. The shop might not carry the requested ingredients, in which case, the visit is ignored.
itemsToOrderA ConstStringList object containing a list of the ingredients to order.
Returns
Returns a value from the PlaceOrderReponse enumeration indicating whether the order was accepted, ignored, or there was an error fulfilling the order (typically an out of memory condition).

Definition at line 177 of file Visitor_Shop.c.

References _StringizeStringList(), ConstStringList_AddString(), ConstStringList_AddStrings(), ConstStringList_AreListsEqual(), ConstStringList_Clear(), DynamicString_Clear(), DynamicString_Initialize(), MapOfStrings::entries, Visitor_Shop::IngredientsForItems, OrderVisitor::ItemsReceived, OrderVisitor::ItemsToOrder, MapOfStrings_Find(), Visitor_Shop::Name, OrderVisitor_Clear(), PlaceOrderResponse_Error, PlaceOrderResponse_OrderAccepted, PlaceOrderResponse_OrderIgnored, Shop_AddItemToInventory(), Shop_DoesShopSellItem(), Shop_IsItemInStock(), DynamicString::string, _ConstStringList::strings, _ConstStringList::strings_count, MapOfStringsEntry::value, Visitor_Shop::Village, and Village_VisitShop().

Referenced by OrderVisitor_VisitShop().