Design Pattern Examples
Overview of object-oriented design patterns
visitor_exercise.py
Go to the documentation of this file.
6
7from .visitor_village import Visitor_Village
8from .visitor_ordervisitor import OrderVisitor
9
10
24
25# ! [Using Visitor in Python]
27 print()
28 print("Visitor Exercise")
29
30 print(" Creating Village")
31 village = Visitor_Village()
32 village.LoadVillage()
33
34 visitor = OrderVisitor([ "hamburger" ])
35 print(" Ordering a hamburger from a shop in the {0}".format(village.Name))
36
37 # Visit all shops and place an order for a hamburger at the shop
38 # that sells them. We don't know which shop it is and we don't
39 # need to know until we receive the order.
40 village.Accept(visitor)
41 if visitor.ItemsReceived:
42 # We are expecting only a single item
43 print(" We received a {0} from {1}.".format(visitor.ItemsReceived[0], visitor.ShopNameReceivedFrom))
44 else:
45 print(" Failed to receive a {0}".format(visitor.ItemsToOrder[0]))
46
47 print(" Done.")
48# ! [Using Visitor in Python]
A visitor used for ordering items from various shops.
Represents a collection of shops that can be visited.
def Visitor_Exercise()
Example of using the Visitor Pattern.