注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
楼主感觉最近算法已经趋近通透,但是系统设计和OOD始终摸不到诀窍(上周跟面试官聊rate limiter聊到血崩)。决心攻坚。今后会每天坚持分享一道自己写的系统设计/OOD直到找到工作,并把答案放在这里面。求和大家分享讨论。
Design a call center
- """
- call_center
- composition:
- employee: opeartor, supervisor, directo
- logic:
- first: check operator : satisfy importance and available
- \
- V
- second: check supervisor
- \
- V
- third: check director
- \
- V
- going to waiting queue
- employee:
- logic:
- get_call, free
- get(): Success, fail
- free(): sucess, free
- call:
- logic: rank, state
- run()
- finished()
- """
- class Rank(Enum):
- OPERATOR = 0
- SUPERVISOR = 1
- DIRECTOR = 2
- class Employee(object):
- def __init__(self, employee_id, name, rank:Rank, call_center):
- self.employee_id = employee_id
- self.name = name
- self.rank = rank
- self.call_center = call_center
- self.call = None
- def take_call(self, call):
- self.call = call
- call.setCaller(self)
- call.run()
- def complete_call(self):
- # call.update_employee_finished(self)
- # self.
- self.call = None
- self.call_center.recheck()
- @abstractmethod
- def usable(self, rank):
- pass
- class Opeartor(Employee):
- def __init__(self, employee_id, name, call_center):
- super(Operator, self).__init__(...,...,Rank.OPERATOR, call_center)
- def usable(self, rank):
- return True if self.call == None
-
- class ...
- calss ...
- class CallState(Enum):
- READY = 0
- IN_PROGRESS = 1
- FINISHED = 1
- class Call(object):
- def __init__(self, rank):
- self.rank = rank
- self.state = CallState.READY
- self.employee = None
- def set_caller(self, employee):
- sefl.employee = employee
- def run(self):
- self.state = CallState.IN_PROGRESS
- pass
- def finished(self):
- self.state = FINISHED
- self.employee.complete_call()
- class CallCenter(object):
- def __init__(self, opeartors, supervisors, directors):
- self....
- self....
- self....
- self.waitingqueue = []
- self.lock = Threading.lock()
- def add_call(self, call):
- with self.lock:
- employee = None
- if call.rank == RANK.OPERATOR:
- employee = self._dispatch(call, self.operators)
- if call.rank == RANK.... and not employee:
- if ...
- if not employee:
- self.waitingqueue.append(call)
- def _dispatch(self, call, employees):
- for employee in employees:
- if employee.call is None:
- employee.take_call(call)
- def recheck(self, employee):
- with self.lock:
- for waiting_call in waitingqueue:
- if employee.usable(waiting_call.rank):
- employee.take_call(waiting_call)
- break
复制代码
|