|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
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 | +""" |
5 | 31 |
|
6 | 32 |
|
7 | 33 | class GreekGetter(object): |
|
0 commit comments