"""*What is this pattern about?
A Factory is an object for creating other objects.
*What does this example do?
The code shows a way to localize words in two languages: English and
Greek. "get_localizer" is the factory function that constructs a
localizer depending on the language chosen. The localizer object will
be an instance from a different class according to the language
localized. However, the main code does not have to worry about which
localizer will be instantiated, since the method "localize" will be called
in the same way independently of the language.
*Where can the pattern be used practically?
The Factory Method can be seen in the popular web framework 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.
*References:
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
*TL;DR
Creates objects without having to specify the exact class.
"""classGreekLocalizer:"""A simple localizer a la gettext"""def__init__(self):
self.translations ={"dog":"σκύλος","cat":"γάτα"}deflocalize(self, msg):"""We'll punt if we don't have a translation"""return self.translations.get(msg, msg)classEnglishLocalizer:"""Simply echoes the message"""deflocalize(self, msg):return msg
defget_localizer(language="English"):"""Factory"""
localizers ={"English": EnglishLocalizer,"Greek": GreekLocalizer,}return localizers[language]()defmain():"""
# Create our localizers
>>> e, g = get_localizer(language="English"), get_localizer(language="Greek")
# Localize some text
>>> for msg in "dog parrot cat bear".split():
... print(e.localize(msg), g.localize(msg))
dog σκύλος
parrot parrot
cat γάτα
bear bear
"""if __name__ =="__main__":import doctest
doctest.testmod()
工厂模式"""*What is this pattern about?A Factory is an object for creating other objects.*What does this example do?The code shows a way to localize words in two languages: English andGreek. "get_loc...