Design Pattern Examples
Overview of object-oriented design patterns
Visitor_Element_Classes.cs
Go to the documentation of this file.
1
6
7using System;
8using System.Collections.Generic;
9using System.Linq;
10using System.Text;
11
13{
14
25 public class Visitor
26 {
31 public virtual void VisitRestaurant(Visitor_Restaurant shop) { }
32
37 public virtual void VisitButcher(Visitor_Butcher shop) { }
38
43 public virtual void VisitBaker(Visitor_Baker shop) { }
44
49 public virtual void VisitVegetableGrocer(Visitor_VegetableGrocer shop) { }
50
55 public virtual void VisitCondimentGrocer(Visitor_CondimentGrocer shop) { }
56
61 public virtual void VisitPickleGrocer(Visitor_PickleGrocer shop) { }
62
67 public virtual void VisitMaker(Visitor_Maker shop) { }
68
69 }
70
75 public interface ISupportVisitors
76 {
82 void Accept(Visitor visitor);
83 }
84
91 public abstract class Visitor_Shop
92 {
96 private Dictionary<string, int> Inventory = new Dictionary<string, int>();
97
101 public string Name = string.Empty;
102
107 public string Address = string.Empty;
108
112 public Village Village = new Village();
113
114 //-------------------------------------------------------------------
115
121 public Dictionary<string, string[]> IngredientsForItems = new Dictionary<string, string[]>();
122
123
130 bool DoesShopSellItem(string item)
131 {
132 return IngredientsForItems.ContainsKey(item);
133 }
134
141 bool IsItemInStock(string item)
142 {
143 return Inventory.ContainsKey(item) && Inventory[item] > 0;
144 }
145
150 void AddItemToInventory(string item)
151 {
152 if (!Inventory.ContainsKey(item))
153 {
154 Inventory.Add(item, 1);
155 }
156 else
157 {
158 Inventory[item]++;
159 }
160 }
161
169 string StringizeList(string[] items)
170 {
171 StringBuilder output = new StringBuilder();
172
173 foreach(string item in items)
174 {
175 if (output.Length > 0)
176 {
177 output.Append(", ");
178 }
179 output.Append(item);
180 }
181 return output.ToString();
182 }
183
191 bool AreListContentsTheSame(string[] left, string[] right)
192 {
193 bool matched = false;
194
195 if (left.Length == right.Length)
196 {
197 matched = true;
198 foreach (string item in left)
199 {
200 if (!right.Contains(item))
201 {
202 matched = false;
203 break;
204 }
205 }
206 }
207 return matched;
208 }
209
220 public bool PlaceOrder(string[] items)
221 {
222 bool orderPlaced = false;
223 List<string> outOfStockItems = new List<string>();
224 List<string> itemsInThisShop = new List<string>();
225
226 foreach (string item in items)
227 {
228 if (DoesShopSellItem(item))
229 {
230 if (!IsItemInStock(item))
231 {
232 outOfStockItems.Add(item);
233 }
234 itemsInThisShop.Add(item);
235 }
236 }
237
238 if (itemsInThisShop.Count > 0)
239 {
240 Console.WriteLine(" {0}: Received an order for {1}.", Name, StringizeList(itemsInThisShop.ToArray()));
241 orderPlaced = true;
242 }
243 if (outOfStockItems.Count > 0)
244 {
245 foreach (string itemToOrder in outOfStockItems)
246 {
247 if (IngredientsForItems[itemToOrder].Length > 0)
248 {
249 Console.WriteLine(" {0}: {1} out of stock, ordering ingredients to make more...", Name, itemToOrder);
250 OrderVisitor visitor = new OrderVisitor(IngredientsForItems[itemToOrder]);
251 Village.Accept(visitor);
252 if (AreListContentsTheSame(visitor.ItemsReceived.ToArray(), IngredientsForItems[itemToOrder]))
253 {
254 // verify the ingredients received matches the
255 // ingredients needed. Only then add 1 to the
256 // inventory.
257 AddItemToInventory(itemToOrder);
258 }
259 }
260 else
261 {
262 // The ordered item has no ingredients so the
263 // ordered item will be magically added to inventory
264 Console.WriteLine(" {0}: {1} out of stock, making...", Name, itemToOrder);
265 AddItemToInventory(itemToOrder);
266 }
267 }
268 }
269 return orderPlaced;
270 }
271
281 public void PickupOrder(string[] items, List<string> itemsToBePickedUp)
282 {
283 foreach (string item in items)
284 {
285 // If this shop sells the item and the item is in stock then
286 if (DoesShopSellItem(item))
287 {
288 if (IsItemInStock(item))
289 {
290 itemsToBePickedUp.Add(item);
291 }
292 else
293 {
294 Console.WriteLine(" Error! {0}: Item {1} is not in the inventory when it should be.", Name, item);
295 }
296 }
297 }
298
299 if (itemsToBePickedUp.Count > 0)
300 {
301 // Reduce inventory for the ordered items
302 StringBuilder output = new StringBuilder();
303 foreach (string itemToBePickedUp in itemsToBePickedUp)
304 {
305 if (DoesShopSellItem(itemToBePickedUp))
306 {
307 if (output.Length > 0)
308 {
309 output.Append(", ");
310 }
311 output.Append(itemToBePickedUp);
312 Inventory[itemToBePickedUp]--;
313 }
314 }
315 Console.WriteLine(" {0}: Order picked up for {1}.", Name, output.ToString());
316 }
317 }
318
324 public abstract void Accept(Visitor visitor);
325 }
326
327
328 //-------------------------------------------------------------------------
329 //-------------------------------------------------------------------------
330
331
336 {
341 public override void Accept(Visitor visitor)
342 {
343 visitor.VisitRestaurant(this);
344 }
345 }
346
347
348 //-------------------------------------------------------------------------
349 //-------------------------------------------------------------------------
350
351
356 {
361 public override void Accept(Visitor visitor)
362 {
363 visitor.VisitButcher(this);
364 }
365 }
366
367
368 //-------------------------------------------------------------------------
369 //-------------------------------------------------------------------------
370
371
376 {
381 public override void Accept(Visitor visitor)
382 {
383 visitor.VisitBaker(this);
384 }
385 }
386
387
388 //-------------------------------------------------------------------------
389 //-------------------------------------------------------------------------
390
391
396 {
401 public override void Accept(Visitor visitor)
402 {
403 visitor.VisitVegetableGrocer(this);
404 }
405 }
406
407
408 //-------------------------------------------------------------------------
409 //-------------------------------------------------------------------------
410
411
416 {
421 public override void Accept(Visitor visitor)
422 {
423 visitor.VisitCondimentGrocer(this);
424 }
425 }
426
427
428 //-------------------------------------------------------------------------
429 //-------------------------------------------------------------------------
430
431
436 {
441 public override void Accept(Visitor visitor)
442 {
443 visitor.VisitPickleGrocer(this);
444 }
445 }
446
447
448 //-------------------------------------------------------------------------
449 //-------------------------------------------------------------------------
450
451
456 {
461 public override void Accept(Visitor visitor)
462 {
463 visitor.VisitMaker(this);
464 }
465 }
466}
A visitor used for ordering items from various shops. The user starts with an instance of this class ...
List< string > ItemsReceived
List of items received from an order/pickup process.
Represents a collection of shops that can be visited.
void Accept(Visitor visitor)
The visitor will call this method on each element it wants to visit.
override void Accept(Visitor visitor)
Handle visitors to this class.
override void Accept(Visitor visitor)
Handle visitors to this class.
override void Accept(Visitor visitor)
Handle visitors to this class.
override void Accept(Visitor visitor)
Handle visitors to this class.
override void Accept(Visitor visitor)
Handle visitors to this class.
override void Accept(Visitor visitor)
Handle visitors to this class.
Base class that all shops must implement.
bool AreListContentsTheSame(string[] left, string[] right)
Determine if the two string lists have the same contents.
abstract void Accept(Visitor visitor)
The visitor will call this method on each element it wants to visit.
void AddItemToInventory(string item)
Add the specified item to this shop's inventory.
Dictionary< string, int > Inventory
Inventory for this shop.
Dictionary< string, string[]> IngredientsForItems
Specifies the ingredients needed for each item sold by the shop. Also, the keys are what the shop sel...
bool PlaceOrder(string[] items)
Place an order for the specified items. If the inventory is empty, replenish the inventory by visitin...
Village Village
The Village that contains this shop.
string StringizeList(string[] items)
Convert a string list to a comma-delimited string. Useful for displaying the list.
bool DoesShopSellItem(string item)
Determine if this shop sells the specified itrem.
bool IsItemInStock(string item)
Determine if this shop has the specified item in stock.
string Address
Address of the shop (could be a structure but a simple string is sufficient for this example).
void PickupOrder(string[] items, List< string > itemsToBePickedUp)
Pick up the items sold by this shop (assumes the items were ordered already). Basically,...
override void Accept(Visitor visitor)
Handle visitors to this class.
All visitors must implement this base class and then override one or more of the VisitXXX() methods,...
virtual void VisitPickleGrocer(Visitor_PickleGrocer shop)
Let the visitor visit a Visitor_PickleGrocer shop.
virtual void VisitMaker(Visitor_Maker shop)
Let the visitor visit a Visitor_Maker shop.
virtual void VisitBaker(Visitor_Baker shop)
Let the visitor visit a Visitor_Baker shop.
virtual void VisitButcher(Visitor_Butcher shop)
Let the visitor visit a Visitor_Butcher shop.
virtual void VisitCondimentGrocer(Visitor_CondimentGrocer shop)
Let the visitor visit a Visitor_CondimentGrocer shop.
virtual void VisitVegetableGrocer(Visitor_VegetableGrocer shop)
Let the visitor visit a Visitor_VegetableGrocer shop.
virtual void VisitRestaurant(Visitor_Restaurant shop)
Let the visitor visit a Visitor_Restaurant shop.
The interface that all element classes must implement if they are to participate in the visitor patte...
void Accept(Visitor visitor)
The visitor will call this method on each element it wants to visit.
The namespace containing all Design Pattern Examples implemented in C#.