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的更多相关文章

  1. python设计模式---创建型之工厂模式

    # coding = utf-8 from abc import ABCMeta, abstractmethod # 简单工厂模式 class Animal(metaclass=ABCMeta): @ ...

  2. [Python编程实战] 第一章 python的创建型设计模式1.1抽象工厂模式

    注:关乎对象的创建方式的设计模式就是“创建型设计模式”(creational design pattern) 1.1 抽象工厂模式 “抽象工厂模式”(Abstract Factory Pattern) ...

  3. Head First 设计模式 第4章工厂模式

    第4章 工厂模式 在介绍工厂模式之前,先让我们来看一个例子. 这里有一个Pizza类,用来生产pizza,并返回对象,具体代码如下: package com.ek.factory.simple; im ...

  4. C#设计模式之二简单工厂模式(过渡模式)

    一.引言 之所以写这个系列,是了为了自己更好的理解设计模式,也为新手提供一些帮助,我都是用最简单的.最生活化的实例来说明.在上一篇文章中讲解了单例模式,今天就给大家讲一个比较简单的模式--简单工厂模式 ...

  5. Java设计模式(1)工厂模式(Factory模式)

    工厂模式定义:提供创建对象的接口. 为何使用工厂模式 工厂模式是我们最常用的模式了,著名的Jive论坛,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因 ...

  6. php基础设计模式 注册树模式、工厂模式、单列模式

    废话不多说了,先给大家介绍注册树模式然后介绍工厂模式最后给大家介绍单列模式,本文写的很详细,一起来学习吧. php注册树模式 什么是注册树模式? 注册树模式当然也叫注册模式,注册器模式.之所以我在这里 ...

  7. IOS设计模式浅析之抽象工厂模式(Abstract Factory)

    概述 在前面两章中,分别介绍了简单工厂模式和工厂方法模式,我们知道简单工厂模式的优点是去除了客户端与具体产品的依赖,缺点是违反了“开放-关闭原则”:工厂方法模式克服了简单工厂模式的缺点,将产品的创建工 ...

  8. 设计模式之单例模式与工厂模式的Python实现(二)

    2. 工厂模式 工厂模式是创建型设计模式的一种.核心的思想是,通过传递给类或函数某种产品的信息来创建产品并返回.当我们想得到产品a对象,只需把产品a的名字传递给工厂函数就能得到产品a对象.而核心思想的 ...

  9. 创建型模式(过渡模式) 简单工厂模式(Simple Factory)

    简单工厂模式(Simple Factory Pattern)属于类的创建型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern) 是通过专门定义一个类来负责创建其他类的 ...

  10. C# 设计模式(1)——简单工厂模式、工厂模式、抽象工厂模式

    1.前言 上一篇写了设计模式原则有助于我们开发程序的时候能写出高质量的代码(牵一发而不动全身),这个系列还是做个笔记温习一下各种设计模式,下面就看看简单工厂模式.工厂模式.抽象工厂模式. 2.简单工厂 ...

随机推荐

  1. 攻防世界 Misc 新手练习区 如来十三掌 Writeup

    攻防世界 Misc 新手练习区 如来十三掌 Writeup 题目介绍 题目考点 佛曰加密.base64.Rot13等加密方法的了解 Writeup 下载并打开附件 联想到佛曰加密,复制内容到 佛曰加密 ...

  2. CentOS服务器的网络配置与部署

    1.系统安装与软件安装 1.1选择CentOs7.9release版本用作所研发系统部署服务器,官网以及所选择镜像为地址为:http://ftp.sjtu.edu.cn/centos/7.9.2009 ...

  3. gorm框架表名自动加s问题

    查看日志会发现表名自动加了s 在model实现以下方法即可解决 type UsUser struct { ID int64 `gorm:"column:id" db:"c ...

  4. 查看python是32位,还是64位

    步骤:cmd打开命令行,输入python,查看. 如果32bit,则是32位:如果是64,则是64位 如果需要安装客户端进行orcale数据库操作,则要保证python\

  5. [gym102220I]Temperature Survey

    (为了方便,以下记$a_{0}=0,a_{n+1}=n$​​,并将$n$​​加上1) 构造一个$n$行的网格图,从上到下第$i$行有$a_{i}$个格子,格子左对齐 记第$i$行第$j$个格子为$(i ...

  6. [luogu2303]Longge的问题

    1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 ll n,ans; 5 ll phi(l ...

  7. exCRT & 骆克强乘法

    exCRT & 骆克强乘法 只是丢两个板子啦. exCRT的做法就是每次拿两个方程合并成一个,合并的过程推下式子就是个 exgcd.具体可以在 zjk 的 ptt 里面找到. 先放个 $ O( ...

  8. Codeforces 1299D - Around the World(线性基+图论+dp)

    Codeforces 题目传送门 & 洛谷题目传送门 一道线性基的综合题 %%%%%% 首先注意到"非简单路径""异或和"等字眼,可以本能地想到线性基. ...

  9. DirectX12 3D 游戏开发与实战第十章内容(上)

    仅供个人学习使用,请勿转载.谢谢! 10.混合 本章将研究混合技术,混合技术可以让我们将当前需要光栅化的像素(也称为源像素)和之前已经光栅化到后台缓冲区的像素(也称为目标像素)进行融合.因此,该技术可 ...

  10. Matlab 调用 Python 脚本

    Matlab 调用 Python 脚本 最近尝试在 Matlab 环境中调用 Python 脚本,这里总结下碰到的几个问题. 1. Python 模块加载 在 Matlab 函数中,想要将 Pytho ...