-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfactorymethod_naive.py
132 lines (99 loc) · 3.12 KB
/
factorymethod_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Author: CHIRAG SHAH
Created On: 19th July 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 AbstractCreator(metaclass = ABCMeta):
"""
Abstract base class with __init__ as the abstract method
Used for inheritance by different types of ConcreteCreators
@return-values: name of creator
"""
def __init__(self):
self.product = self.factory_method()
@abstractmethod
def factory_method(self):
pass
#Logging functions
def log(self):
#self.product.interface()
pass
class ConcreteCreatorA(AbstractCreator):
"""
Class used for customising the type of creator needed by client / provided by store to client
Here we use the abstract method __init__ to set the concretecreator attributes
@params: class to inherit
"""
def factory_method(self):
return ConcreteProductA()
class ConcreteCreatorB(AbstractCreator):
"""
Class used for customising the type of creator needed by client / provided by store to client
Here we use the abstract method __init__ to set the concretecreator attributes
@params: class to inherit
"""
def factory_method(self):
return ConcreteProductB()
class AbstractProduct(metaclass = ABCMeta):
"""
Abstract Interface for creating objects
Here we do not instantiate the object but pass the instantiation to further subclasses
@return-values: Complete product delivered to client
"""
@abstractmethod
def interface(self):
pass
class ConcreteProductA(AbstractProduct):
"""
Subclass of the product factory for instantiating appropriate concretecreator object
Here we use the ideal concept of factorymethod design pattern by allowing the subclass to initialise the class
@return-values: instance of concretecreator
"""
def interface(self):
return "I am in A"
class ConcreteProductB(AbstractProduct):
"""
Subclass of the product factory for instantiating appropriate concretecreator object
Here we use the ideal concept of factorymethod design pattern by allowing the subclass to initialise the class
@return-values: instance of concretecreator
"""
def interface(self):
return "I am in B"
def test_factory():
"""
Demonstration of factorymethod pattern
"""
concretecreator = ConcreteCreatorA()
concretecreator.product.interface()
#concretecreator.log()
def get_code():
"""
@return-values: source code
"""
a = inspect.getsource(AbstractCreator)
b = inspect.getsource(ConcreteCreatorA)
c = inspect.getsource(ConcreteCreatorB)
d = inspect.getsource(AbstractProduct)
e = inspect.getsource(ConcreteProductA)
f = inspect.getsource(ConcreteProductB)
g = inspect.getsource(test_factory)
return a + '\n' + b + '\n' + c + '\n' + d + '\n' + e + '\n' + f + '\n' + g
def get_classdiagram():
"""
@return-values: matplotlib object with class diagram
"""
diagram = class_diagram("factorymethod.png")
#plt.show()
return diagram
def get_outputimage():
"""
@return-values: matplotlib object with code output
"""
output = output_image("factorymethod_naive.png")
#plt.show()
return output