Python: Visitor Pattern

GeovinDuVisitor.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 访问者模式 Visitor Pattern GeovinDuVistitor.py
 
class Courses_At_GFG:
 
    def accept(self, visitor):
        visitor.visit(self)
 
    def teaching(self, visitor):
        print(self, "教授於 ", visitor)
 
    def studying(self, visitor):
       print(self, "學習於 ", visitor)
 
    def __str__(self):
       return self.__class__.__name__
 
 
"""Concrete Courses_At_GFG class: Classes being visited."""
 
 
class SDE(Courses_At_GFG): pass
 
 
class STL(Courses_At_GFG): pass
 
 
class DSA(Courses_At_GFG): pass
 
 
""" Abstract Visitor class for Concrete Visitor classes:
 method defined in this class will be inherited by all
 Concrete Visitor classes."""
 
 
class Visitor:
 
  def __str__(self):
        return self.__class__.__name__
 
 
class Instructor(Visitor):
    def visit(self, crop):
        crop.teaching(self)
 
 
class Student(Visitor):
    def visit(self, crop):
        crop.studying(self)
 
# 二類
class Item():
    """Visitable class"""
    def accept(self):
        pass
 
class Shirt(Item):
    def __init__(self, price, size):
        self.price = price
        self.size = size
 
    def get_price(self):
        return self.price
 
    def get_size(self):
        return self.size
 
    def accept(self, visitor):
        return visitor.visit(self)
 
class Book(Item):
    def __init__(self, cost, genre):
        self.price = cost
        self.genre = genre
 
    def get_price(self):
        return self.price
 
    def get_genre(self):
        return self.genre
 
    def accept(self, visitor):
        return visitor.visit(self)
 
class Visitor():
    """Abstract Vistor Class"""
 
    def visit(self, item):
        pass
 
class CartVisitor(Visitor):
    def visit(self, item):
        if isinstance(item, Book):
            cost = item.get_price()
            print("书的类型: {}, 费用 = ${}".format(item.get_genre(), cost))
            return cost
        elif isinstance(item, Shirt):
            cost = item.get_price()
            print("衬衫,尺寸{} 费用 = ${}".format(item.get_size(), cost))
            return cost
 
def calculate_price(items):
    visitor = CartVisitor()
    sum = 0
    for item in items:
        sum = sum + item.accept(visitor)
 
    return sum

  

main.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
# 访问者模式 Visitor Pattern
sde = GeovinDuVisitor.SDE()
stl = GeovinDuVisitor.STL()
dsa = GeovinDuVisitor.DSA()
 
"""Creating Visitors"""
instructor = GeovinDuVisitor.Instructor()
student = GeovinDuVisitor.Student()
 
"""Visitors visiting courses"""
sde.accept(instructor)
sde.accept(student)
 
stl.accept(instructor)
stl.accept(student)
 
dsa.accept(instructor)
dsa.accept(student)
 
items = [
        GeovinDuVisitor.Shirt(10, "XL"),
        GeovinDuVisitor.Shirt(15, "XXL"),
        GeovinDuVisitor.Book(20, "虚构小说"),
        GeovinDuVisitor.Book(100, "自助"),
    ]
 
total = GeovinDuVisitor.calculate_price(items)
print("涂聚文所购总成本 = ${}".format(total))

  

输出:

1
2
3
4
5
6
7
8
9
10
11
SDE 教授於  Instructor
SDE 學習於  Student
STL 教授於  Instructor
STL 學習於  Student
DSA 教授於  Instructor
DSA 學習於  Student
衬衫,尺寸XL 费用 = $10
衬衫,尺寸XXL 费用 = $15
书的类型: 虚构小说, 费用 = $20
书的类型: 自助, 费用 = $100
涂聚文所购总成本 = $145

  

posted @   ®Geovin Du Dream Park™  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示