From 99c65776495e816f621dd524bdd133f3f6907f36 Mon Sep 17 00:00:00 2001 From: Floyda Date: Thu, 20 Aug 2015 17:06:59 +0800 Subject: [PATCH 1/6] Chinese README.md --- README.md | 2 ++ zh-CN/3-tier.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ zh-CN/README.md | 40 ++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 zh-CN/3-tier.py create mode 100644 zh-CN/README.md diff --git a/README.md b/README.md index 1fdee48a2..9e3296620 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ python-patterns =============== +[中文翻译](./zh-CN/README.md) + A collection of design patterns and idioms in Python. When an implementation is added or modified, be sure to update this file and diff --git a/zh-CN/3-tier.py b/zh-CN/3-tier.py new file mode 100644 index 000000000..6346be2c4 --- /dev/null +++ b/zh-CN/3-tier.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +class Data(object): + """ Data Store Class """ + + products = { + 'milk': {'price': 1.50, 'quantity': 10}, + 'eggs': {'price': 0.20, 'quantity': 100}, + 'cheese': {'price': 2.00, 'quantity': 10} + } + + def __get__(self, obj, klas): + print ("(Fetching from Data Store)") + return {'products': self.products} + + +class BusinessLogic(object): + """ Business logic holding data store instances """ + + data = Data() + + def product_list(self): + return self.data['products'].keys() + + def product_information(self, product): + return self.data['products'].get(product, None) + + +class Ui(object): + """ UI interaction class """ + + def __init__(self): + self.business_logic = BusinessLogic() + + def get_product_list(self): + print('PRODUCT LIST:') + for product in self.business_logic.product_list(): + print(product) + print('') + + def get_product_information(self, product): + product_info = self.business_logic.product_information(product) + if product_info: + print('PRODUCT INFORMATION:') + print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format( + product.title(), product_info.get('price', 0), + product_info.get('quantity', 0))) + else: + print('That product "{0}" does not exist in the records'.format( + product)) + + +def main(): + ui = Ui() + ui.get_product_list() + ui.get_product_information('cheese') + ui.get_product_information('eggs') + ui.get_product_information('milk') + ui.get_product_information('arepas') + +if __name__ == '__main__': + main() + +### OUTPUT ### +# PRODUCT LIST: +# (Fetching from Data Store) +# cheese +# eggs +# milk +# +# (Fetching from Data Store) +# PRODUCT INFORMATION: +# Name: Cheese, Price: 2.00, Quantity: 10 +# (Fetching from Data Store) +# PRODUCT INFORMATION: +# Name: Eggs, Price: 0.20, Quantity: 100 +# (Fetching from Data Store) +# PRODUCT INFORMATION: +# Name: Milk, Price: 1.50, Quantity: 10 +# (Fetching from Data Store) +# That product "arepas" does not exist in the records diff --git a/zh-CN/README.md b/zh-CN/README.md new file mode 100644 index 000000000..5116821ed --- /dev/null +++ b/zh-CN/README.md @@ -0,0 +1,40 @@ +pyhton-设计模式 +=============== + +收集用Pyhton写的设计模式和风格. + +当有一个新的成品时, 更新这个文件并执行`append_output.sh` +(举个栗子: ./append_output.sh borg.py), 以保证底部的输入结果是最新的. + +已有的模式: + +| 模式 | 说明 | +|:-------:| ----------- | +| [三层架构](3-tier.py) | 数据<->业务逻辑<->描述 分离 (严格的关系) | +| [abstract_factory](abstract_factory.py) | use a generic function with specific factories | +| [adapter](adapter.py) | adapt one interface to another using a whitelist | +| [borg](borg.py) | a singleton with shared-state among instances | +| [bridge](bridge.py) | a client-provider middleman to soften interface changes | +| [builder](builder.py) | call many little discrete methods rather than having a huge number of constructor parameters | +| [catalog](catalog.py) | general methods will call different specialized methods based on construction parameter | +| [chain](chain.py) | apply a chain of successive handlers to try and process the data | +| [command](command.py) | bundle a command and arguments to call later | +| [composite](composite.py) | encapsulate and provide access to a number of different objects | +| [decorator](decorator.py) | wrap functionality with other functionality in order to affect outputs | +| [facade](facade.py) | use one class as an API to a number of others | +| [factory_method](factory_method.py) | delegate a specialized function/method to create instances | +| [flyweight](flyweight.py) | transparently reuse existing instances of objects with similar/identical state | +| [graph_search](graph_search.py) | (graphing algorithms, not design patterns) | +| [mediator](mediator.py) | an object that knows how to connect other objects and act as a proxy | +| [memento](memento.py) | generate an opaque token that can be used to go back to a previous state | +| [mvc](mvc.py) | model<->view<->controller (non-strict relationships) | +| [observer](observer.py) | provide a callback for notification of events/changes to data | +| [pool](pool.py) | preinstantiate and maintain a group of instances of the same type | +| [prototype](prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) | +| [proxy](proxy.py) | an object funnels operations to something else | +| [publish_subscribe](publish_subscribe.py) | a source syndicates events/data to 0+ registered listeners | +| [state](state.py) | logic is org'd into a discrete number of potential states and the next state that can be transitioned to | +| [strategy](strategy.py) | selectable operations over the same data | +| [template](template.py) | an object imposes a structure but takes pluggable components | +| [visitor](visitor.py) | invoke a callback for all items of a collection | +| [chaining_method](chaining_method.py) | continue callback next object method | From da30b499c83a3f3191803c1856763aecded4b6cc Mon Sep 17 00:00:00 2001 From: Floyda Date: Thu, 20 Aug 2015 20:25:13 +0800 Subject: [PATCH 2/6] mvc --- zh-CN/3-tier.py | 83 ------------------------------------------------- zh-CN/README.md | 4 +-- 2 files changed, 2 insertions(+), 85 deletions(-) delete mode 100644 zh-CN/3-tier.py diff --git a/zh-CN/3-tier.py b/zh-CN/3-tier.py deleted file mode 100644 index 6346be2c4..000000000 --- a/zh-CN/3-tier.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - - -class Data(object): - """ Data Store Class """ - - products = { - 'milk': {'price': 1.50, 'quantity': 10}, - 'eggs': {'price': 0.20, 'quantity': 100}, - 'cheese': {'price': 2.00, 'quantity': 10} - } - - def __get__(self, obj, klas): - print ("(Fetching from Data Store)") - return {'products': self.products} - - -class BusinessLogic(object): - """ Business logic holding data store instances """ - - data = Data() - - def product_list(self): - return self.data['products'].keys() - - def product_information(self, product): - return self.data['products'].get(product, None) - - -class Ui(object): - """ UI interaction class """ - - def __init__(self): - self.business_logic = BusinessLogic() - - def get_product_list(self): - print('PRODUCT LIST:') - for product in self.business_logic.product_list(): - print(product) - print('') - - def get_product_information(self, product): - product_info = self.business_logic.product_information(product) - if product_info: - print('PRODUCT INFORMATION:') - print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format( - product.title(), product_info.get('price', 0), - product_info.get('quantity', 0))) - else: - print('That product "{0}" does not exist in the records'.format( - product)) - - -def main(): - ui = Ui() - ui.get_product_list() - ui.get_product_information('cheese') - ui.get_product_information('eggs') - ui.get_product_information('milk') - ui.get_product_information('arepas') - -if __name__ == '__main__': - main() - -### OUTPUT ### -# PRODUCT LIST: -# (Fetching from Data Store) -# cheese -# eggs -# milk -# -# (Fetching from Data Store) -# PRODUCT INFORMATION: -# Name: Cheese, Price: 2.00, Quantity: 10 -# (Fetching from Data Store) -# PRODUCT INFORMATION: -# Name: Eggs, Price: 0.20, Quantity: 100 -# (Fetching from Data Store) -# PRODUCT INFORMATION: -# Name: Milk, Price: 1.50, Quantity: 10 -# (Fetching from Data Store) -# That product "arepas" does not exist in the records diff --git a/zh-CN/README.md b/zh-CN/README.md index 5116821ed..45d092093 100644 --- a/zh-CN/README.md +++ b/zh-CN/README.md @@ -9,7 +9,7 @@ pyhton-设计模式 已有的模式: | 模式 | 说明 | -|:-------:| ----------- | +| -------| ----------- | | [三层架构](3-tier.py) | 数据<->业务逻辑<->描述 分离 (严格的关系) | | [abstract_factory](abstract_factory.py) | use a generic function with specific factories | | [adapter](adapter.py) | adapt one interface to another using a whitelist | @@ -27,7 +27,7 @@ pyhton-设计模式 | [graph_search](graph_search.py) | (graphing algorithms, not design patterns) | | [mediator](mediator.py) | an object that knows how to connect other objects and act as a proxy | | [memento](memento.py) | generate an opaque token that can be used to go back to a previous state | -| [mvc](mvc.py) | model<->view<->controller (non-strict relationships) | +| [mvc](mvc.py) | 模型<->视图<->控制器 (非严格的关系) | | [observer](observer.py) | provide a callback for notification of events/changes to data | | [pool](pool.py) | preinstantiate and maintain a group of instances of the same type | | [prototype](prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) | From f8708304792df3a190919dc6790bcaaf5a9ba0dc Mon Sep 17 00:00:00 2001 From: Floyda Date: Fri, 21 Aug 2015 11:52:33 +0800 Subject: [PATCH 3/6] Chinese README.md --- README.md | 2 +- zh-CN/README.md => README_zh.md | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename zh-CN/README.md => README_zh.md (100%) diff --git a/README.md b/README.md index 9e3296620..566427e57 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ python-patterns =============== -[中文翻译](./zh-CN/README.md) +[中文](./zh-CN/README.md) A collection of design patterns and idioms in Python. diff --git a/zh-CN/README.md b/README_zh.md similarity index 100% rename from zh-CN/README.md rename to README_zh.md From 27fc1939d61f846b23c02d52e1846c266f4655fa Mon Sep 17 00:00:00 2001 From: Floyda Date: Tue, 25 Aug 2015 16:25:52 +0800 Subject: [PATCH 4/6] Borg --- README_zh.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README_zh.md b/README_zh.md index 45d092093..561d93069 100644 --- a/README_zh.md +++ b/README_zh.md @@ -11,9 +11,10 @@ pyhton-设计模式 | 模式 | 说明 | | -------| ----------- | | [三层架构](3-tier.py) | 数据<->业务逻辑<->描述 分离 (严格的关系) | +| [mvc](mvc.py) | 模型<->视图<->控制器 (非严格的关系) | +| [borg](borg.py) | 在实例中共享状态的一种单例模式(id不一样, state一样) | | [abstract_factory](abstract_factory.py) | use a generic function with specific factories | | [adapter](adapter.py) | adapt one interface to another using a whitelist | -| [borg](borg.py) | a singleton with shared-state among instances | | [bridge](bridge.py) | a client-provider middleman to soften interface changes | | [builder](builder.py) | call many little discrete methods rather than having a huge number of constructor parameters | | [catalog](catalog.py) | general methods will call different specialized methods based on construction parameter | @@ -27,7 +28,6 @@ pyhton-设计模式 | [graph_search](graph_search.py) | (graphing algorithms, not design patterns) | | [mediator](mediator.py) | an object that knows how to connect other objects and act as a proxy | | [memento](memento.py) | generate an opaque token that can be used to go back to a previous state | -| [mvc](mvc.py) | 模型<->视图<->控制器 (非严格的关系) | | [observer](observer.py) | provide a callback for notification of events/changes to data | | [pool](pool.py) | preinstantiate and maintain a group of instances of the same type | | [prototype](prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) | From ce8e15163fcc12e82b433fad22a4b71ac7f74592 Mon Sep 17 00:00:00 2001 From: Floyda Date: Wed, 26 Aug 2015 18:24:54 +0800 Subject: [PATCH 5/6] factory --- README_zh.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README_zh.md b/README_zh.md index 561d93069..40599548c 100644 --- a/README_zh.md +++ b/README_zh.md @@ -13,7 +13,9 @@ pyhton-设计模式 | [三层架构](3-tier.py) | 数据<->业务逻辑<->描述 分离 (严格的关系) | | [mvc](mvc.py) | 模型<->视图<->控制器 (非严格的关系) | | [borg](borg.py) | 在实例中共享状态的一种单例模式(id不一样, state一样) | -| [abstract_factory](abstract_factory.py) | use a generic function with specific factories | +| [abstract_factory](abstract_factory.lua) | 用一个通用的方法, 根据特定的工厂类来创建一个类的实例 | +| [factory_method](factory_method.lua) | 根据专门的函数或者方法, 创建一个类的实例 | + | [adapter](adapter.py) | adapt one interface to another using a whitelist | | [bridge](bridge.py) | a client-provider middleman to soften interface changes | | [builder](builder.py) | call many little discrete methods rather than having a huge number of constructor parameters | @@ -23,7 +25,6 @@ pyhton-设计模式 | [composite](composite.py) | encapsulate and provide access to a number of different objects | | [decorator](decorator.py) | wrap functionality with other functionality in order to affect outputs | | [facade](facade.py) | use one class as an API to a number of others | -| [factory_method](factory_method.py) | delegate a specialized function/method to create instances | | [flyweight](flyweight.py) | transparently reuse existing instances of objects with similar/identical state | | [graph_search](graph_search.py) | (graphing algorithms, not design patterns) | | [mediator](mediator.py) | an object that knows how to connect other objects and act as a proxy | From f5ac20c2ed61dcd012a5a6e78bd1030d52cad93c Mon Sep 17 00:00:00 2001 From: Floyda Date: Sun, 4 Oct 2015 03:47:09 +0800 Subject: [PATCH 6/6] prototype --- README_zh.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README_zh.md b/README_zh.md index 40599548c..34a357991 100644 --- a/README_zh.md +++ b/README_zh.md @@ -13,9 +13,9 @@ pyhton-设计模式 | [三层架构](3-tier.py) | 数据<->业务逻辑<->描述 分离 (严格的关系) | | [mvc](mvc.py) | 模型<->视图<->控制器 (非严格的关系) | | [borg](borg.py) | 在实例中共享状态的一种单例模式(id不一样, state一样) | -| [abstract_factory](abstract_factory.lua) | 用一个通用的方法, 根据特定的工厂类来创建一个类的实例 | -| [factory_method](factory_method.lua) | 根据专门的函数或者方法, 创建一个类的实例 | - +| [抽象工厂模式](abstract_factory.lua) | 用一个通用的方法, 根据特定的工厂类来创建一个类的实例 | +| [工厂模式](factory_method.lua) | 根据专门的函数或者方法, 创建一个类的实例 | +| [原型](prototype.py) | 用一个工厂类, 通过复制原型, 得到一个新的实例(如果实例存在的话) | | [adapter](adapter.py) | adapt one interface to another using a whitelist | | [bridge](bridge.py) | a client-provider middleman to soften interface changes | | [builder](builder.py) | call many little discrete methods rather than having a huge number of constructor parameters | @@ -31,7 +31,6 @@ pyhton-设计模式 | [memento](memento.py) | generate an opaque token that can be used to go back to a previous state | | [observer](observer.py) | provide a callback for notification of events/changes to data | | [pool](pool.py) | preinstantiate and maintain a group of instances of the same type | -| [prototype](prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) | | [proxy](proxy.py) | an object funnels operations to something else | | [publish_subscribe](publish_subscribe.py) | a source syndicates events/data to 0+ registered listeners | | [state](state.py) | logic is org'd into a discrete number of potential states and the next state that can be transitioned to |