Skip to content

Commit d6cac0f

Browse files
committed
Rename _factory method_ to factory, update
1 parent 27395d8 commit d6cac0f

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ __Creational Patterns__:
1313
| [abstract_factory](patterns/creational/abstract_factory.py) | use a generic function with specific factories |
1414
| [borg](patterns/creational/borg.py) | a singleton with shared-state among instances |
1515
| [builder](patterns/creational/builder.py) | instead of using multiple constructors, builder object receives parameters and returns constructed objects |
16-
| [factory_method](patterns/creational/factory_method.py) | delegate a specialized function/method to create instances |
16+
| [factory](patterns/creational/factory.py) | delegate a specialized function/method to create instances |
1717
| [lazy_evaluation](patterns/creational/lazy_evaluation.py) | lazily-evaluated property pattern in Python |
1818
| [pool](patterns/creational/pool.py) | preinstantiate and maintain a group of instances of the same type |
1919
| [prototype](patterns/creational/prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) |

patterns/creational/factory_method.py renamed to patterns/creational/factory.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
# -*- coding: utf-8 -*-
33

44
"""*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.
86
97
*What does this example do?
108
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
1210
localizer depending on the language chosen. The localizer object will
1311
be an instance from a different class according to the language
1412
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
1614
in the same way independently of the language.
1715
1816
*Where can the pattern be used practically?
@@ -25,49 +23,57 @@
2523
2624
*References:
2725
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
3026
3127
*TL;DR80
3228
Creates objects without having to specify the exact class.
3329
"""
3430

31+
from __future__ import unicode_literals
32+
from __future__ import print_function
3533

36-
class GreekGetter(object):
3734

35+
class GreekLocalizer(object):
3836
"""A simple localizer a la gettext"""
3937

4038
def __init__(self):
41-
self.trans = dict(dog="σκύλος", cat="γάτα")
39+
self.translations = {"dog": "σκύλος", "cat": "γάτα"}
4240

43-
def get(self, msgid):
41+
def localize(self, msg):
4442
"""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)
4644

4745

48-
class EnglishGetter(object):
46+
class EnglishLocalizer(object):
47+
"""Simply echoes the message"""
4948

50-
"""Simply echoes the msg ids"""
51-
52-
def get(self, msgid):
53-
return str(msgid)
49+
def localize(self, msg):
50+
return msg
5451

5552

5653
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]()
6060

6161

62-
if __name__ == '__main__':
62+
def main():
63+
"""
6364
# 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+
6567
# 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

Comments
 (0)