-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvisitor.py
69 lines (52 loc) · 1.72 KB
/
visitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Visitor.
Represent an operation to be performed
on the elements of an object structure. Visitor lets
you define a new operation without changing the classes
of the elements on which it operates.
<ObjectStructure> <Element> <Visitor>
School (create)-> Engineer <==> HR
| | |
SoftwareEngineer IncomeVisitor VacationVisitor
"""
# ConcreteVisitor1.
class IncomeVisitor:
def visit(self, engineer):
engineer.show_income()
# ConcreteVisitor2.
class VacationVisitor:
def visit(self, engineer):
engineer.show_vacation()
# ConcreteElement
class SoftwareEngineer:
def __init__(self, name, income, vacation):
self.name = name
self.income = income
self.vacation = vacation
def accept(self, visitor):
visitor.visit(self)
def show_income(self):
print("%s's income: %d" % (self.name, self.income))
def show_vacation(self):
print("%s's vacation: %d" % (self.name, self.vacation))
# ObjectStructure. (Element producer).
# It is typically contains multiple containers of different types
# and interfaces, such as List, Set, Map, etc.
class School:
def create_software_engineer(self, name, income, vacation):
return SoftwareEngineer(name, income, vacation)
if __name__ == '__main__':
# Create two visitors
v1 = IncomeVisitor()
v2 = VacationVisitor()
# Create Engineers by School.
THU = School()
Tom = THU.create_software_engineer("Tom", 25000, 14);
Jimmy = THU.create_software_engineer("Jimmy", 36000, 6);
Tom.accept(v1);
Tom.accept(v2);
Jimmy.accept(v1);
Jimmy.accept(v2);