Lynda 假设有一个系统服务,有几个模块需要使用服务,组件可以订阅这个服务或消息,通过系统广播通知所有的模块。 无 ?php// 订阅发布模式 接口的定义及消息的传递class Dispatcher{ // 监视public static $listeners = array();protected function __constru
Lynda
假设有一个系统服务,有几个模块需要使用服务,组件可以订阅这个服务或消息,通过系统广播通知所有的模块。
<?php // 订阅发布模式 接口的定义及消息的传递 class Dispatcher { // 监视 public static $listeners = array(); protected function __construct(){} // 订阅服务 public static function subscribe($object, $subscribe) { $id = spl_object_hash($object); self::$listeners[$id][] = $subscribe; } // 广播消息 public static function publish($object) { $id = spl_object_hash($object); foreach (self::$listeners[$id] as $subscriber) { //var_dump($subscriber); $subscriber->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.
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
刺客信條陰影:貝殼謎語解決方案
4 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
4 週前ByDDD
<🎜>:死鐵路 - 如何完成所有挑戰
1 個月前ByDDD
Atomfall指南:項目位置,任務指南和技巧
1 個月前ByDDD

熱工具

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。