Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5014527
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2013-12-19 11:34:57


  1. #! /usr/bin/env python
  2. #coding=utf-8

  3. ## 1 多适配

  4. 一个简单的适配器只适配一个对象,但一个适配器可适配多个对象。
  5. 如果一个适配器采用一个以上的对象,它被称为多适配器。

  6. from zope.interface import Interface
  7. from zope.interface import implements
  8. from zope.component import adapts

  9. class IAdapteeOne(Interface):
  10.     pass

  11. class IAdapteeTwo(Interface):
  12.     pass

  13. class IFunctionality(Interface):
  14.     pass

  15. #适配器
  16. class MyFunctionality(object):
  17.     #提供的接口
  18.     implements(IFunctionality)
  19.     #适配对象类
  20.     adapts(IAdapteeOne, IAdapteeTwo)
  21.     def __init__(self, one, two):
  22.         self.one = one
  23.         self.two = two

  24. from zope.component import getGlobalSiteManager
  25. gsm = getGlobalSiteManager()
  26. gsm.registerAdapter(MyFunctionality)

  27. class One(object):
  28.     implements(IAdapteeOne)

  29. class Two(object):
  30.     implements(IAdapteeTwo)

  31. one = One()
  32. two = Two()

  33. from zope.component import getMultiAdapter
  34. print getMultiAdapter((one, two), IFunctionality) #doctest: +ELLIPSIS

  35. myfunctionality = getMultiAdapter((one,two), IFunctionality)
  36. print myfunctionality.one #doctest: +ELLIPSIS
  37. print myfunctionality.two #doctest: +ELLIPSIS


  1. #! /usr/bin/env python
  2. #coding=utf-8

  3. #2 订阅适配器

  4. #适配器订阅时, 我们想要的所有适配器对象适配一个特定的接口。也被称为订阅者订阅适配器。
  5. #考虑一个验证问题。我们有很多对象,我们想要评估这些对象是否符合某种标准。
  6. #我们定义了一个验证接口

  7. from zope.interface import Interface
  8. from zope.interface import Attribute
  9. from zope.interface import implements

  10. class IValidate(Interface):
  11.     def validate(ob):
  12.         """ Determine whether the object is valid
  13.             Return a string describing a validation problem.
  14.             An empty string is returned to indicate that the
  15.             object is valid.
  16.         """
  17.         
  18. class IDocument(Interface):
  19.     summary = Attribute("Document summary")
  20.     body = Attribute("Document text")

  21. class Document(object):
  22.     implements(IDocument)
  23.     def __init__(self, summary, body):
  24.         self.summary, self.body = summary, body
  25.         
  26. from zope.component import adapts

  27. #单行适配器
  28. class SingleLineSummary:
  29.     #是陪对象为提供IDocument接口的对象
  30.     adapts(IDocument)
  31.     implements(IValidate)
  32.     def __init__(self, doc):
  33.         self.doc = doc

  34.     def validate(self):
  35.         if '\n' in self.doc.summary:
  36.             return 'Summary should only have one line'
  37.         else:
  38.             return ''

  39. #长度适配器
  40. class AdequateLength(object):
  41.     adapts(IDocument)
  42.     implements(IValidate)
  43.     def __init__(self, doc):
  44.         self.doc = doc

  45.     def validate(self):
  46.         if len(self.doc.body) < 1000:
  47.             return 'too short'
  48.         else:
  49.             return ''
  50.         
  51. from zope.component import getGlobalSiteManager
  52. gsm = getGlobalSiteManager()

  53. #注册两个适配器
  54. gsm.registerSubscriptionAdapter(SingleLineSummary)
  55. gsm.registerSubscriptionAdapter(AdequateLength)

  56. from zope.component import subscribers

  57. #我们能够通过订阅者验证对象
  58. doc = Document("A\nDocument", "blah")
  59. print [adapter.validate() for adapter in subscribers([doc], IValidate) if adapter.validate()]

  60. #['Summary should only have one line', 'too short']

  61. doc = Document("A\nDocument", "blah" * 1000)
  62. print [adapter.validate()
  63.     for adapter in subscribers([doc], IValidate)
  64.         if adapter.validate()]
  65.         
  66. #['Summary should only have one line']
  67. doc = Document("A Document", "blah")
  68. print [adapter.validate()
  69.     for adapter in subscribers([doc], IValidate)
  70.         if adapter.validate()]
  71.         
  72. #['too short']



  1. #! /usr/bin/env python
  2. #coding=utf-8

  3. #3.处理器
  4. #处理程序是订阅适配器不产生任何东东的工厂。当调用时,做所有的工作。
  5. #处理程序通常用于处理事件。处理程序也被称为事件订阅者或事件订阅适配器。
  6. #事件订阅者不同于其他订阅适配器的是:事件订阅者的访问者不希望任何直接的方式与之交互。
  7. #例如,一个事件发布者不期望得到任何返回值。
  8. #因为订阅者不需要给她的调用者提供一个API.他是更自然的函数定义而不是类。
  9. #例如,在文档管理系统中,我们可能需要记录文件的创建时间:
  10.     
  11. import datetime


  12. from zope.interface import Interface
  13. from zope.interface import Attribute
  14. from zope.interface import implements

  15. class IDocumentCreated(Interface):
  16.     doc = Attribute("The document that was created")
  17.     
  18. class DocumentCreated(object):
  19.     implements(IDocumentCreated)
  20.     def __init__(self, doc):
  21.         self.doc = doc
  22.         print self.doc
  23.         
  24. from zope.component import adapter

  25. #作为渲染器
  26. @adapter(IDocumentCreated)
  27. def documentCreated(event):
  28.     event.doc["b"] = datetime.datetime.utcnow()
  29.     event.doc["c"] = "lallallllallallal"
  30.     print event.doc
  31.     
  32. from zope.component import getGlobalSiteManager
  33. gsm = getGlobalSiteManager()
  34. gsm.registerHandler(documentCreated)

  35. #我们可以创建一个事件和使用的处理函数调用注册为事件处理程序
  36. from zope.component import handle
  37. doc = {"a":1}
  38. handle(DocumentCreated(doc))
  39. print doc

阅读(1356) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~