zoukankan      html  css  js  c++  java
  • <读书笔记> Thinking in python (Python 设计模式) 3. Proxy and State模式

    1.Proxy代理模式
     1 #: c04:ProxyDemo.py
     2 
     3 # Simple demonstration of the Proxy pattern.
     4 
     5 
     6 
     7 class Implementation:
     8 
     9   def f(self): 
    10 
    11     print "Implementation.f()"
    12 
    13   def g(self): 
    14 
    15     print "Implementation.g()" 
    16 
    17   def h(self): 
    18 
    19     print "Implementation.h()" 
    20 
    21 
    22 
    23 class Proxy:
    24 
    25   def __init__(self): 
    26 
    27     self.__implementation = Implementation() 
    28 
    29   # Pass method calls to the implementation:
    30 
    31   def f(self): self.__implementation.f() 
    32 
    33   def g(self): self.__implementation.g() 
    34 
    35   def h(self): self.__implementation.h() 
    36 
    37 
    38 
    39 = Proxy()
    40 
    41 p.f(); p.g(); p.h()
    42 
    43 #<hr>
    44 
    45 output = '''
    46 
    47 Implementation.f()
    48 
    49 Implementation.g()
    50 
    51 Implementation.h()
    52 
    53 '''
    54 

    是一种利用复合(using a)代替继承(is a)的方法,Implementaion不一定需要和Proxy具有相同的接口名称,但是相同的名称更易理解。
    Python中的委派机制(delegation)使得Proxy的实现可以非常的简洁优美。如下,利用__getattr__,使得程序具有完整的通用性(generic)
    这是动态语言特有的优势,见Dive Into Python第四章,自省的魅力,
    "
    Python 中万 物皆对象,自省指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作。
    利用这种方法,你可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用事先并不知道名称的函数(getattr)
    "
     1 #: c04:ProxyDemo2.py
     2 
     3 # Simple demonstration of the Proxy pattern.
     4 
     5 
     6 
     7 class Implementation2:
     8 
     9   def f(self): 
    10 
    11     print "Implementation.f()"
    12 
    13   def g(self): 
    14 
    15     print "Implementation.g()" 
    16 
    17   def h(self): 
    18 
    19     print "Implementation.h()" 
    20 
    21 
    22 
    23 class Proxy2:
    24 
    25   def __init__(self): 
    26 
    27     self.__implementation = Implementation2() 
    28 
    29   def __getattr__(self, name):
    30 
    31     return getattr(self.__implementation, name)
    32 
    33 
    34 
    35 = Proxy2()
    36 
    37 p.f(); p.g(); p.h();
    38 
    39 #<hr>
    40 
    41 output = '''
    42 
    43 Implementation.f()
    44 
    45 Implementation.g()
    46 
    47 Implementation.h()
    48 
    49 '''
    50 
    2.State模式
    与Proxy模式类似,作者认为区别是可以使用不同的implementaion,动态调整使用的implementation.
     1 #: c04:StateDemo.py
     2 
     3 # Simple demonstration of the State pattern.
     4 
     5 
     6 
     7 class State_d:
     8 
     9   def __init__(self, imp): 
    10 
    11     self.__implementation = imp 
    12 
    13   def changeImp(self, newImp):
    14 
    15     self.__implementation = newImp
    16 
    17   # Delegate calls to the implementation:
    18 
    19   def __getattr__(self, name):
    20 
    21     return getattr(self.__implementation, name)
    22 
    23 
    24 
    25 class Implementation1:
    26 
    27   def f(self): 
    28 
    29     print "Fiddle de dum, Fiddle de dee," 
    30 
    31   def g(self): 
    32 
    33     print "Eric the half a bee." 
    34 
    35   def h(self): 
    36 
    37     print "Ho ho ho, tee hee hee," 
    38 
    39 
    40 
    41 class Implementation2:
    42 
    43   def f(self): 
    44 
    45     print "We're Knights of the Round Table." 
    46 
    47   def g(self): 
    48 
    49     print "We dance whene'er we're able." 
    50 
    51   def h(self): 
    52 
    53     print "We do routines and chorus scenes" 
    54 
    55 
    56 
    57 def run(b):
    58 
    59   b.f()
    60 
    61   b.g()
    62 
    63   b.h()
    64 
    65   b.g()
    66 
    67 
    68 
    69 = State_d(Implementation1())
    70 
    71 run(b)
    72 
    73 b.changeImp(Implementation2())
    74 
    75 run(b)
    76 
    77 #<hr>
    78 
    79 output = '''
    80 
    81 Fiddle de dum, Fiddle de dee,
    82 
    83 Eric the half a bee.
    84 
    85 Ho ho ho, tee hee hee,
    86 
    87 Eric the half a bee.
    88 
    89 We're Knights of the Round Table.
    90 
    91 We dance whene'er we're able.
    92 
    93 We do routines and chorus scenes
    94 
    95 We dance whene'er we're able.
    96 
    97 '''
    98 

  • 相关阅读:
    Java学习第十五章 之 Map、可变参数、Collections
    Java第十四章 之 List、Set
    Java学习第十三章 之 常用API
    通过shell终端上传下载文件
    javamail邮件发送
    linux防火墙添加例外端口shell脚本
    MySQL批量更新
    MySQL返回列名
    发现一个有意思的东西
    struts2,action方法自动执行两次
  • 原文地址:https://www.cnblogs.com/rocketfan/p/1575182.html
Copyright © 2011-2022 走看看