Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- # Wzorzec Budowniczy
- class House:
- def __init__(self):
- self.walls = None
- self.doors = None
- self.windows = None
- self.roof = None
- self.material = None
- def __str__(self):
- return f"House built with {self.material} has {self.walls} walls, {self.doors} doors, {self.windows} windows, and a {self.roof} roof."
- class HouseBuilder:
- def __init__(self):
- self.house = House()
- def build_walls(self, walls):
- self.house.walls = walls
- def build_doors(self, doors):
- self.house.doors = doors
- def build_windows(self, windows):
- self.house.windows = windows
- def build_roof(self, roof):
- self.house.roof = roof
- def set_material(self, material):
- self.house.material = material
- def get_house(self):
- return self.house
- class WoodenHouseBuilder(HouseBuilder):
- def build(self):
- self.build_walls("wooden walls")
- self.build_doors("wooden doors")
- self.build_windows("wooden windows")
- self.build_roof("wooden roof")
- self.set_material("wood")
- class BrickHouseBuilder(HouseBuilder):
- def build(self):
- self.build_walls("brick walls")
- self.build_doors("wooden doors")
- self.build_windows("glass windows")
- self.build_roof("tile roof")
- self.set_material("brick")
- class Director:
- def __init__(self, builder):
- self.builder = builder
- def construct(self):
- self.builder.build()
- # Wzorzec Prototyp
- class HousePrototype:
- def clone(self):
- return copy.deepcopy(self)
- # Integracja wzorca Prototyp z klasą House
- class PrototypeHouse(House, HousePrototype):
- pass
- # Testowanie aplikacji
- def main():
- # Tworzenie domów za pomocą wzorca Budowniczy
- wooden_builder = WoodenHouseBuilder()
- brick_builder = BrickHouseBuilder()
- director = Director(wooden_builder)
- director.construct()
- wooden_house = wooden_builder.get_house()
- print(wooden_house)
- director = Director(brick_builder)
- director.construct()
- brick_house = brick_builder.get_house()
- print(brick_house)
- # Tworzenie prototypów domów
- prototype_house = PrototypeHouse()
- prototype_house.walls = "concrete walls"
- prototype_house.doors = "metal doors"
- prototype_house.windows = "plastic windows"
- prototype_house.roof = "concrete roof"
- prototype_house.material = "concrete"
- # Klonowanie domu z prototypu
- cloned_house = prototype_house.clone()
- print(cloned_house)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement