Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from abc import ABC, abstractmethod
- import time
- class Eatable(ABC):
- @abstractmethod
- def eat(self):
- pass
- class Workable(ABC):
- @abstractmethod
- def work(self):
- pass
- class Worker(Workable, Eatable):
- def work(self):
- print("I'm normal worker. I'm working.")
- def eat(self):
- print("Lunch break....(5 secs)")
- class SuperWorker(Workable, Eatable):
- def work(self):
- print("I'm super worker. I work very hard!")
- def eat(self):
- print("Lunch break....(3 secs)")
- class BaseManager(ABC):
- def __init__(self):
- self.worker = None
- @abstractmethod
- def set_worker(self, worker):
- ...
- class WorkManager(BaseManager):
- def set_worker(self, worker):
- if isinstance(worker, Workable):
- self.worker = worker
- else:
- raise TypeError(f"`worker` must be of type {Workable}")
- def manage(self):
- self.worker.work()
- class BreakManager(BaseManager):
- def set_worker(self, worker):
- if isinstance(worker, Eatable):
- self.worker = worker
- else:
- raise TypeError(f"`worker` must be of type {Eatable}")
- def lunch_break(self):
- self.worker.eat()
- class Robot(Workable):
- def work(self):
- print("I'm a robot. I'm working....")
- work_manager = WorkManager()
- break_manager = BreakManager()
- work_manager.set_worker(Worker())
- break_manager.set_worker(Worker())
- work_manager.manage()
- break_manager.lunch_break()
- work_manager.set_worker(SuperWorker())
- break_manager.set_worker(SuperWorker())
- work_manager.manage()
- break_manager.lunch_break()
- work_manager.set_worker(Robot())
- work_manager.manage()
- try:
- break_manager.set_worker(Robot())
- break_manager.lunch_break()
- except:
- pass
- """
- I'm normal worker. I'm working.
- Lunch break....(5 secs)
- I'm super worker. I work very hard!
- Lunch break....(3 secs)
- I'm a robot. I'm working....
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement