Evgenii Legotckoi
Қыр. 18, 2018, 4:06 Т.Қ.

Мысал - Python тіліндегі шаблонды құрастырушы

Пример шаблон проектирования Строитель на Python.


  1. """
  2. Separate the construction of a complex object from its representation so
  3. that the same construction process can create different representations.
  4. """
  5.  
  6. import abc
  7.  
  8.  
  9. class Director:
  10. """
  11. Construct an object using the Builder interface.
  12. """
  13.  
  14. def __init__(self):
  15. self._builder = None
  16.  
  17. def construct(self, builder):
  18. self._builder = builder
  19. self._builder._build_part_a()
  20. self._builder._build_part_b()
  21. self._builder._build_part_c()
  22.  
  23.  
  24. class Builder(metaclass=abc.ABCMeta):
  25. """
  26. Specify an abstract interface for creating parts of a Product
  27. object.
  28. """
  29.  
  30. def __init__(self):
  31. self.product = Product()
  32.  
  33. @abc.abstractmethod
  34. def _build_part_a(self):
  35. pass
  36.  
  37. @abc.abstractmethod
  38. def _build_part_b(self):
  39. pass
  40.  
  41. @abc.abstractmethod
  42. def _build_part_c(self):
  43. pass
  44.  
  45.  
  46. class ConcreteBuilder(Builder):
  47. """
  48. Construct and assemble parts of the product by implementing the
  49. Builder interface.
  50. Define and keep track of the representation it creates.
  51. Provide an interface for retrieving the product.
  52. """
  53.  
  54. def _build_part_a(self):
  55. pass
  56.  
  57. def _build_part_b(self):
  58. pass
  59.  
  60. def _build_part_c(self):
  61. pass
  62.  
  63.  
  64. class Product:
  65. """
  66. Represent the complex object under construction.
  67. """
  68.  
  69. pass
  70.  
  71.  
  72. def main():
  73. concrete_builder = ConcreteBuilder()
  74. director = Director()
  75. director.construct(concrete_builder)
  76. product = concrete_builder.product
  77.  
  78.  
  79. if __name__ == "__main__":
  80. main()

Ол саған ұнайды ма? Әлеуметтік желілерде бөлісіңіз!

Пікірлер

Тек рұқсаты бар пайдаланушылар ғана пікір қалдыра алады.
Кіріңіз немесе Тіркеліңіз