-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07_decorator.py
51 lines (35 loc) · 1.04 KB
/
07_decorator.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
"""
Decorator
- a structural pattern that allows adding new behaviors to objects dynamically
by placing them inside special wrapper objects, called decorators.
"""
import abc
class Page(abc.ABC): # Abstract Component
@abc.abstractmethod
def show(self):
pass
class AuthPage(Page): # Concrete Component 1
def show(self):
print('Welcome to authenticated page')
class AnonPage(Page): # Concrete Component 2
def show(self):
print('Welcome to anonymous page')
class PageDecorator(Page, abc.ABC): # Abstract Decorator
def __init__(self, component):
self._component = component
@abc.abstractmethod
def show(self):
pass
class PageAuthDecorator(PageDecorator): # Concrete Decorator
def show(self):
username = input('Enter your username... ')
password = input('Enter your password... ')
if username == 'admin' and password == 'secret':
self._component.show()
else:
print('you are not authenticated')
def client_decorator():
page = AuthPage()
authenticated = PageAuthDecorator(page)
authenticated.show()
client_decorator()