-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.py
63 lines (40 loc) · 1.17 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
from abc import ABC, abstractmethod
class Knife(ABC): # Product
@abstractmethod
def get_name(self) -> str:
pass
@abstractmethod
def test(self) -> None:
pass
def pack(self) -> None:
print(self.get_name(), ": pack()")
class ChefKnife(Knife): # Concrete product
def get_name(self) -> str:
self.name = "Chef Knife"
return self.name
def test(self) -> None:
print(self.get_name(), ": test()")
class BreadKnife(Knife): # Concrete product
def get_name(self) -> str:
self.name = "Bread Knife"
return self.name
def test(self) -> None:
print(self.get_name(), ": test()")
class KnifeFactory(ABC): # creator
def make_product(self, product_name) -> Knife:
product = None
if product_name == 'BreadKnife':
product = BreadKnife()
elif product_name == 'ChefKnife':
product = ChefKnife()
else:
return product
return product
class KnifeStore(KnifeFactory): # Concrete creator
def order_product(self, product_name) -> Knife:
product = self.make_product(product_name)
product.test()
product.pack()
return product
product1 = KnifeStore().order_product('BreadKnife')
product2 = KnifeStore().order_product('ChefKnife')