這篇文章給大家介紹怎么在PHP中應(yīng)用觀察者模式,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

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對(duì)象觀察select的變化,selecte變化就會(huì)通知這個(gè)2個(gè)對(duì)象,并引起這2個(gè)對(duì)象的變化,實(shí)現(xiàn)觀察者模式。
-->
<h2>用觀察者模式切換頁(yè)面風(fēng)格</h2>
<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);
}
}
//客戶(hù)端
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 = '汽車(chē)';
}else if(ob.value == 'female'){
this.innerHTML = '減肥';
}
}
//讓content觀察select的變化
sel.attach('content',content);
sel.attach('ad',ad);
//新增監(jiān)聽(tīng)study區(qū)
var study = document.getElementById('study');
study.update = function(ob){
if (ob.value == 'male') {
this.innerHTML = '學(xué)習(xí)計(jì)算機(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();
}
}
}
//用戶(hù)安全登錄模塊
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 '英超開(kāi)始啦<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中應(yīng)用觀察者模式就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站欄目:怎么在PHP中應(yīng)用觀察者模式-創(chuàng)新互聯(lián)
文章位置:http://www.chinadenli.net/article0/djogoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站策劃、電子商務(wù)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容