javascript实现观察者模式
2009-03-24 23:28
453 查看
/*---------------------------
defined observer
----------------------------*/
function Observer()
{
}
Observer.prototype.update = function(context)
{
alert(context);
}
function ConcreteObserver()
{
Observer.call(this);
}
ConcreteObserver.prototype.update = function(context)
{
alert("ConcreteObserver response " + context);
}
/*---------------------------
defined ObserverCollection
----------------------------*/
function ObserverCollection()
{
this._observers_ = new Array();
}
ObserverCollection.prototype.add = function(observer)
{
this._observers_.push(observer);
}
ObserverCollection.prototype.count = function()
{
return this._observers_.length;
}
ObserverCollection.prototype.getAt = function(index)
{
if (index > -1 && index < this._observers_.length)
{
return this._observers_[index];
}
return undefined;
}
/*---------------------------
defined Subject
----------------------------*/
function Subject(name)
{
this.name = name;
this._obs_ = new ObserverCollection();
}
Subject.prototype.add = function(ob)
{
if (ob.update)
{
this._obs_.add(ob);
}
}
Subject.prototype.nameChanged = function()
{
var ob;
for(var i=0; i < this._obs_.count(); i++)
{
ob = this._obs_.getAt(i);
ob.update(this.name);
}
};
Subject.prototype.setName = function(newName)
{
if (this.name != newName)
{
this.name = newName;
this.nameChanged();
}
}
var sub = new Subject("jjy");
sub.add(new Observer());
sub.add(new ConcreteObserver());
sub.setName("Jack");
sub.setName("HongYing");
defined observer
----------------------------*/
function Observer()
{
}
Observer.prototype.update = function(context)
{
alert(context);
}
function ConcreteObserver()
{
Observer.call(this);
}
ConcreteObserver.prototype.update = function(context)
{
alert("ConcreteObserver response " + context);
}
/*---------------------------
defined ObserverCollection
----------------------------*/
function ObserverCollection()
{
this._observers_ = new Array();
}
ObserverCollection.prototype.add = function(observer)
{
this._observers_.push(observer);
}
ObserverCollection.prototype.count = function()
{
return this._observers_.length;
}
ObserverCollection.prototype.getAt = function(index)
{
if (index > -1 && index < this._observers_.length)
{
return this._observers_[index];
}
return undefined;
}
/*---------------------------
defined Subject
----------------------------*/
function Subject(name)
{
this.name = name;
this._obs_ = new ObserverCollection();
}
Subject.prototype.add = function(ob)
{
if (ob.update)
{
this._obs_.add(ob);
}
}
Subject.prototype.nameChanged = function()
{
var ob;
for(var i=0; i < this._obs_.count(); i++)
{
ob = this._obs_.getAt(i);
ob.update(this.name);
}
};
Subject.prototype.setName = function(newName)
{
if (this.name != newName)
{
this.name = newName;
this.nameChanged();
}
}
var sub = new Subject("jjy");
sub.add(new Observer());
sub.add(new ConcreteObserver());
sub.setName("Jack");
sub.setName("HongYing");
相关文章推荐
- 以【猫叫、老鼠跑、主人醒】为例子,使用 javascript 来实现 观察者模式 (有在线演示)
- 翻译:观察者模式—使用JavaScript实现(转)
- 用javascript实现观察者模式
- javascript中的观察者模式实现
- ruby、javascript、php中的观察者模式实现代码
- 使用 javascript 来实现 观察者模式
- 介绍一个款可以在javascript对象上实现观察者模式的类库-Watch.js
- javascript观察者模式实现自动刷新效果
- 观察者模式的Javascript实现方式
- 原生JavaScript实现观察者模式
- 翻译:观察者模式—使用JavaScript实现
- javascript 观察者模式实现
- 用javaScript实现观察者模式
- JavaScript原生实现观察者模式的示例
- javascript 观察者模式实现
- 传统javascript事件模型实现观察者模式
- 我所理解的设计模式(C++实现)——观察者模式(Observer Pattern)
- 老鼠跑猫叫主人惊醒c++观察者模式实现
- Guava学习笔记-简化观察者模式的实现
- Javascript实现单例模式