Last active
December 15, 2020 18:22
-
-
Save FirstShanti/a5a98648cf40f2076696a50e25158ade to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from abc import ABC, abstractmethod | |
from typing import List | |
""" | |
Define a family of algorithms, encapsulate each one, and make them | |
interchangeable. Strategy lets the algorithm vary independently from | |
clients that use it. | |
""" | |
class Context: | |
""" | |
Define the interface of interest to clients. | |
Maintain a reference to a Strategy object. | |
""" | |
def __init__(self): | |
pass | |
@property | |
def strategy(self) -> Strategy: | |
""" | |
The context was stored link on Strategy instance. | |
Context doesn't know the specific class of Strategy. | |
He must work with all strategies to throw the Strategy interface. | |
""" | |
return self._strategy | |
@strategy.setter | |
def strategy(self, strategy: Strategy) -> None: | |
""" | |
Typically, the Context allows you to replace the Strategy object at runtime. | |
""" | |
self._strategy = strategy | |
def context_interface(self, data): | |
self._strategy.algorithm_interface(data) | |
class Strategy(ABC): | |
""" | |
Declare an interface common to all supported algorithms. | |
Context uses this interface to call the algorithm defined | |
by a <ConcreteStrategy>. | |
""" | |
@abstractmethod | |
def algorithm_interface(self): | |
pass | |
""" | |
Implemented a strategy pattern for | |
user notification based on the available methods. | |
""" | |
class Notification(Context): | |
""" | |
Implement the context using the Context interface. | |
""" | |
pass | |
class Send_email(Strategy): | |
""" | |
Implement the algorithm using the Strategy interface. | |
""" | |
def algorithm_interface(self, data): | |
""" | |
Implement validation, connect to smtp and send email | |
""" | |
print(f'send email to {data}') | |
class Send_sms(Strategy): | |
""" | |
Implement the algorithm using the Strategy interface. | |
""" | |
def algorithm_interface(self, data): | |
""" | |
Implement validation, connect to API and send sms | |
""" | |
print(f'send sms to {data}') | |
class Make_call(Strategy): | |
""" | |
Implement the algorithm using the Strategy interface. | |
""" | |
def algorithm_interface(self, data): | |
""" | |
Implement validation, connect to API and make a call | |
""" | |
print(f'make call to {data}') | |
class User: | |
def __init__(self, *args, **kwargs): | |
self.__dict__ = kwargs | |
def send_notification(self): | |
notification = Notification() | |
if self.notification == 'email': | |
notification.strategy = Send_email() | |
data = self.email | |
elif self.notification == 'sms': | |
notification.strategy = Send_sms() | |
data = self.phonenumber | |
elif self.notification == 'call': | |
notification.strategy = Make_call() | |
data = self.phonenumber | |
notification.context_interface(data) | |
def main(): | |
user1 = User( | |
firstname='Ted', | |
secondname='Chan', | |
email='[email protected]', | |
notification='email') | |
user2 = User( | |
firstname='Ray', | |
secondname='Bradbury', | |
phonenumber='123456789', | |
notification='sms') | |
user3 = User( | |
firstname='Gary', | |
secondname='Garrison', | |
phonenumber='0000000', | |
notification='call') | |
user1.send_notification() | |
user2.send_notification() | |
user3.send_notification() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment