-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactoryMethodPattern.py
More file actions
58 lines (45 loc) · 1.57 KB
/
Copy pathFactoryMethodPattern.py
File metadata and controls
58 lines (45 loc) · 1.57 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
"""
Factory Method Pattern - A pattern which uses sub classes to generate an
object. This pattern can be considered a more advanced version of the
Simple Factory Pattern but it delegates the creation of components to
the subclasses
Example - The Super Administrator is considered the Creator in the application
He/she is able to create an administrator account, an editor account or just
a normal user account
In this example I use npm:prompt to get user input
"""
class SuperAdministrator(object):
def createUser(self):
return
class AdminCreator(SuperAdministrator):
def createUser(self):
return Admin()
class EditorCreator(SuperAdministrator):
def createUser(self):
return Editor()
class UserCreator(SuperAdministrator):
def createUser(self):
return User()
class User(object):
def __init__(self, privileges=None):
# Default CanView
self.privileges = ["CanView"];
if privileges:
self.privileges.extend(privileges)
def getPrivileges(self):
return self.privileges
class Admin(User):
def __init__(self):
super(Admin, self).__init__(["CanCreate", "CanEdit"])
class Editor(User):
def __init__(self):
super(Editor, self).__init__(["CanEdit"])
userType = raw_input("What kind of user do you wish to create? ['admin', 'editor', 'normal']")
if userType == "admin":
userFactory = AdminCreator()
elif userType == "editor":
userFactory = EditorCreator()
else:
userFactory = UserCreator()
user = userFactory.createUser()
print user.getPrivileges()