zoukankan      html  css  js  c++  java
  • Factory Method(工厂方法)

    Factory Method(工厂方法)

    意图:

    定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使个类的实例化延迟到其子类。

    适用性:

    当一个类不知道它所必须创建的对象的类的时候。

    当一个类希望由它的子类来指定它所创建的对象的时候。

    当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。

    实现:

    #!/usr/bin/python
    #coding:utf8
    '''
    Factory Method
    '''
     
    class ChinaGetter:
        """A simple localizer a la gettext"""
        def __init__(self):
            self.trans = dict(dog=u"小狗", cat=u"小猫")
     
        def get(self, msgid):
            """We'll punt if we don't have a translation"""
            try:
                return self.trans[msgid]
            except KeyError:
                return str(msgid)
     
     
    class EnglishGetter:
        """Simply echoes the msg ids"""
        def get(self, msgid):
            return str(msgid)
     
     
    def get_localizer(language="English"):
        """The factory method"""
        languages = dict(English=EnglishGetter, China=ChinaGetter)
        return languages[language]()
     
    # Create our localizers
    e, g = get_localizer("English"), get_localizer("China")
    # Localize some text
    for msgid in "dog parrot cat bear".split():
        print(e.get(msgid), g.get(msgid))
  • 相关阅读:
    (转)django上传文件
    django中的认证与登录
    django中的转义
    django中的request对象详解
    dotnetspider
    区块链试验
    管理员权限
    axure跨inframe传递参数
    python selenium chrome 测试
    python chrome selenium
  • 原文地址:https://www.cnblogs.com/navysummer/p/9835084.html
Copyright © 2011-2022 走看看