【编程思想】【设计模式】【创建模式creational】建造者模式builder
Python版
https://github.com/faif/python-patterns/blob/master/creational/builder.py
#!/usr/bin/python
# -*- coding : utf-8 -*- """
*What is this pattern about?
It decouples the creation of a complex object and its representation,
so that the same process can be reused to build objects from the same
family.
>>他降低了创建复杂类的难度和重现他的难度,因此相同的流程在相同家族总可以被重用
This is useful when you must separate the specification of an object
from its actual representation (generally for abstraction).
>>当你必须把它特殊的部分从他实际的表现中分离出来的时候很有用,通常用来做抽象 *What does this example do? The first example achieves this by using an abstract base
class for a building, where the initializer (__init__ method) specifies the
steps needed, and the concrete subclasses implement these steps.
>>第一个例子达到了这个目的,他使用一个抽象的积累构建了一个building,
>>初始化方法(__init__)指定了需要的方法,这个实际的子类实现了这些步骤 In other programming languages, a more complex arrangement is sometimes
necessary. In particular, you cannot have polymorphic behaviour in a
constructor in C++ - see https://stackoverflow.com/questions/1453131/how-can-i-get-polymorphic-behavior-in-a-c-constructor
>>在其他的开发语言中,往往需要更加复杂的设计。就像在C++中不能使用多态一样
- which means this Python technique will not work. The polymorphism
required has to be provided by an external, already constructed
instance of a different class.
>>这就意味着python技术不好使了。多态需要外部的,已经构成的,不同的类型的实例来支持 In general, in Python this won't be necessary, but a second example showing
this kind of arrangement is also included.
>>总的来说,在python中,这个不是必须的,但是第二个例子表明这种设计也是可以的 *Where is the pattern used practically? *References:
https://sourcemaking.com/design_patterns/builder *TL;DR80
Decouples the creation of a complex object and its representation.
""" # Abstract Building
class Building(object): def __init__(self):
self.build_floor()
self.build_size() def build_floor(self):
raise NotImplementedError def build_size(self):
raise NotImplementedError def __repr__(self):
return 'Floor: {0.floor} | Size: {0.size}'.format(self) # Concrete Buildings
class House(Building): def build_floor(self):
self.floor = 'One' def build_size(self):
self.size = 'Big' class Flat(Building): def build_floor(self):
self.floor = 'More than One' def build_size(self):
self.size = 'Small' # In some very complex cases, it might be desirable to pull out the building
# logic into another function (or a method on another class), rather than being
# in the base class '__init__'. (This leaves you in the strange situation where
# a concrete class does not have a useful constructor) class ComplexBuilding(object):
def __repr__(self):
return 'Floor: {0.floor} | Size: {0.size}'.format(self) class ComplexHouse(ComplexBuilding):
def build_floor(self):
self.floor = 'One' def build_size(self):
self.size = 'Big and fancy' def construct_building(cls):
building = cls()
building.build_floor()
building.build_size()
return building # Client
if __name__ == "__main__":
house = House()
print(house)
flat = Flat()
print(flat) # Using an external constructor function:
complex_house = construct_building(ComplexHouse)
print(complex_house) ### OUTPUT ###
# Floor: One | Size: Big
# Floor: More than One | Size: Small
# Floor: One | Size: Big and fancy
Python转载版
【编程思想】【设计模式】【创建模式creational】建造者模式builder的更多相关文章
- 设计模式的征途—6.建造者(Builder)模式
建造者模式又称为生成器模式,它是一种较为复杂.使用频率也相对较低的创建型模式.建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品.因为,没有人买车会只买一个方向盘或者轮胎,大 ...
- 设计模式(4)建造者模式/生成器模式(Builder)
设计模式(0)简单工厂模式 设计模式(1)单例模式(Singleton) 设计模式(2)工厂方法模式(Factory Method) 设计模式(3)抽象工厂模式(Abstract Factory) 源 ...
- Java设计模式(3)建造者模式(Builder模式)
Builder模式定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构 ...
- 【设计模式】 模式PK:工厂模式VS建造者模式
1.概述 工厂方法模式注重的是整体对象的创建方法,而建造者模式注重的是部件构建的过程,旨在通过一步一步地精确构造创建出一个复杂的对象.我们举个简单例子来说明两者的差异,如要制造一个超人,如果使用工厂方 ...
- 【设计模式】 模式PK:抽象工厂模式VS建造者模式
1.概述 抽象工厂模式实现对产品家族的创建,一个产品家族是这样的一系列产品:具有不同分类维度的产品组合,采用抽象工厂模式则是不需要关心构建过程,只关心什么产品由什么工厂生产即可.而建造者模式则是要求按 ...
- 设计模式(六)——建造者模式(源码StringBuilder分析)
建造者模式 1 盖房项目需求 1) 需要建房子:这一过程为打桩.砌墙.封顶 2) 房子有各种各样的,比如普通房,高楼,别墅,各种房子的过程虽然一样,但是要求不要相同的. 3) 请编写程序,完成需求. ...
- java设计模式(五)--建造者模式(Builder)
转载:http://zz563143188.iteye.com/blog/1847029 工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,所谓复合对象 ...
- 【设计模式 - 3】之建造者模式(Builder)
1 模式简介 建造者模式也叫生成器模式,和抽象工厂模式相似,也是一种构建复杂对象的模式. 建造者模式中的角色分类: 抽象建造者Builder:接口类型,用于规范各个产品的组成部分: 具体建造 ...
- 2015-03-12---外观模式,建造者模式(附代码),观察者模式(附代码),boost库应用
今天白天主要看了boost库的应用,主要是经常使用的一些库,array,bind,function,regex,thread,unordered,ref,smartpointers库,晚上看了看设计模 ...
- Java设计模式(5)——创建型模式之建造者模式(Builder)
一.概述 概念 将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示.(与工厂类不同的是它用于创建复合对象) UML图 主要角色 抽象建造者(Builder)——规范建造方法与结果 ...
随机推荐
- js事件常用操作、事件流
注册事件 给元素添加事件,称为注册事件或者绑定事件. 注册事件有两种方式:传统方式和方法监听注册方式 传统方式 on开头的事件,例如onclick <button onclick="a ...
- hudi clustering 数据聚集(一)
概要 数据湖的业务场景主要包括对数据库.日志.文件的分析,而管理数据湖有两点比较重要:写入的吞吐量和查询性能,这里主要说明以下问题: 1.为了获得更好的写入吞吐量,通常把数据直接写入文件中,这种情况下 ...
- Linux usb 5. usbip (USB Over IP) 使用实例
文章目录 0. 简介 1. Server 配置 2. Client 配置 参考资料 0. 简介 USB Over IP 是一种应用很多的场景,目前已经有现成的解决方案 usbip.linux 和 wi ...
- ajax - error
... <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...
- 15. mac安装多版本jdk
一.jdk下载地址 jdk官网下载地址:http://jdk.java.net/archive/ 二.安装jdk Mac的JDK都是安装到一个指定目录的:/Library/Java/JavaVirtu ...
- [bzoj1188]分裂游戏
容易发现所有豆子相互独立,只需要考虑每一个豆子的sg函数并异或起来即可,sg函数从后往前暴力即可 1 #include<bits/stdc++.h> 2 using namespace s ...
- [SQL]master..sysprocesses
--https://docs.microsoft.com/zh-cn/sql/relational-databases/system-compatibility-views/sys-sysproces ...
- 配置GitHub和 Gitee共存环境
配置GitHub 和Gitee共存环境 前言 Git共有三个级别的config文件,分别是system.global和local 在当前环境中,分别对应 %GitPath%\mingw64\etc\g ...
- SR4R数据库:水稻4个SNP集的筛选及其应用
目录 前言 四个SNP集 hapmapSNPs tagSNPs fixedSNPs barcodeSNPs hapmapSNPs的指标统计 tagSNPs的群体结构验证 tagSNPs的遗传多样性 t ...
- PHP对称加密-AES加密、DES加密
对称加密 对称加密算法是指,数据发信方将明文(原始数据)和密钥一起经过加密处理后,使其变成复杂的加密密文发送出去.收信方收到密文后,若要解读原文,则需要使用加密密钥及相关算法的逆算法对密文进行解密,使 ...