-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.py
38 lines (30 loc) · 1.28 KB
/
adapter.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
"""
An adapter is a design pattern that allows you to modify or extend the behavior of a
class or a function without modifying its structure.
"""
from django.core.mail import send_mail
#########################################################################################
# Function Adapter
def send_email(subject, message, recipient_list):
send_mail(subject, message, '[email protected]', recipient_list)
def send_email_with_logging(subject, message, recipient_list):
"""
It is a send_email function's adapter, which can run some logic after or before send_email runs.
"""
print('before')
send_email(subject, message, recipient_list)
print('after')
#########################################################################################
# Class Adapter
class EmailService:
@staticmethod
def send_email(subject, message, recipient_list):
send_mail(subject, message, '[email protected]', recipient_list)
class EmailServiceAdapter(EmailService):
def send_email_with_logging(self, subject, message, recipient_list):
"""
It is a send_email function's adapter, which can run some logic after or before send_email runs.
"""
print('before')
self.send_email(subject, message, recipient_list)
print('after')