Design Pattern Examples
Overview of object-oriented design patterns
visitor_exercise.py
Go to the documentation of this file.
1
6
7
from
.visitor_village
import
Visitor_Village
8
from
.visitor_ordervisitor
import
OrderVisitor
9
10
24
25
# ! [Using Visitor in Python]
26
def
Visitor_Exercise
():
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]
DesignPatternExamples_python.visitor.visitor_ordervisitor.OrderVisitor
A visitor used for ordering items from various shops.
Definition:
visitor_ordervisitor.py:14
DesignPatternExamples_python.visitor.visitor_village.Visitor_Village
Represents a collection of shops that can be visited.
Definition:
visitor_village.py:14
DesignPatternExamples_python.visitor.visitor_exercise.Visitor_Exercise
def Visitor_Exercise()
Example of using the Visitor Pattern.
Definition:
visitor_exercise.py:26
python
DesignPatternExamples_python
visitor
visitor_exercise.py
Generated on Tue Aug 29 2023 19:47:44 for Design Pattern Examples by
1.9.6