Skip to content

Commit f851891

Browse files
committed
Added explanation about the Factory Method pattern.
1 parent e3d1dd0 commit f851891

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

creational/factory_method.py

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

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

632

733
class GreekGetter(object):

0 commit comments

Comments
 (0)