|
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 | +"""*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 | +""" |
5 | 32 |
|
6 | 33 |
|
7 | 34 | class GreekGetter(object):
|
|
0 commit comments