Advertisement
go6odn28

solid

Aug 3rd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2. import time
  3.  
  4.  
  5. class Eatable(ABC):
  6.  
  7.     @abstractmethod
  8.     def eat(self):
  9.         pass
  10.  
  11.  
  12. class Workable(ABC):
  13.  
  14.     @abstractmethod
  15.     def work(self):
  16.         pass
  17.  
  18.  
  19. class Worker(Workable, Eatable):
  20.  
  21.     def work(self):
  22.         print("I'm normal worker. I'm working.")
  23.  
  24.     def eat(self):
  25.         print("Lunch break....(5 secs)")
  26.  
  27.  
  28. class SuperWorker(Workable, Eatable):
  29.  
  30.     def work(self):
  31.         print("I'm super worker. I work very hard!")
  32.  
  33.     def eat(self):
  34.         print("Lunch break....(3 secs)")
  35.  
  36.  
  37. class BaseManager(ABC):
  38.  
  39.     def __init__(self):
  40.         self.worker = None
  41.  
  42.     @abstractmethod
  43.     def set_worker(self, worker):
  44.         ...
  45.  
  46.  
  47. class WorkManager(BaseManager):
  48.  
  49.     def set_worker(self, worker):
  50.         if isinstance(worker, Workable):
  51.             self.worker = worker
  52.         else:
  53.             raise TypeError(f"`worker` must be of type {Workable}")
  54.  
  55.     def manage(self):
  56.         self.worker.work()
  57.  
  58.  
  59. class BreakManager(BaseManager):
  60.  
  61.     def set_worker(self, worker):
  62.         if isinstance(worker, Eatable):
  63.             self.worker = worker
  64.         else:
  65.             raise TypeError(f"`worker` must be of type {Eatable}")
  66.  
  67.     def lunch_break(self):
  68.         self.worker.eat()
  69.  
  70.  
  71. class Robot(Workable):
  72.  
  73.     def work(self):
  74.         print("I'm a robot. I'm working....")
  75.  
  76.  
  77. work_manager = WorkManager()
  78. break_manager = BreakManager()
  79. work_manager.set_worker(Worker())
  80. break_manager.set_worker(Worker())
  81. work_manager.manage()
  82. break_manager.lunch_break()
  83. work_manager.set_worker(SuperWorker())
  84. break_manager.set_worker(SuperWorker())
  85. work_manager.manage()
  86. break_manager.lunch_break()
  87. work_manager.set_worker(Robot())
  88. work_manager.manage()
  89. try:
  90.     break_manager.set_worker(Robot())
  91.     break_manager.lunch_break()
  92. except:
  93.     pass
  94.  
  95.  
  96.  
  97. """
  98. I'm normal worker. I'm working.
  99. Lunch break....(5 secs)
  100. I'm super worker. I work very hard!
  101. Lunch break....(3 secs)
  102. I'm a robot. I'm working....
  103. """
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement