基于Prototype实现对象级别Observer模式
关键字: ObserverPrototype框架提供了基于JavaScript语言的面向对象风格的AJAX库,使编写动态WEB程序成为可能。基于Prototype的Scriptaculous的流行就是一个很好的证明。
问题:

上述方式对于降低类之间依赖不错。但是我们必须从特定的类继承或实现特定的接口。Observer消息触发方式导致问题也需要考虑。详见:http://www.microsoft.com/china/MSDN/library/architecture/patterns/esp/DesObserver.mspx?mfr=true
当然,我们可以在JavaScript中按照上述结构实现Observer会导致更多问题,例如如何实现接口编程?即使这个问题很容易解决,无论从理解和使用上这种模式都不太方便、不够直接。
设计:
再次聚焦Observer模式并观察它的核心,会发现如何实现消息触发是关键。一旦ConcreteSubject对象拥有ConcreteObserver的引用,在消息触发时就可以直接调用后者的Update方法。
代码执行顺序:
在concreteSubject的对象内部:
if(this.stateChanged())
concreteObservers.update(this,args);
再看我的问题:在调用A对象X方法之后调用B对象的Y方法。
在A对象内部:
this.X();
B.Y();
A. _$_X = A.X;
A.X = {this._$_X(); B.Y();}
同时增加unregister方法设法恢复A对象的行为(X方法)。
代码:
- var StringBuffer = Class.create();
- StringBuffer.prototype = {
- emptyString:"",
- initialize:function(){
- this.data=[];
- },
- clear:function(){
- this.data.length=0;
- },
- append:function(){
- for(var i=0,len=arguments.length;i
- this.data[this.data.length]=arguments[i];
- }
- return this;
- },
- toString:function(){
- return this.data.join(this.emptyString);
- }
- };
- var Observer = Class.create();
- Observer.prototype = {
- initialize:function(){
- },
- /**
- * register the action to the object's method
- * @param {Object} object
- * @param {String} method
- * @param {String} action
- */
- register:function(object,method,action){
- if(!object || !method || !action || method == typeof String || action == typeof String)return;
- //checks whether the method is existed.
- var f = object[method];
- if(!f)return;
- //copy, rename the method and replace it as a new method.
- var _$_f = "_$_"+method;
- if(!object[_$_f]){
- object[_$_f] = f;
- object[method]= this.createFunction(method,action);
- }
- },
- /**
- * unregister the action from the object's method
- * @param {Object} object
- * @param {Object} method
- */
- unregister:function(object,method){
- if(!object||!method)return;
- var _$_f = "_$_"+method;
- if(object[_$_f]){
- object[method] = null;
- object[method] = object[_$_f];
- object[_$_f] = null;
- }
- },
- /**
- * create a new function that invokes both the method and the action
- * @param {String} method
- * @param {String} action
- */
- createFunction:function(method,action){
- var sb = new StringBuffer();
- sb.append("this[\"_$_",method,"\"]();\r",action);
- return new Function(sb.toString());
- }
- }
评论
--至少在AJAX时代里,还允许我们名正言顺地玩一下js.再看看一个command模式的小小实现,嘻嘻~
先定义一个Command对象,代码如下:
function Command(obj) {
var commandObj = obj; // save the reference of working object
var oldProp = new Object(); // save old properties
// set new properties and save old properties
this.Do = function() {
for (var o in this) {
oldProp[o] = commandObj[o];
commandObj[o] = this[o];
}
}
// restore old properties
this.Undo = function() {
for (var o in oldProp) {
commandObj[o] = oldProp[o];
}
delete oldProp;
oldProp = new Object();
}
}
如果要调用,这样写就可以了:
// "mc" is an object that has _alpha and _x attributes var com = new Command(mc); com._alpha = 20; com._x = 200; com.Do(); // do something... // remember to save this Command somewhere... // now, undo it! com.Undo();
http://developer.yahoo.com/yui/docs/CustomEvent.js.html
yui早就以这来设计组件了,看看yui,yui-ext充满了event.
yui-ext对其进行一定的扩展.
另外dojo.connect更是用的有点走火魔了。
yui-ext的observable简直就是我的最爱,没办法--太sexy了
--我甚至把它应用在服务端的JS(asp)
其实没必要把obserbable看的那么神秘高深的.
都是被java害苦的,其实对于很多动态语言来说,这不算什么.
aop其实也是这样,在java,c#中,aop好象是救世主一样,在很多动态语言中,什么aop什么的,语言级别就支持了.
还有设计模式,在动态语言眼中,
尤其是在服务器端,选择太多了.
感叹一声,和我一样,生长在java,c#阳光下的coder
http://developer.yahoo.com/yui/docs/CustomEvent.js.html
yui早就以这来设计组件了,看看yui,yui-ext充满了event.
yui-ext对其进行一定的扩展.
另外dojo.connect更是用的有点走火魔了。
yui-ext的observable简直就是我的最爱,没办法--太sexy了
--我甚至把它应用在服务端的JS(asp)
http://developer.yahoo.com/yui/docs/CustomEvent.js.html
yui早就以这来设计组件了,看看yui,yui-ext充满了event.
yui-ext对其进行一定的扩展.
另外dojo.connect更是用的有点走火魔了。
- 浏览: 69536 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
基于MVC的Flex framework ...
PureMVC 不错。 简单易学,也好用
-- by zhouzhao21 -
PSP开发环境
请问一下这段代码有什么问题么?? bool flag = mResourceMg ...
-- by wangshu3000 -
Tomcat下设置session超时 ...
"Tomcat默认的会话超时是1800秒,测试一次需要等太久" -- 把系统时 ...
-- by sam.ds.chen -
[翻译]Ext vs. Dojo
netfishx 写道ext始终没有机会用于正式的项目中,但它确实是我所见过最协 ...
-- by blackanger -
TDD in AJAX
现在yui,jquery,dojo等都用自己的testcase框架,很是郁闷,为 ...
-- by hyysguyang






评论排行榜