码迷,mamicode.com
首页 > 编程语言 > 详细

Python实现的Observer Pattern

时间:2014-07-10 17:34:56      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:style   color   使用   os   for   io   

题记:
设计模式是需要不断的巩固的,尤其日常工作中使用C编码,很少涉及模式的工作。我一直认为程序员是一份需要长久经验积累的职业,但是如果不注重学习和总结,这种积累多数只能落实到虚无的所谓“管理”,暗暗寄希望能够谋求一份不用编码的管理岗,而这完全不是我所希望的。


正文:
最近因为部门调整,学习时间较多,利用这段时间抽空把Python又拿出来学习一下,借此机会也复习一下设计模式。下面使用Python简单实现一下Observer Pattern,希望对大家有所帮助。

#Version: 0.1
#Author: Walle Fu
class Account(object):
    def __init__(self,id,name,balance):
        self.ID = id
        self.name = name
        self.__balance = balance
        self.observers = set()
       
    def __del__(self):
        for ob in self.observers:
            ob.close()
            del self.observers
           
    def register(self,observer):
        self.observers.add(observer)
       
    def unregister(self,observer):
        self.observers.remove(observer)
       
    def notify(self):
        for ob in self.observers:
            ob.update()
       
    def withdraw(self,amt):
        self.__balance -= amt
        self.notify()
    def getBalanceValue(self):
        return self.__balance
   
import weakref
class AccountObserver(object):
    accountDict = {}
    def __init__(self, theaccount):
        #self.accountref = weakref.ref(theaccount) # Create a weakref
        theaccount.register(self)
        if theaccount.ID not in AccountObserver.accountDict:
            AccountObserver.accountDict[theaccount.ID]= weakref.ref(theaccount)
        else:
            print "Error>> Account Id conflict, please recheck it."
    def __del__(self):
        for id in AccountObserver.accountDict:
            acc = AccountObserver.accountDict[id]() # Get account
            if acc: # Unregister if still exists
                acc.unregister(self)
           
    def update(self):
        for id in AccountObserver.accountDict:
             if id:
                 print "ID=", id, ", Name=", AccountObserver.accountDict[id]().name, \
                       ", Balance=", AccountObserver.accountDict[id]().getBalanceValue()
       
    def close(self, id):
        if id in AccountObserver.accountDict:
            print "ID=", id, ", Account no longer in use"
            acc = AccountObserver.accountDict[id]()
            if acc:
                acc.unregister(self)
            del AccountObserver.accountDict[id]
    def add(self, theAccount):
        theAccount.register(self)
        if theAccount.ID not in AccountObserver.accountDict:
            AccountObserver.accountDict[theAccount.ID]= weakref.ref(theAccount)
        else:
            print "Error>> Account Id conflict, please recheck it."
   
a = Account(‘0001‘,‘Tom‘,1000.00)
b = Account(‘0002‘,‘Jimmy‘,2000.00)
a_ob = AccountObserver(a)
a_ob.update()
a_ob.add(b)
a_ob.update()
a_ob.close(‘0001‘)
a_ob.update()
b.withdraw(100)
a.withdraw(100)


PS:
我一直在强化的一些技能(C++,Python,重构,设计模式,模式识别算法),在现在的工作中完全无用武之地,但我一直不希望这些技能荒废掉。也许我有一些不思进取,始终不肯逃出来主动寻求一些改变,在现有的“安逸”状态迷茫着。人们常说“男怕入错行”,还真有一些道理,我这个学计算机的,毕业后一直在通信圈里混,现在想改变也有很多的困境,即将毕业的同学一定要慎重选择好第一份工作。



Python实现的Observer Pattern,布布扣,bubuko.com

Python实现的Observer Pattern

标签:style   color   使用   os   for   io   

原文地址:http://blog.csdn.net/walle_love_eva/article/details/37597197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!