class Burger:
def __init__(self, patty, cheese, toppings):
self.patty = patty
self.cheese = cheese
self.toppings = toppings
def __repr__(self):
return f'Patty: {self.patty}, Cheese: {self.cheese}, Toppings: {self.toppings}'
class BurgerBuilder:
def __init__(self):
self.patty = None
self.cheese = None
self.toppings = []
def add_patty(self, patty):
self.patty = patty
return self
def add_cheese(self, cheese):
self.cheese = cheese
return self
def add_topping(self, topping):
self.toppings.append(topping)
return self
def build(self):
return Burger(self.patty, self.cheese, self.toppings)
burger_builder = BurgerBuilder()
burger = burger_builder.add_patty('Beef').add_cheese('Cheddar').add_topping('Lettuce').add_topping('Tomato').build()
print(burger)
python 建造者模式实例
最新推荐文章于 2024-06-14 02:02:23 发布