PHP版本

<?php
/**
* 观察者模式
* 观察者模式能够便利的创建查看目标对象状态的对象,并且提供与核心对象非耦合的指定性功能。
*
* 为软件添加由某个动作或状态变化激活的,但是松散耦合的新功能时,应当创建基于观察者模式的对象。
*
* SPL:Standard PHP Library PHP标准库
*/ 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() {
$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 Login implements SplObserver{
public function update(SplSubject $subject) {
echo "登录次数:".$subject->lognum.'<br>';
}
}
//实施观察
$user = new User('学习');
$user->attach(new Login());
$user->login(); //添加模块
class Hobby implements SplObserver{
public function update(SplSubject $subject) {
echo "爱好:".$subject->hobby;
}
}
$hobby = new Hobby;
$user->attach($hobby);
$user->login();
echo '<br>';
//删除观察模块
$user->detach($hobby);
$user->login();

JS版本

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>观察者模式</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
div{border:solid gray 1px;margin-top:10px;height: 100px;width: 200px;}
</style>
</head>
<body>
<h1>观察者模式</h1>
<select>
<option value="1">风格1</option>
<option value="2">风格2</option>
</select>
<button onclick="detachC()">不观察C</button>
<button onclick="attachC()">观察C</button>
<!--模块一-->
<div id="A">A</div>
<!--模块二-->
<div id="B">B</div>
<!--模块三-->
<div id="C">C</div>
<script>
//观察者
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 a = document.getElementById('A');
var b = document.getElementById('B');
a.update = function(observe) {
if (1 == observe.value) {
this.innerHTML = 'A模块:风格1';
}
if (2 == observe.value) {
this.innerHTML = 'A模块:风格2';
}
}
b.update = function(observe) {
if (1 == observe.value) {
this.innerHTML = 'B模块:风格1';
}
if (2 == observe.value) {
this.innerHTML = 'B模块:风格2';
}
}
//开启观察
sel.attach('a', a);
sel.attach('b', b); //添加观察C模块
var c = document.getElementById('C');
c.update = function(observe) {
if (1 == observe.value) {
this.innerHTML = 'C模块:风格1';
}
if (2 == observe.value) {
this.innerHTML = 'C模块:风格2';
}
}
sel.attach('c', c); //取消观察
function attachC(){
sel.attach('c',c);
}
function detachC(){
sel.detach('c');
}
</script>
</body>
</html>

最新文章

  1. IE8兼容模式设置
  2. session会话复制
  3. 百度地图api 常用demo
  4. Git撤销提交和修改相关操作
  5. SQL Server 中的三种分页方式
  6. js:合同-已知起始日期、年限,自动计算截止日期
  7. 【j2ee】div浮动层拖拽
  8. vue 使用 supermap iclient-classic
  9. Python操作Excel 之 openpyxl
  10. 数据分析之Matplotlib和机器学习基础
  11. RabbitMQ总结
  12. 本地项目托管到github上
  13. 【hdu 5628】Clarke and math (Dirichlet卷积)
  14. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型
  15. Android 模拟器设置
  16. python dataframe (method,partial,dir,hasattr,setattr,getarrt)
  17. 【加密算法】DES
  18. C. Connect Three(构造)
  19. 开源单点登录系统CAS入门
  20. application/x-www-form-urlencoded 与 application/json区别

热门文章

  1. (转)Linux 最大进程数
  2. 牛客网Java刷题知识点之内存溢出和内存泄漏的概念、区别、内存泄露产生原因、内存溢出产生原因、内存泄露解决方案、内存溢出解决方案
  3. 023-将表单序列化为json对象
  4. matlab中如何根据t检验参数查找t检验值
  5. Javascript怎么跳出循环,嵌套循环。
  6. 迟到的UED(转发)
  7. 2017年9月17日 JavaScript简介
  8. Scanners-Box:开源扫描器大全 2017-04-22
  9. 选择排序——Java实现
  10. HDU 5011 NIM博弈