Advertisement
horozov86

Email - Solid

Jul 17th, 2023 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2.  
  3. class IContent(ABC):
  4.     def __init__(self, text):
  5.         self.text = text
  6.        
  7.     @abstractmethod
  8.     def format(self):
  9.         pass
  10.    
  11. class MyContent(IContent):
  12.     def format(self):
  13.         return ''.join(['<MyML>', self.text, '</MyML>'])
  14.        
  15. class IProtocol(ABC):
  16.     def __init__(self, text):
  17.         self.text = text
  18.        
  19.     @abstractmethod
  20.     def format(self):
  21.         pass
  22.    
  23. class MyProtocol(IProtocol):
  24.     def format(self):
  25.         return ''.join(["I'm" , self.text])
  26.    
  27.  
  28. class IEmail(ABC):
  29.    
  30.     @abstractmethod
  31.     def set_sender(self, sender):
  32.         pass
  33.    
  34.     @abstractmethod
  35.     def set_receiver(self, receiver):
  36.         pass
  37.    
  38.     @abstractmethod
  39.     def set_content(self, content):
  40.         pass
  41.    
  42. class Email(IEmail):
  43.     def __init__(self, protocol):
  44.         self.protocol = protocol
  45.         self.__sender = None
  46.         self.__receiver = None
  47.         self.__content = None
  48.        
  49.     def set_sender(self, sender):
  50.         self.__sender = sender.format()
  51.            
  52.     def set_receiver(self, receiver):
  53.         self.__receiver = receiver.format()
  54.            
  55.     def set_content(self, content):
  56.         self.__content = content.format()
  57.            
  58.     def __repr__(self):
  59.        
  60.         template = "Sender: {sender}\nReceiver: {receiver}\nContent: \n{content}"
  61.        
  62.         return template.format(sender = self.__sender, receiver = self.__receiver, content = self.__content)
  63.        
  64. email = Email('IM')
  65. email.set_sender('qmal')
  66. email.set_receiver('james')
  67. content = MyContent('Hello, there!')
  68. email.set_content(content)
  69. print(email)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement