且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

几行代码搞定python 设计模式[转]

更新时间:2022-09-06 08:10:40

Python代码  几行代码搞定python 设计模式[转]
  1. #!/usr/bin/env python   
  2. # -*- coding:utf-8   
  3.   
  4. class HttpBase:   
  5.     def get(self):   
  6.         psss   
  7. class Http1(HttpBase):   
  8.     def get(self):   
  9.         print 'http1'   
  10. class Http2(HttpBase):   
  11.     def get(self):   
  12.         print 'http2'   
  13.   
  14.   
  15. class Base:   
  16.     def __init__(self):   
  17.         self.httpobj = None   
  18.     def http(self):   
  19.         self.httpobj.get()   
  20.     def compute(self):   
  21.         self.http()   
  22.         self.show()   
  23.     #虚函数   
  24.     def show(self):   
  25.         pass   
  26.     def notify(self, k):   
  27.         print 'notify', k   
  28.           
  29.   
  30. #桥接模式,通过A,B 关联不同的http1和http2   
  31. class BaseA(Base):   
  32.     def __init__(self):   
  33.         self.httpobj = Http1()          
  34.     def notify(self, k):   
  35.         print 'A notify', k       
  36.     def show(self):   
  37.         print 'show a'   
  38.              
  39. class BaseB(Base):   
  40.     def __init__(self):   
  41.         self.httpobj = Http2()   
  42.     def notify(self, k):   
  43.         print 'B notify', k   
  44.     def show(self):   
  45.         print 'show b'   
  46.   
  47. #观测者模式   
  48. class Observer:   
  49.     def __init__(self):   
  50.         self.listOB = []   
  51.     def register(self, obj):   
  52.         self.listOB.append(obj)   
  53.     def notify(self):   
  54.         for obj in self.listOB:   
  55.             obj.notify(len(self.listOB))   
  56.   
  57. #适配器模式   
  58. class B1:   
  59.     def http(self):   
  60.         BaseB().http()   
  61. #工厂模式   
  62. class Factory:   
  63.     def CreateA(self):   
  64.         return BaseA()   
  65.     def CreateB(self):   
  66.         return BaseB()   
  67.   
  68.   
  69. #单例模式   
  70. class Logger(object):   
  71.     log = None   
  72.     @staticmethod   
  73.     def new():   
  74.           
  75.         import threading   
  76.         #线程安全   
  77.         mylock = threading.RLock()   
  78.         mylock.acquire()   
  79.         if not Logger.log:   
  80.             Logger.log = Logger()   
  81.         mylock.release()   
  82.           
  83.         return Logger.log   
  84.     def write(self, v):   
  85.         print 'Logger ', v   
  86.   
  87. if __name__ == "__main__":   
  88.     a = Factory().CreateA()   
  89.     b = Factory().CreateB()   
  90.       
  91.     objS = Observer()   
  92.     objS.register(a)   
  93.     objS.register(b)   
  94.       
  95.     a.compute()   
  96.     b.compute()   
  97.     objS.notify()   
  98.       
  99.     b1 = B1()   
  100.     b1.http()   
  101.       
  102.     Logger.new().log.write('v')   
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2012/07/04/2575513.html,如需转载请自行联系原作者