-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.py
52 lines (41 loc) · 1.06 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
from abc import ABC, abstractmethod
# Product interface
class Transport(ABC):
@abstractmethod
def deliver(self):
pass
# Concrete ProductA
class Truck(Transport):
def deliver(self):
return "Delivering by truck"
# Concrete ProductB
class Ship(Transport):
def deliver(self):
return "Delivering by ship"
# Creator
class Logistic(ABC):
@abstractmethod
def create_transport(self):
pass
def plan_delivery(self):
transport = self.create_transport()
return f"Planning delivery using {transport.deliver()}"
# Concrete CreatorA
class RoadLogistic(Logistic):
def create_transport(self):
return Truck()
# Concrete CreatorB
class SeaLogistic(Logistic):
def create_transport(self):
return Ship()
# Client code
def client_code(logistic):
print(logistic.plan_delivery())
# Usage
road_logistic = RoadLogistic()
sea_logistic = SeaLogistic()
client_code(road_logistic)
client_code(sea_logistic)
## Output
# Planning delivery using Delivering by truck
# Planning delivery using Delivering by ship