Python: Visitor Pattern

GeovinDuVisitor.py

# 访问者模式 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 调用

# 访问者模式 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))

  

输出:

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

  

posted @ 2022-10-25 23:10  ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报