-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor.py
67 lines (50 loc) · 1.28 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
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab/
# --------------------------------------------------------
"""Visitor"""
class FruitVisitor:
"""Visitor"""
def draw(self, fruit):
methods = {
Apple: self.draw_apple,
Pear: self.draw_pear,
}
draw = methods.get(type(fruit), self.draw_unknown)
draw(fruit)
def draw_apple(self, fruit):
print('Apple')
return fruit
def draw_pear(self, fruit):
print('Pear')
return fruit
def draw_unknown(self, fruit):
print('Fruit')
return fruit
class Fruit:
"""Fruits"""
def draw(self, visitor_):
visitor_.draw(self)
class Apple(Fruit):
"""Apple"""
class Pear(Fruit):
"""Pear"""
class Banana(Fruit):
"""Banana"""
def main():
visitor = FruitVisitor()
apple = Apple()
apple.draw(visitor)
# Apple
pear = Pear()
pear.draw(visitor)
# Pear
banana = Banana()
banana.draw(visitor)
# Fruit
if __name__ == '__main__':
main()