From f851891213e5d7b45ab49845f9c105356e47d518 Mon Sep 17 00:00:00 2001 From: Mateus Cruz Date: Tue, 13 Jun 2017 20:19:07 +0900 Subject: [PATCH 1/2] Added explanation about the Factory Method pattern. --- creational/factory_method.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/creational/factory_method.py b/creational/factory_method.py index d29a2ed3f..0f7bab66b 100644 --- a/creational/factory_method.py +++ b/creational/factory_method.py @@ -1,7 +1,33 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/""" +""" +*What is this pattern about? +The Factory Method pattern can be used to create an interface for a +method, leaving the implementation to the class that gets +instantiated. + +*What does this example do? +The code shows a way to localize words in two languages: English and +Greek. "getLocalizer" is the factory method that constructs a +localizer depending on the language chosen. The localizer object will +instantiate a different class according to the language of that +localized, but the main code does not have to worry about which +localizer will be instantiated, since the method "get" will be called +in the same way independently of the language. + +*Where can the pattern be used practically? +The Factory Method can be seen in the popular web framework Django: +http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns +For example, in a contact form, the subject and the message fields are +created using the same form factory (CharField()), even though they +will have different implementations according to their purposes. + +*References: +http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ +https://fkromer.github.io/python-pattern-references/design/#factory-method +https://sourcemaking.com/design_patterns/factory_method +""" class GreekGetter(object): From c71e9a2fd745d0c3a70ced78a2060c4a95df80aa Mon Sep 17 00:00:00 2001 From: Mateus Cruz Date: Tue, 13 Jun 2017 20:24:38 +0900 Subject: [PATCH 2/2] Corrected description of Factory Method pattern. --- creational/factory_method.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/creational/factory_method.py b/creational/factory_method.py index 0f7bab66b..6a1d334b6 100644 --- a/creational/factory_method.py +++ b/creational/factory_method.py @@ -1,8 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -""" -*What is this pattern about? +"""*What is this pattern about? The Factory Method pattern can be used to create an interface for a method, leaving the implementation to the class that gets instantiated. @@ -11,22 +10,24 @@ The code shows a way to localize words in two languages: English and Greek. "getLocalizer" is the factory method that constructs a localizer depending on the language chosen. The localizer object will -instantiate a different class according to the language of that -localized, but the main code does not have to worry about which +be an instance from a different class according to the language +localized. However, the main code does not have to worry about which localizer will be instantiated, since the method "get" will be called in the same way independently of the language. *Where can the pattern be used practically? The Factory Method can be seen in the popular web framework Django: -http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns -For example, in a contact form, the subject and the message fields are -created using the same form factory (CharField()), even though they -will have different implementations according to their purposes. +http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns For +example, in a contact form of a web page, the subject and the message +fields are created using the same form factory (CharField()), even +though they have different implementations according to their +purposes. *References: http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ https://fkromer.github.io/python-pattern-references/design/#factory-method https://sourcemaking.com/design_patterns/factory_method + """