烽火社区

标题: 【设计模式玩pyBoard】4.代理模式 [打印本页]

作者: tmb1983    时间: 2016-6-2 08:44 AM
标题: 【设计模式玩pyBoard】4.代理模式
    代理模式:为其他对象提供代理机制来控制访问这个对象。
    代码:

  1. import pyb

  2. class Interface:
  3.         def Request(self):
  4.                 pyb.LED(1).on()
  5.                 pyb.delay(1000)
  6.                 pyb.LED(1).off()
  7.                 pyb.delay(1000)
  8.                
  9. class Subject(Interface):
  10.         def Request(self):
  11.                 pyb.LED(2).on()
  12.                 pyb.delay(1000)
  13.                 pyb.LED(2).off()
  14.                 pyb.delay(1000)
  15.                
  16. class Proxy(Interface):
  17.         def Request(self):
  18.                 self.obj = Subject()
  19.                 self.obj.Request()
  20.         

  21. if __name__ == "__main__":
  22.         led = Proxy()
  23.         led.Request()
复制代码

    通过Proxy类来实现对Interface类的访问,但其实Interface类的Request函数并没有执行。看效果是这样,不知道理解的对不对。

作者: RedAdobe    时间: 2016-6-2 08:57 AM
代理一般都代理啥呢、?
作者: wbc178    时间: 2016-6-2 09:00 AM
灵澈 发表于 2016-6-2 08:57
代理一般都代理啥呢、?

对类的代理,实例化时不是直接对父类操作,经过子类的封装来选择执行父类的方法。
作者: 德州仪器    时间: 2016-6-2 10:29 AM
不懂呀。。。。
作者: 可乐兑    时间: 2016-6-2 10:42 AM
HelloWii 发表于 2016-6-2 10:29
不懂呀。。。。

我也不懂,一点一点的理解,可能有偏差。
作者: hongyu123    时间: 2016-6-2 03:20 PM
好尼害的样子
作者: fsy_1979ELeAcer    时间: 2016-6-2 06:24 PM
灵澈 发表于 2016-6-2 15:20
好尼害的样子

学习中




欢迎光临 烽火社区 (http://bbs.cnecport.com/) Powered by Discuz! X3.2