-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfactory_method.py
More file actions
76 lines (54 loc) · 1.72 KB
/
factory_method.py
File metadata and controls
76 lines (54 loc) · 1.72 KB
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
"""
Learn how to create simple factory which helps to hide
logic of creating objects.
"""
from abc import ABCMeta, abstractmethod
class AbstractDegree(metaclass=ABCMeta):
@abstractmethod
def info(self):
pass
class BE(AbstractDegree):
def info(self):
print("Bachelor of engineering")
def __str__(self):
return "Bachelor of engineering"
class ME(AbstractDegree):
def info(self):
print("Master of engineering")
def __str__(self):
return "Master of engineering"
class MBA(AbstractDegree):
def info(self):
print("Master of business administration")
def __str__(self):
return "Master of business administration"
class ProfileAbstractFactory(object):
def __init__(self):
self._degrees = []
self.createProfile()
@abstractmethod
def createProfile(self):
pass
def addDegree(self, degree):
self._degrees.append(degree)
def getDegrees(self):
return self._degrees
class ManagerFactory(ProfileAbstractFactory):
def createProfile(self):
self.addDegree(BE())
self.addDegree(MBA())
class EngineerFactory(ProfileAbstractFactory):
def createProfile(self):
self.addDegree(BE())
self.addDegree(ME())
class ProfileCreatorFactory(object):
@classmethod
def create_profile(self, name):
return eval(profile_type + 'Factory')()
if __name__ == '__main__':
profile_type = input("Which Profile would you like to create? Manager/Engineer - ")
profile = ProfileCreatorFactory.create_profile(profile_type)
print("Creating Profile of ", profile_type)
print("Profile has following degrees -")
for deg in profile.getDegrees():
print('- ', deg)