lynda
假设有一个系统服务,有几个模块需要使用服务,组件可以订阅这个服务或消息,通过系统广播通知所有的模块。 dosomething(); } }}class service{ protected $name = ''; public function __construct($name) { $this->name = $name; } // 触发动作 public function dosomething() { echo sprintf(%s has something happened.\n, $this->name); dispatcher::publish($this); }}class component{ protected $name = ''; public function __construct($name) { $this->name = $name; } public function dosomething() { echo sprintf(%s did something.\n, $this->name); }}$servicea = new service(servicea);$componenta = new component(componenta);$componentb = new component(componentb);$componentc = new component(componentc);dispatcher::subscribe($servicea, $componenta);dispatcher::subscribe($servicea, $componentb);dispatcher::subscribe($servicea, $componentc);// 触发一个动作$servicea->dosomething();// output//servicea something happened.//componenta did something.//componentb did something.//componentc did something.
