-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
builder_concept.py
75 lines (54 loc) · 1.42 KB
/
builder_concept.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
# pylint: disable=too-few-public-methods
# pylint: disable=arguments-differ
"Builder Concept Sample Code"
from abc import ABCMeta, abstractmethod
class IBuilder(metaclass=ABCMeta):
"The Builder Interface"
@staticmethod
@abstractmethod
def build_part_a():
"Build part a"
@staticmethod
@abstractmethod
def build_part_b():
"Build part b"
@staticmethod
@abstractmethod
def build_part_c():
"Build part c"
@staticmethod
@abstractmethod
def get_result():
"Return the final product"
class Builder(IBuilder):
"The Concrete Builder."
def __init__(self):
self.product = Product()
def build_part_a(self):
self.product.parts.append('a')
return self
def build_part_b(self):
self.product.parts.append('b')
return self
def build_part_c(self):
self.product.parts.append('c')
return self
def get_result(self):
return self.product
class Product():
"The Product"
def __init__(self):
self.parts = []
class Director:
"The Director, building a complex representation."
@staticmethod
def construct():
"Constructs and returns the final product"
return Builder()\
.build_part_a()\
.build_part_b()\
.build_part_c()\
.get_result()
# The Client
PRODUCT = Director.construct()
print(PRODUCT.parts)