|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 | """*What is this pattern about?
|
5 |
| -The Factory Method pattern can be used to create an interface for a |
6 |
| -method, leaving the implementation to the class that gets |
7 |
| -instantiated. |
| 5 | +A Factory is an object for creating other objects. |
8 | 6 |
|
9 | 7 | *What does this example do?
|
10 | 8 | The code shows a way to localize words in two languages: English and
|
11 |
| -Greek. "getLocalizer" is the factory method that constructs a |
| 9 | +Greek. "get_localizer" is the factory function that constructs a |
12 | 10 | localizer depending on the language chosen. The localizer object will
|
13 | 11 | be an instance from a different class according to the language
|
14 | 12 | localized. However, the main code does not have to worry about which
|
15 |
| -localizer will be instantiated, since the method "get" will be called |
| 13 | +localizer will be instantiated, since the method "localize" will be called |
16 | 14 | in the same way independently of the language.
|
17 | 15 |
|
18 | 16 | *Where can the pattern be used practically?
|
|
25 | 23 |
|
26 | 24 | *References:
|
27 | 25 | http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
|
28 |
| -https://fkromer.github.io/python-pattern-references/design/#factory-method |
29 |
| -https://sourcemaking.com/design_patterns/factory_method |
30 | 26 |
|
31 | 27 | *TL;DR80
|
32 | 28 | Creates objects without having to specify the exact class.
|
33 | 29 | """
|
34 | 30 |
|
| 31 | +from __future__ import unicode_literals |
| 32 | +from __future__ import print_function |
35 | 33 |
|
36 |
| -class GreekGetter(object): |
37 | 34 |
|
| 35 | +class GreekLocalizer(object): |
38 | 36 | """A simple localizer a la gettext"""
|
39 | 37 |
|
40 | 38 | def __init__(self):
|
41 |
| - self.trans = dict(dog="σκύλος", cat="γάτα") |
| 39 | + self.translations = {"dog": "σκύλος", "cat": "γάτα"} |
42 | 40 |
|
43 |
| - def get(self, msgid): |
| 41 | + def localize(self, msg): |
44 | 42 | """We'll punt if we don't have a translation"""
|
45 |
| - return self.trans.get(msgid, str(msgid)) |
| 43 | + return self.translations.get(msg, msg) |
46 | 44 |
|
47 | 45 |
|
48 |
| -class EnglishGetter(object): |
| 46 | +class EnglishLocalizer(object): |
| 47 | + """Simply echoes the message""" |
49 | 48 |
|
50 |
| - """Simply echoes the msg ids""" |
51 |
| - |
52 |
| - def get(self, msgid): |
53 |
| - return str(msgid) |
| 49 | + def localize(self, msg): |
| 50 | + return msg |
54 | 51 |
|
55 | 52 |
|
56 | 53 | def get_localizer(language="English"):
|
57 |
| - """The factory method""" |
58 |
| - languages = dict(English=EnglishGetter, Greek=GreekGetter) |
59 |
| - return languages[language]() |
| 54 | + """Factory""" |
| 55 | + localizers = { |
| 56 | + "English": EnglishLocalizer, |
| 57 | + "Greek": GreekLocalizer, |
| 58 | + } |
| 59 | + return localizers[language]() |
60 | 60 |
|
61 | 61 |
|
62 |
| -if __name__ == '__main__': |
| 62 | +def main(): |
| 63 | + """ |
63 | 64 | # Create our localizers
|
64 |
| - e, g = get_localizer(language="English"), get_localizer(language="Greek") |
| 65 | + >>> e, g = get_localizer(language="English"), get_localizer(language="Greek") |
| 66 | +
|
65 | 67 | # Localize some text
|
66 |
| - for msgid in "dog parrot cat bear".split(): |
67 |
| - print(e.get(msgid), g.get(msgid)) |
68 |
| - |
69 |
| -### OUTPUT ### |
70 |
| -# dog σκύλος |
71 |
| -# parrot parrot |
72 |
| -# cat γάτα |
73 |
| -# bear bear |
| 68 | + >>> for msg in "dog parrot cat bear".split(): |
| 69 | + ... print(e.localize(msg), g.localize(msg)) |
| 70 | + dog σκύλος |
| 71 | + parrot parrot |
| 72 | + cat γάτα |
| 73 | + bear bear |
| 74 | + """ |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + import doctest |
| 79 | + doctest.testmod() |
0 commit comments