php 订阅功能,简单的订阅者模式

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.

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值