zoukankan      html  css  js  c++  java
  • 【编程思想】【设计模式】【创建模式creational 】工厂模式factory_method

    Python版

    https://github.com/faif/python-patterns/blob/master/creational/factory_method.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """*What is this pattern about?
    >>这个设计模式是干什么的
    The Factory Method pattern can be used to create an interface for a
    method, leaving the implementation to the class that gets
    instantiated.
    >>这个设计模式可以为方法创建一个接口,让其他继承的类来实现
    
    *What does this example do?
    The code shows a way to localize words in two languages: English and
    Greek.
    >>这个代码表示把单词翻译成两种语言,英语和希腊语
    "getLocalizer" is the factory method that constructs a
    localizer depending on the language chosen.
    “getLocalizer”是工厂方法,他根据选择的语言的不同,构建Localizer
    The localizer object will
    be an instance from a different class according to the language
    localized.
    >>Localizer对象根据选择语言翻译器的不同,成为不同的实例
    However, the main code does not have to worry about which
    localizer will be instantiated, since the method "get" will be called
    in the same way independently of the language.
    >>但是,主要程序不同担心哪个翻译器会被实力话,因为get方法会根据选择语言的不同有不同的调用
    
    *Where can the pattern be used practically?
    >>这个设计模式会被用在什么地方
    The Factory Method can be seen in the popular web framework Django:
    >>在非常流程的Django网络框架中,我们经常可以看到工厂模式
    http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns For
    example, in a contact form of a web page, the subject and the message
    fields are created using the same form factory (CharField()), even
    though they have different implementations according to their
    purposes.
    >>例如在http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns,
    >>在网页的联系表中,主题和消息的地方就是使用相同的表格工厂(CharField()),
    >>尽管看起来他们有不同的实现方式
    
    *References:
    http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
    https://fkromer.github.io/python-pattern-references/design/#factory-method
    https://sourcemaking.com/design_patterns/factory_method
    
    *TL;DR80
    Creates objects without having to specify the exact class.
    """
    
    
    class GreekGetter(object):
    
        """A simple localizer a la gettext"""
    
        def __init__(self):
            self.trans = dict(dog="σκύλος", cat="γάτα")
    
        def get(self, msgid):
            """We'll punt if we don't have a translation"""
            return self.trans.get(msgid, str(msgid))
    
    
    class EnglishGetter(object):
    
        """Simply echoes the msg ids"""
    
        def get(self, msgid):
            return str(msgid)
    
    
    def get_localizer(language="English"):
        """The factory method"""
        languages = dict(English=EnglishGetter, Greek=GreekGetter)
        return languages[language]()
    
    
    if __name__ == '__main__':
        # Create our localizers
        e, g = get_localizer(language="English"), get_localizer(language="Greek")
        # Localize some text
        for msgid in "dog parrot cat bear".split():
            print(e.get(msgid), g.get(msgid))
    
    ### OUTPUT ###
    # dog σκύλος
    # parrot parrot
    # cat γάτα
    # bear bear
    Python转载版

    Python版

    大话设计模式

  • 相关阅读:
    腾讯2017校招编程:一个数等于两个素数的和
    人类简史:从动物到上帝
    讨论:研发团队到底应该是制定OKR还是制定KPI?
    绩效主义毁了索尼
    研发团队是该制定OKR还是KPI?
    一切不行,都是“人”的不行?
    能力陷阱:能力越强,越容易失败
    一个好的产品经理到底有多么重要?
    管理:不会把目标翻译成任务,要你何用?
    做一个有产品思维的研发:Scrapy安装
  • 原文地址:https://www.cnblogs.com/demonzk/p/9015462.html
Copyright © 2011-2022 走看看