本文實(shí)例講述了PHP觀察者模式。分享給大家供大家參考,具體如下:
1.用js實(shí)現(xiàn)觀察者模式
!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個對象的變化,實(shí)現(xiàn)觀察者模式。
-->
h1>用觀察者模式切換頁面風(fēng)格/h1>
select>
option value="male">男式風(fēng)格/option>
option value="female">女士風(fēng)格/option>
/select>
button onclick="t1()">觀察學(xué)習(xí)區(qū)/button>
button onclick="t2()">不觀察學(xué)習(xí)區(qū)/button>
div id="content">我是內(nèi)容/div>
div id="ad">我是廣告/div>
div id="study">學(xué)習(xí)/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);
//新增監(jiān)聽study區(qū)
var study = document.getElementById('study');
study.update = function(ob){
if (ob.value == 'male') {
this.innerHTML = '學(xué)習(xí)計算機(jī)';
}else if(ob.value == 'female'){
this.innerHTML = '學(xué)習(xí)美容';
}
}
sel.attach('study',study);
function t1(){
sel.attach('study',study);
}
function t2(){
sel.detach('study');
}
/script>
/html>
2.用php實(shí)現(xiàn)觀察模式
?php
//php實(shí)現(xiàn)觀察者
//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 '好好學(xué)習(xí)br>';
}
}
}
//實(shí)施觀察
// $user = new User('sports');
$user = new User('study');
$user->attach(new Safe());
$user->attach(new Ad());
$user->login();//登錄
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- PHP設(shè)計模式之觀察者模式入門與應(yīng)用案例詳解
- PHP 觀察者模式深入理解與應(yīng)用分析
- php模式設(shè)計之觀察者模式應(yīng)用實(shí)例分析
- PHP使用觀察者模式處理異常信息的方法詳解
- php設(shè)計模式之觀察者模式定義與用法經(jīng)典示例
- PHP中常用的三種設(shè)計模式詳解【單例模式、工廠模式、觀察者模式】
- PHP設(shè)計模式之觀察者模式定義與用法分析
- PHP觀察者模式定義與用法實(shí)例分析
- 解析PHP觀察者模式Observer