一句话概括:其实核心就是用了回调机制。

 1 class PyBus (object):
 2 
 3     def __init__(self,):
 4         self.clear()
 5         
 6     def clear(self):
 7         self.subscriptions = {}
 8     
 9     def subscribe(self, subject, owner, func):
10         if not self.subscriptions.has_key(owner):
11             self.subscriptions[owner] = {}
12         self.subscriptions[owner][subject] = func
13         
14     def has_subscription(self, owner, subject):
15         return self.subscriptions.has_key(owner) and self.subscriptions[owner].has_key(subject)
16         
17     def publish(self, subject, *args, **kwargs):
18         for owner in self.subscriptions.keys():
19             if self.has_subscription(owner, subject):
20                 self.subscriptions[owner][subject](*args, **kwargs)

相关文章:

  • 2022-12-23
  • 2021-09-15
  • 2021-06-10
  • 2022-12-23
  • 2021-12-23
  • 2021-12-23
猜你喜欢
  • 2021-12-23
  • 2022-01-13
  • 2021-05-17
  • 2021-11-30
  • 2021-12-23
  • 2021-08-15
  • 2022-01-29
相关资源
相似解决方案