Advertisement
andreymal

pizza.py

Sep 26th, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. # https://metanit.com/sharp/patterns/4.1.php
  5.  
  6. class Pizza:
  7.     def __init__(self, name: str) -> None:
  8.         self._name = name
  9.  
  10.     @property
  11.     def name(self) -> str:
  12.         return self._name
  13.  
  14.     def get_cost(self) -> int:
  15.         raise NotImplementedError
  16.  
  17.  
  18. class ItalianPizza(Pizza):
  19.     def __init__(self) -> None:
  20.         super().__init__('Итальянская пицца')
  21.  
  22.     def get_cost(self) -> int:
  23.         return 10
  24.  
  25.  
  26. class BulgerianPizza(Pizza):
  27.     def __init__(self) -> None:
  28.         super().__init__('Болгарская пицца')
  29.  
  30.     def get_cost(self) -> int:
  31.         return 8
  32.  
  33.  
  34. class PizzaDecorator(Pizza):
  35.     def __init__(self, pizza: Pizza, name: str) -> None:
  36.         super().__init__(name)
  37.         self._pizza = pizza
  38.  
  39.     def get_cost(self) -> int:
  40.         raise NotImplementedError
  41.  
  42.  
  43. class TomatoPizza(PizzaDecorator):
  44.     def __init__(self, pizza: Pizza) -> None:
  45.         super().__init__(pizza, pizza.name + ', с томатами')
  46.  
  47.     def get_cost(self) -> int:
  48.         return self._pizza.get_cost() + 3
  49.  
  50.  
  51. class CheesePizza(PizzaDecorator):
  52.     def __init__(self, pizza: Pizza) -> None:
  53.         super().__init__(pizza, pizza.name + ', с сыром')
  54.  
  55.     def get_cost(self) -> int:
  56.         return self._pizza.get_cost() + 5
  57.  
  58.  
  59. def main():
  60.     pizza1: Pizza = ItalianPizza()
  61.     pizza1 = TomatoPizza(pizza1)  # итальянская пицца с томатами
  62.     print('Название: {0}'.format(pizza1.name))
  63.     print('Цена: {0}'.format(pizza1.get_cost()))
  64.  
  65.     pizza2: Pizza = ItalianPizza()
  66.     pizza2 = CheesePizza(pizza2)  # итальянская пиццы с сыром
  67.     print('Название: {0}'.format(pizza2.name))
  68.     print('Цена: {0}'.format(pizza2.get_cost()))
  69.  
  70.     pizza3: Pizza = BulgerianPizza()
  71.     pizza3 = TomatoPizza(pizza3)  # итальянская пицца с томатами
  72.     pizza3 = CheesePizza(pizza3)  # болгарская пиццы с томатами и сыром
  73.     print('Название: {0}'.format(pizza3.name))
  74.     print('Цена: {0}'.format(pizza3.get_cost()))
  75.  
  76.     input()
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement