-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.py
85 lines (57 loc) · 2.01 KB
/
factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from abc import ABC, abstractmethod
class Vehicle(ABC):
def __init__(self, name: str):
self.name = name
@abstractmethod
def drive(self):
pass
class Car(Vehicle):
def drive(self):
return f"{self.name} is driving on the road."
def spez(self):
return f"{self.name} special ability"
class Ship(Vehicle):
def drive(self):
return f"{self.name} is sailing on the water."
class Plane(Vehicle):
def drive(self):
return f"{self.name} is flying in the sky."
class Jet(Vehicle):
def drive(self):
return f"{self.name} is flying very very fast in the sky."
def spez(self):
return f"{self.name} makes a looping."
class Garage:
_vehicles: list[Vehicle] = []
def deliverVehicleToGarage(self, vehicle: Vehicle):
self._vehicles.append(vehicle)
def deleteVehicleFromGarage(self, vehicle: Vehicle):
self._vehicles.remove(vehicle)
def printGarage(self):
for vehicle in self._vehicles:
print(vehicle.drive())
if hasattr(vehicle, "spez"):
print(vehicle.spez())
class VehicleFactory:
_legitTypes = [Car, Ship, Plane, Jet]
_deliverTo: Garage
def __init__(self, garage: Garage) -> None:
self._deliverTo = garage
def setDelivery(self, garage: Garage):
self._deliverTo = garage
def createVehicle(self, typ: type, name: str) -> Vehicle:
if typ in self._legitTypes:
vehicle = typ(name)
self._deliverTo.deliverVehicleToGarage(vehicle)
return vehicle
else:
raise NotImplementedError(f"Unknown vehicle: {typ}")
if __name__ == "__main__":
myGarage = Garage()
factory = VehicleFactory(myGarage)
myCar = factory.createVehicle(Car, "Volkswagen Golf")
MyShip = factory.createVehicle(Ship, "Big yacht")
MyPlane = factory.createVehicle(Plane, "Boeing 747")
MyJet = factory.createVehicle(Jet, "F-12")
myGarage.deleteVehicleFromGarage(MyShip)
myGarage.printGarage()