PHP观察者模式实例分析【对比JS观察者模式】
程序员文章站
2022-06-16 18:46:05
本文实例讲述了php观察者模式。分享给大家供大家参考,具体如下:
1.用js实现观察者模式
本文实例讲述了php观察者模式。分享给大家供大家参考,具体如下:
1.用js实现观察者模式
<!doctype html> <html> <head> <title></title> <style type="text/css"> div{width: 100px;height: 100px;border: 1px #999 solid;margin-bottom: 5px;} </style> </head> <body> <!-- 我们让div对象观察select的变化,selecte变化就会通知这个2个对象,并引起这2个对象的变化,实现观察者模式。 --> <h1>用观察者模式切换页面风格</h1> <select> <option value="male">男式风格</option> <option value="female">女士风格</option> </select> <button onclick="t1()">观察学习区</button> <button onclick="t2()">不观察学习区</button> <div id="content">我是内容</div> <div id="ad">我是广告</div> <div id="study">学习</div> </body> <script type="text/javascript"> var sel = document.getelementsbytagname('select')[0]; sel.observers = {}; sel.attach = function(key,obj){ this.observers[key] = obj; } sel.detach = function(key){ delete this.observers[key]; } sel.onchange = sel.notify = function(){ for(var key in this.observers){ this.observers[key].update(this); } } //客户端 var content = document.getelementbyid('content'); var ad = document.getelementbyid('ad'); content.update = function(ob){ if (ob.value == 'male') { this.style.backgroundcolor = 'gray'; }else if(ob.value == 'female'){ this.style.backgroundcolor = 'pink'; } } ad.update = function(ob){ if (ob.value == 'male') { this.innerhtml = '汽车'; }else if(ob.value == 'female'){ this.innerhtml = '减肥'; } } //让content观察select的变化 sel.attach('content',content); sel.attach('ad',ad); //新增监听study区 var study = document.getelementbyid('study'); study.update = function(ob){ if (ob.value == 'male') { this.innerhtml = '学习计算机'; }else if(ob.value == 'female'){ this.innerhtml = '学习美容'; } } sel.attach('study',study); function t1(){ sel.attach('study',study); } function t2(){ sel.detach('study'); } </script> </html>
2.用php实现观察模式
<?php //php实现观察者 //php5中提供观察者observer和被观察者subject的接口 class user implements splsubject { public $lognum; public $hobby; protected $observers = null; public function __construct($hobby) { $this->lognum = rand(1,10); $this->hobby = $hobby; $this->observers = new splobjectstorage(); } public function login() { //操作session等 $this->notify(); } public function attach(splobserver $observer) { $this->observers->attach($observer); } public function detach(splobserver $observer) { $this->observers->detach($observer); } public function notify() { $this->observers->rewind(); while ($this->observers->valid()) { $observer = $this->observers->current(); $observer->update($this); $this->observers->next(); } } } //用户安全登录模块 class safe implements splobserver { public function update(splsubject $subject) { if ($subject->lognum < 3) { echo '这是第' . $subject->lognum . '次安全登录<br>'; }else{ echo '这是第' . $subject->lognum . '次登录,异常<br>'; } } } //广告模块 class ad implements splobserver { public function update(splsubject $subject) { if ($subject->hobby == 'sports') { echo '英超开始啦<br>'; }else{ echo '好好学习<br>'; } } } //实施观察 // $user = new user('sports'); $user = new user('study'); $user->attach(new safe()); $user->attach(new ad()); $user->login();//登录
更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
上一篇: 浅析C# 结构体struct