【编程思想】【设计模式】【创建模式creational 】工厂模式factory_method
Python版
https://github.com/faif/python-patterns/blob/master/creational/factory_method.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- """*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.
“getLocalizer”是工厂方法,他根据选择的语言的不同,构建Localizer
The localizer object will
be an instance from a different class according to the language
localized.
>>Localizer对象根据选择语言翻译器的不同,成为不同的实例
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.
>>但是,主要程序不同担心哪个翻译器会被实力话,因为get方法会根据选择语言的不同有不同的调用 *Where can the pattern be used practically?
>>这个设计模式会被用在什么地方
The Factory Method can be seen in the popular web framework Django:
>>在非常流程的Django网络框架中,我们经常可以看到工厂模式
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.
>>例如在http://django.wikispaces.asu.edu/*NEW*+Django+Design+Patterns,
>>在网页的联系表中,主题和消息的地方就是使用相同的表格工厂(CharField()),
>>尽管看起来他们有不同的实现方式 *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 *TL;DR80
Creates objects without having to specify the exact class.
""" class GreekGetter(object): """A simple localizer a la gettext""" def __init__(self):
self.trans = dict(dog="σκύλος", cat="γάτα") def get(self, msgid):
"""We'll punt if we don't have a translation"""
return self.trans.get(msgid, str(msgid)) class EnglishGetter(object): """Simply echoes the msg ids""" def get(self, msgid):
return str(msgid) def get_localizer(language="English"):
"""The factory method"""
languages = dict(English=EnglishGetter, Greek=GreekGetter)
return languages[language]() if __name__ == '__main__':
# Create our localizers
e, g = get_localizer(language="English"), get_localizer(language="Greek")
# Localize some text
for msgid in "dog parrot cat bear".split():
print(e.get(msgid), g.get(msgid)) ### OUTPUT ###
# dog σκύλος
# parrot parrot
# cat γάτα
# bear bear
Python转载版
Python版
大话设计模式
【编程思想】【设计模式】【创建模式creational 】工厂模式factory_method的更多相关文章
- python设计模式---创建型之工厂模式
		# coding = utf-8 from abc import ABCMeta, abstractmethod # 简单工厂模式 class Animal(metaclass=ABCMeta): @ ... 
- [Python编程实战] 第一章 python的创建型设计模式1.1抽象工厂模式
		注:关乎对象的创建方式的设计模式就是“创建型设计模式”(creational design pattern) 1.1 抽象工厂模式 “抽象工厂模式”(Abstract Factory Pattern) ... 
- Head First 设计模式 第4章工厂模式
		第4章 工厂模式 在介绍工厂模式之前,先让我们来看一个例子. 这里有一个Pizza类,用来生产pizza,并返回对象,具体代码如下: package com.ek.factory.simple; im ... 
- C#设计模式之二简单工厂模式(过渡模式)
		一.引言 之所以写这个系列,是了为了自己更好的理解设计模式,也为新手提供一些帮助,我都是用最简单的.最生活化的实例来说明.在上一篇文章中讲解了单例模式,今天就给大家讲一个比较简单的模式--简单工厂模式 ... 
- Java设计模式(1)工厂模式(Factory模式)
		工厂模式定义:提供创建对象的接口. 为何使用工厂模式 工厂模式是我们最常用的模式了,著名的Jive论坛,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因 ... 
- php基础设计模式 注册树模式、工厂模式、单列模式
		废话不多说了,先给大家介绍注册树模式然后介绍工厂模式最后给大家介绍单列模式,本文写的很详细,一起来学习吧. php注册树模式 什么是注册树模式? 注册树模式当然也叫注册模式,注册器模式.之所以我在这里 ... 
- IOS设计模式浅析之抽象工厂模式(Abstract Factory)
		概述 在前面两章中,分别介绍了简单工厂模式和工厂方法模式,我们知道简单工厂模式的优点是去除了客户端与具体产品的依赖,缺点是违反了“开放-关闭原则”:工厂方法模式克服了简单工厂模式的缺点,将产品的创建工 ... 
- 设计模式之单例模式与工厂模式的Python实现(二)
		2. 工厂模式 工厂模式是创建型设计模式的一种.核心的思想是,通过传递给类或函数某种产品的信息来创建产品并返回.当我们想得到产品a对象,只需把产品a的名字传递给工厂函数就能得到产品a对象.而核心思想的 ... 
- 创建型模式(过渡模式) 简单工厂模式(Simple Factory)
		简单工厂模式(Simple Factory Pattern)属于类的创建型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern) 是通过专门定义一个类来负责创建其他类的 ... 
- C# 设计模式(1)——简单工厂模式、工厂模式、抽象工厂模式
		1.前言 上一篇写了设计模式原则有助于我们开发程序的时候能写出高质量的代码(牵一发而不动全身),这个系列还是做个笔记温习一下各种设计模式,下面就看看简单工厂模式.工厂模式.抽象工厂模式. 2.简单工厂 ... 
随机推荐
- 开源项目|Go 开发的一款分布式唯一 ID 生成系统
			原文连接: 开源项目|Go 开发的一款分布式唯一 ID 生成系统 今天跟大家介绍一个开源项目:id-maker,主要功能是用来在分布式环境下生成唯一 ID.上周停更了一周,也是用来开发和测试这个项目的 ... 
- url的hash和HTML5的history
			url的hash和HTML5的history 第一种方法是改变url的hash值. **显示当前路径 : **location.href http://localhost:8080/# 切换路径: l ... 
- grep命令详解与正则表达式
			grep命令主要是做什么的呢 ?下面我们就来研究下. grep命令简单来说就是"过滤".就是把想看的数据通过grep过滤出来,把不想看的通过grep过滤掉. 它是一种强大的文本搜索 ... 
- Merge into用法总结
			简单的说就是,判断表中有没有符合on()条件中的数据,有了就更新数据,没有就插入数据. 有一个表T,有两个字段a.b,我们想在表T中做Insert/Update,如果条件满足,则更新T中b的值,否则在 ... 
- oracle合并列的函数wm_concat的使用详解
			oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_concat(column)函数实现字段合并,如果您对oracle wm_concat( ... 
- IO流(二)
			一:字符流 字符输入流 写入文件字符流 import java.io.FileWriter; import java.io.IOException; //fileWriter public class ... 
- Python 爬取 拉钩
			... from urllib import request from urllib import parse from urllib.error import URLError import jso ... 
- 多线程合集(一)---信号量,锁,以及并发编程,自定义任务调度和awaiter
			引言 在后端开发中,多线程技术总是后端开发中常用到的技术,那什么是多线程呢,在操作系统中,程序运行的最小单位是进程,那线程则是进程里面的最小单位,关系是一对多的关系,而线程的调度,是由操作系统的时间片 ... 
- Jetpack架构组件学习(2)——ViewModel和Livedata使用
			要看本系列其他文章,可访问此链接Jetpack架构学习 | Stars-One的杂货小窝 原文地址:Jetpack架构组件学习(2)--ViewModel和Livedata使用 | Stars-One ... 
- idea解决Command line is too long. Shorten command line for ServiceStarter or also for Application报错
			找到 .idea\workspace.xml: 找到<component name="PropertiesComponent">,在里面添加<property n ... 
