2010年4月29日 星期四

Design Pattern 設計模式 Observer 觀察者模式

I am wrintng webpage app recently,so I used the GAE standard for 'Google App Engine' to be the Server client,So I use the Python and Javascript often,of course JQuery else,


最近在開發 webpage app,使用GAE做為Server端,所以得使用到Python和Javascript,當然還有JQuery。

and because I am a person who like to use 'Design Pattern' in my code.
so I start to share how to use 'Design Pattern' in the code today, and hope this series can help someone who is writing webpage app ,too and help me to remenber this.
If you have any problem,it's welcome to comment,thx~

也因為我是個"設計模式"的愛好者,所以開始分享一些我觀察到的一些在javascript和python上可以運用的"設計模式"方法,希望可以提醒我自已記得如何使用,及幫助到需要的人。
若是有任何指教,歡迎留言,謝謝。
the first design pattern , I want to share its 'Observer' pattern:

觀察者模式:

Python:

import sys
import os
################### BASIC CLASS begin ##############
class Subject():
name='Subject'
def __init__(self,name):
self.name=name
#
parms={}
def setTemp(self,parms):
self.parms=parms
self.notify()

#for observer
objs=[]
def register(self,obj):
self.objs.append(obj)

def remove(self,obj):
self.objs.remove(obj)

def notify(self):
for i in self.objs:
print i
i.update(self,self.parms)

class Observer():
name='Observer'
def __init__(self,name):
self.name=name

def update(subject,self,parms):
print 'Subject Name: %s,Observer Name : %s ,Value: %i'%(subject.name,self.name, parms['value'])

pass

def display(self):
pass
################### BASIC CLASS end ##############

class Weather(Subject):
pass

class Board(Observer):
value=0

def update(self,parms):
self.value=parms['value']
self.display()
def display(self):
print self.value


class Combine(Subject,Observer):
pass


def main():
s=Weather('Weather')
b=Combine('Board')

s.register(b)
s.setTemp({'value':99})




if __name__!='__main__':
main()




javascript:


cd=console.debug

function Subject()
{
this.name='Subject';
this.parms={};
this.setTemp=function(parms){
this.parms=parms;
this.notify()
}
//
this.objs=new Array();
this.register=function(obj){
this.objs.push(obj);
}
this.remove=function(obj){

}
this.notify=function(){
for (i in this.objs){
this.objs[i].update(this.parms)
}
}
}
function Observer()
{
this.name='Observer';
this.update=function(parms){
this.display(parms);
}

this.display=function(parms){
cd(parms);
}
}

//set different class over there
function Weather()
{
var father=new Subject();
father.name='Weather';
return father;
}

function Board(){
var father=new Observer();
father.name='Board';
father.display=function(parms){
cd('Value:'+parms['value'])
}
return father;
}

function main()
{
var s=new Weather();
var o=new Board();
s.register(o);
s.setTemp({'value':99})
cd(o);
}

in the javascript, if you want to combine the Subject and Observer class, it also have a way to do it,like:

function Observer()
{
.....
this.dir={name:this.name,display:this.display}//you have to add all class's parms and fucntions pointer in the 'dir' arraylist,also do it in Subject class
}
//after this ,than you can add this class
function Combine()
{
var f1=new Subject();
var f2=new Observer();
var me={};
for (i in f1.dir)
{
me[i]=f1.dir[i];
}
for (i in f2.dir)
{
me[i]=f2.dir[i];
}

return me;
}
function Station()
{
var father =new Combine();

return father;
}
//then finally you get combine class

after create combine class in the javascript, the codes is not easy to read amymore,but for the class seldom have to be Subject and Observer.
so just for requirement.
不過為了實現'結合',程式碼好像不前麼好讀了,通常也很少是觀察者也是主題的,看需求囉。

沒有留言:

張貼留言