Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # https://metanit.com/sharp/patterns/4.1.php
- class Pizza:
- def __init__(self, name: str) -> None:
- self._name = name
- @property
- def name(self) -> str:
- return self._name
- def get_cost(self) -> int:
- raise NotImplementedError
- class ItalianPizza(Pizza):
- def __init__(self) -> None:
- super().__init__('Итальянская пицца')
- def get_cost(self) -> int:
- return 10
- class BulgerianPizza(Pizza):
- def __init__(self) -> None:
- super().__init__('Болгарская пицца')
- def get_cost(self) -> int:
- return 8
- class PizzaDecorator(Pizza):
- def __init__(self, pizza: Pizza, name: str) -> None:
- super().__init__(name)
- self._pizza = pizza
- def get_cost(self) -> int:
- raise NotImplementedError
- class TomatoPizza(PizzaDecorator):
- def __init__(self, pizza: Pizza) -> None:
- super().__init__(pizza, pizza.name + ', с томатами')
- def get_cost(self) -> int:
- return self._pizza.get_cost() + 3
- class CheesePizza(PizzaDecorator):
- def __init__(self, pizza: Pizza) -> None:
- super().__init__(pizza, pizza.name + ', с сыром')
- def get_cost(self) -> int:
- return self._pizza.get_cost() + 5
- def main():
- pizza1: Pizza = ItalianPizza()
- pizza1 = TomatoPizza(pizza1) # итальянская пицца с томатами
- print('Название: {0}'.format(pizza1.name))
- print('Цена: {0}'.format(pizza1.get_cost()))
- pizza2: Pizza = ItalianPizza()
- pizza2 = CheesePizza(pizza2) # итальянская пиццы с сыром
- print('Название: {0}'.format(pizza2.name))
- print('Цена: {0}'.format(pizza2.get_cost()))
- pizza3: Pizza = BulgerianPizza()
- pizza3 = TomatoPizza(pizza3) # итальянская пицца с томатами
- pizza3 = CheesePizza(pizza3) # болгарская пиццы с томатами и сыром
- print('Название: {0}'.format(pizza3.name))
- print('Цена: {0}'.format(pizza3.get_cost()))
- input()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement