【编程思想】【设计模式】【创建模式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)——规范建造方法与结果 ...
随机推荐
- @RequestAttribute与@MatrixVariable
@RequestAttribute 它只能使用在方法入参上,从request请求参数中获取对应的属性值. //路径跳转 @GetMapping("/goto") public St ...
- 环境(6)Linux文件系统二
一:计算机间的数据传输 windows---linux : lrzsz :需要手动安装 yum install lrzsz -y ; rz 将文件从window上传到linux : ...
- 美妙绝伦面向node引用-zico图标(逐浪矢量全真图标)1.9发布
15年前,那个农村小伙初入广告行业被讥笑没有审美 于是他狠下决心,积极研发,缔就技术之核, 再后来,那些PPT和美工er们随便怎么自好,无法让其心怵. 因为他是中华人民共和国唯一具备web.cms.o ...
- SCTL 涅槃重生:投入 RAL 的怀抱
在<DistSQL:像数据库一样使用 Apache ShardingSphere>一文中,PMC 孟浩然为大家介绍了 DistSQL 的设计初衷和语法体系,并通过实战操作展示了一条 SQL ...
- 重写(Override)与重载(Overload)区别
重写是子类对父类的允许访问的方法的实现过程进行重新编写. 方法重写三要素: (1)方法名形参列表相同: (2)返回值类型和声明异常类型子类小于父类: (3)访问权限,子类大于等于父类. 重写的好处在于 ...
- [loj3179]视觉程序
暴力做法:1.对每一行/列求$or$:2.枚举行的差值$i$,并对任意相差为$i$的行和相差为$k-i$的列求$and$,对行/列的$and$结果求$or$,对行和列的$or$求$and$,对所有$i ...
- [loj4]Quine
很有趣的一道题目,如何让一个程序输出自身如果用字符串s表示程序,那么意味着可以通过s来输出sprintf是一个可以利用的函数,相当于要求printf(s,s)输出的就是s那么只需要在s中加入%c和%d ...
- maven私服-账号管理
关于maven私服的相关数据: 默认账号admin,密码:admin123 这个账号是不能进行上传代码 到maven仓库的. 我们光是有admin和匿名账号是不够的,我们需要创建一个专门用来部署的账号 ...
- Python+selenium 之xpath定位
- 柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
1.关于柯基数据 南京柯基数据科技有限公司成立于2015年,提供一站式全生命周期知识图谱构建和运维.智能应用服务,致力于"链接海量数据,从大数据中挖掘智慧".帮助企业运用知识 ...