zoukankan      html  css  js  c++  java
  • zope.component的简单例子

    from zope.interface import Interface, Attribute, implements
    from zope.component import adapts, getAdapter, getUtility, getGlobalSiteManager
    
    # Interface
    class IPerson(Interface):
         name = Attribute("Name")
         email = Attribute("Email Address")
         phone = Attribute("Phone Number")
    
    class IPersonAdapter(Interface):
        def showName():
            """showName"""
        
        def showMail():
            """showMail"""
    
    class IUtility(Interface):
        def doSomething():
            """doSomething"""
    
    # Implements
    class Person(object):
        implements(IPerson)
    
        name = None
        email = None
        phone = None
    
    class PersonAdapter(object):
        implements(IPersonAdapter)
        adapts(IPerson)
        
        def __init__(self, person):
            self.person = person
        
        def showName(self):
            print(self.person.name)
        
        def showMail(self):
            print(self.person.email)
    
    class Utility(object):
        implements(IUtility)
        
        def doSomething(self):
            print("doSomething")
        
    
    if __name__ == '__main__':
        gsm = getGlobalSiteManager()
        
        # use Adapter
        gsm.registerAdapter(PersonAdapter)
        person = Person()
        person.name = "William"
        person.email = "admin@qq.com"
        person.phone = "000000"
        personAd = getAdapter(person, IPersonAdapter)
        personAd.showName()
        
        # use Utility
        utility = Utility()
        gsm.registerUtility(utility, IUtility)
        getUtility(IUtility).doSomething()
    

      

  • 相关阅读:
    docker 安装 nexus3 初始密码不再是admin123
    eclipse中Tomcat修改项目名称
    WAMP3.1.3自定义根目录
    git学习笔记
    小米和MAC触摸板手势汇总
    IDEA快捷键汇总
    servelet 实现Post接口访问
    LeetCode:Jump Game II
    LeetCode:Trapping Rain Water
    LeetCode: Container With Most Water
  • 原文地址:https://www.cnblogs.com/catcat811/p/2228276.html
Copyright © 2011-2022 走看看