-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuilder_naive.py
115 lines (87 loc) · 2.1 KB
/
builder_naive.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Author: CHIRAG SHAH
Created On: 18th September 2018
Modified On: 14th October 2018
"""
import inspect, sys
import matplotlib.pyplot as plt
from pathlib import Path
from abc import ABCMeta, abstractmethod
sys.path.append(str(Path(__file__).resolve().parent.parent))
from utility import class_diagram, output_image
class Director:
"""
This class constructs an object using the Builder Interface
"""
def __init__(self):
self.build = None
def construct(self, builder):
self.build = builder
self.build.build_a()
self.build.build_b()
self.build.build_c()
class Builder(metaclass = ABCMeta):
"""
This class specifies abstract interface for creating parts of builder object
"""
def __init__(self):
self.product = Product()
@abstractmethod
def build_a(self):
pass
@abstractmethod
def build_b(self):
pass
@abstractmethod
def build_c(self):
pass
class ConcreteBuilder(Builder):
"""
Construct and assemble parts of the product by implementing the Builder interface.
Define and keep track of the representation it creates.
Provide an interface for retrieving the product.
"""
def build_a(self):
print('Building A..')
def build_b(self):
print('Building B..')
def build_c(self):
print('Building C..')
class Product:
"""
Represent object under construction
"""
pass
def test_builder():
"""
Demonstration of builder pattern
"""
concrete_builder = ConcreteBuilder()
director = Director()
director.construct(concrete_builder)
product = concrete_builder.product
def get_code():
"""
@return-values: source code
"""
a = inspect.getsource(Director)
b = inspect.getsource(Builder)
c = inspect.getsource(ConcreteBuilder)
d = inspect.getsource(Product)
e = inspect.getsource(test_builder)
return a + '\n' + b + '\n' + c + '\n' + d + '\n' + e
def get_classdiagram():
"""
@return-values: matplotlib object with class diagram
"""
diagram = class_diagram("builder.png")
plt.show()
return diagram
def get_outputimage():
"""
@return-values: matplotlib object with code output
"""
output = output_image("builder_naive.png")
plt.show()
return output
test_builder()