Advertisement
adolphuZ

Untitled

Jun 5th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import copy
  2.  
  3. # Wzorzec Budowniczy
  4. class House:
  5.     def __init__(self):
  6.         self.walls = None
  7.         self.doors = None
  8.         self.windows = None
  9.         self.roof = None
  10.         self.material = None
  11.  
  12.     def __str__(self):
  13.         return f"House built with {self.material} has {self.walls} walls, {self.doors} doors, {self.windows} windows, and a {self.roof} roof."
  14.  
  15. class HouseBuilder:
  16.     def __init__(self):
  17.         self.house = House()
  18.  
  19.     def build_walls(self, walls):
  20.         self.house.walls = walls
  21.  
  22.     def build_doors(self, doors):
  23.         self.house.doors = doors
  24.  
  25.     def build_windows(self, windows):
  26.         self.house.windows = windows
  27.  
  28.     def build_roof(self, roof):
  29.         self.house.roof = roof
  30.  
  31.     def set_material(self, material):
  32.         self.house.material = material
  33.  
  34.     def get_house(self):
  35.         return self.house
  36.  
  37. class WoodenHouseBuilder(HouseBuilder):
  38.     def build(self):
  39.         self.build_walls("wooden walls")
  40.         self.build_doors("wooden doors")
  41.         self.build_windows("wooden windows")
  42.         self.build_roof("wooden roof")
  43.         self.set_material("wood")
  44.  
  45. class BrickHouseBuilder(HouseBuilder):
  46.     def build(self):
  47.         self.build_walls("brick walls")
  48.         self.build_doors("wooden doors")
  49.         self.build_windows("glass windows")
  50.         self.build_roof("tile roof")
  51.         self.set_material("brick")
  52.  
  53. class Director:
  54.     def __init__(self, builder):
  55.         self.builder = builder
  56.  
  57.     def construct(self):
  58.         self.builder.build()
  59.  
  60. # Wzorzec Prototyp
  61. class HousePrototype:
  62.     def clone(self):
  63.         return copy.deepcopy(self)
  64.  
  65. # Integracja wzorca Prototyp z klasą House
  66. class PrototypeHouse(House, HousePrototype):
  67.     pass
  68.  
  69. # Testowanie aplikacji
  70. def main():
  71.     # Tworzenie domów za pomocą wzorca Budowniczy
  72.     wooden_builder = WoodenHouseBuilder()
  73.     brick_builder = BrickHouseBuilder()
  74.  
  75.     director = Director(wooden_builder)
  76.     director.construct()
  77.     wooden_house = wooden_builder.get_house()
  78.     print(wooden_house)
  79.  
  80.     director = Director(brick_builder)
  81.     director.construct()
  82.     brick_house = brick_builder.get_house()
  83.     print(brick_house)
  84.  
  85.     # Tworzenie prototypów domów
  86.     prototype_house = PrototypeHouse()
  87.     prototype_house.walls = "concrete walls"
  88.     prototype_house.doors = "metal doors"
  89.     prototype_house.windows = "plastic windows"
  90.     prototype_house.roof = "concrete roof"
  91.     prototype_house.material = "concrete"
  92.  
  93.     # Klonowanie domu z prototypu
  94.     cloned_house = prototype_house.clone()
  95.     print(cloned_house)
  96.  
  97. if __name__ == "__main__":
  98.     main()
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement