Skip to content

Commit 140fabe

Browse files
authored
Merge pull request faif#198 from mshcruz/master
Explanation of the Factory Method pattern
2 parents e3d1dd0 + c71e9a2 commit 140fabe

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

creational/factory_method.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/"""
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.
8+
9+
*What does this example do?
10+
The code shows a way to localize words in two languages: English and
11+
Greek. "getLocalizer" is the factory method that constructs a
12+
localizer depending on the language chosen. The localizer object will
13+
be an instance from a different class according to the language
14+
localized. However, the main code does not have to worry about which
15+
localizer will be instantiated, since the method "get" will be called
16+
in the same way independently of the language.
17+
18+
*Where can the pattern be used practically?
19+
The Factory Method can be seen in the popular web framework Django:
20+
http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns For
21+
example, in a contact form of a web page, the subject and the message
22+
fields are created using the same form factory (CharField()), even
23+
though they have different implementations according to their
24+
purposes.
25+
26+
*References:
27+
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+
31+
"""
532

633

734
class GreekGetter(object):

0 commit comments

Comments
 (0)