您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

简单的订阅者模式

2024/7/27 21:16:00发布32次查看
lynda 假设有一个系统服务,有几个模块需要使用服务,组件可以订阅这个服务或消息,通过系统广播通知所有的模块。 无 ?php// 订阅发布模式 接口的定义及消息的传递class dispatcher{ // 监视public static $listeners = array();protected function __constru
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.
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product