-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxypattern.py
79 lines (57 loc) · 1.95 KB
/
proxypattern.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
from abc import ABC, abstractmethod
class EmployeeDAO(ABC):
try:
@abstractmethod
def create(self, client, employeeObj):
pass
@abstractmethod
def delete(self, client, employeeId):
pass
@abstractmethod
def get(self, client, employeeId):
pass
except Exception as e:
print(e)
class EmployeeDAOImpl(EmployeeDAO):
def create(self, client, employeeObj):
# creates a new row
print('created a new row in employee table')
def delete(self, client, employeeId):
# delete a row
print('deleted a row with employee id: ', employeeId)
def get(self, client, employeeId):
# fetch row
print('fetching data from the DB')
return EmployeeDO()
class EmployeeDAOProxy(EmployeeDAO):
employeeDAOObj = None
def __init__(self):
self.employeeDAOObj = EmployeeDAOImpl()
def create(self, client, employeeObj):
if client == 'ADMIN':
self.employeeDAOObj.create(client, employeeObj)
return
raise Exception('Access Denied!')
def delete(self, client, employeeId):
if client == 'ADMIN':
self.employeeDAOObj.delete(client, employeeId)
return
raise Exception('Access Denied!')
def get(self, client, employeeId):
if client == 'ADMIN' or client == 'USER':
return self.employeeDAOObj.get(client, employeeId)
raise Exception('Access Denied!')
class EmployeeDO():
name = None
id = None
def __init__(self, name, id):
self.name = name
self.id = id
class ProxyDesignPattern:
try:
employeeTableObj = EmployeeDAOProxy()
employeeTableObj.create('USER', EmployeeDO('John', '23'))
# employeeTableObj.delete('ADMIN', EmployeeDO('Doe', '54').id)
print('operation executed successfully')
except Exception as e:
print(e)