Design Pattern Examples
Overview of object-oriented design patterns
cplusplus/Visitor_Shop.h
Go to the documentation of this file.
1
5
6#pragma once
7#ifndef __VISITOR_SHOP_H__
8#define __VISITOR_SHOP_H__
9
10#include <string>
11#include <memory>
12
13#include "helpers/mapofstrings.h"
14
15
17{
18 class Visitor_Village; // forward declaration
19 class Visitor; // forward declaration
20
28 {
29 public:
33 using unique_ptr_t = std::unique_ptr<Visitor_Shop>;
34
35 private:
36 std::string shopName;
37 std::string shopAddress;
40
41 public:
45 std::map<std::string, int> Inventory;
46
47 //-------------------------------------------------------------------
48
53 : village(nullptr)
54 { }
55
59 virtual ~Visitor_Shop() { }
60
64 std::string Name() { return shopName; }
65 void SetName(std::string name) { shopName = name; }
66
71 std::string Address() { return shopAddress; }
72 void SetAddress(std::string address) { shopAddress = address; }
73
79
86 void SetIngredientsForItems(const MapOfStrings& ingredients) { ingredientsForItems = ingredients; }
87
88 //-------------------------------------------------------------------
89
96 bool DoesShopSellItem(std::string item);
97
104 bool IsItemInStock(std::string item);
105
110 void AddItemToInventory(std::string item);
111
119 std::string StringizeList(StringList items);
120
131 bool PlaceOrder(StringList items);
132
142 void PickupOrder(StringList items, StringList& itemsToBePickedUp);
143
149 virtual void Accept(Visitor* visitor) = 0;
150 };
151
152} // end namespace
153
154
155#endif // __VISITOR_SHOP_H__
156
Base class that all shops must implement.
void PickupOrder(StringList items, StringList &itemsToBePickedUp)
Pick up the items sold by this shop (assumes the items were ordered already). Basically,...
void SetIngredientsForItems(const MapOfStrings &ingredients)
std::string Address()
Address of the shop (could be a structure but a simple string is sufficient for this example).
bool DoesShopSellItem(std::string item)
Determine if this shop sells the specified item.
std::string StringizeList(StringList items)
Convert a string list to a comma-delimited string. Useful for displaying the list.
const MapOfStrings & IngredientsForItems()
Specifies the ingredients needed for each item sold by the shop. Also, the keys are what the shop sel...
bool PlaceOrder(StringList items)
Place an order for the specified items. If the inventory is empty, replenish the inventory by visitin...
std::map< std::string, int > Inventory
Inventory for this shop.
std::unique_ptr< Visitor_Shop > unique_ptr_t
Alias to make it easier to refer to a unique instance of this class.
virtual ~Visitor_Shop()
Virtual destructor.
void AddItemToInventory(std::string item)
Add the specified item to this shop's inventory.
Visitor_Village * Village()
The Village that contains this shop.
bool IsItemInStock(std::string item)
Determine if this shop has the specified item in stock.
virtual void Accept(Visitor *visitor)=0
The visitor will call this method on each element it wants to visit.
Represents a collection of shops that can be visited.
All visitors must implement this base class and then override one or more of the VisitXXX() methods,...
std::vector< std::string > StringList
Typedef for a vector of std::string.
The namespace containing all Design Pattern Examples implemented in C++.
Represents a list of structures that map strings to ConstStringList objects.