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

  1. 设计模式的征途—6.建造者(Builder)模式

    建造者模式又称为生成器模式,它是一种较为复杂.使用频率也相对较低的创建型模式.建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品.因为,没有人买车会只买一个方向盘或者轮胎,大 ...

  2. 设计模式(4)建造者模式/生成器模式(Builder)

    设计模式(0)简单工厂模式 设计模式(1)单例模式(Singleton) 设计模式(2)工厂方法模式(Factory Method) 设计模式(3)抽象工厂模式(Abstract Factory) 源 ...

  3. Java设计模式(3)建造者模式(Builder模式)

    Builder模式定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构 ...

  4. 【设计模式】 模式PK:工厂模式VS建造者模式

    1.概述 工厂方法模式注重的是整体对象的创建方法,而建造者模式注重的是部件构建的过程,旨在通过一步一步地精确构造创建出一个复杂的对象.我们举个简单例子来说明两者的差异,如要制造一个超人,如果使用工厂方 ...

  5. 【设计模式】 模式PK:抽象工厂模式VS建造者模式

    1.概述 抽象工厂模式实现对产品家族的创建,一个产品家族是这样的一系列产品:具有不同分类维度的产品组合,采用抽象工厂模式则是不需要关心构建过程,只关心什么产品由什么工厂生产即可.而建造者模式则是要求按 ...

  6. 设计模式(六)——建造者模式(源码StringBuilder分析)

    建造者模式 1 盖房项目需求 1) 需要建房子:这一过程为打桩.砌墙.封顶 2) 房子有各种各样的,比如普通房,高楼,别墅,各种房子的过程虽然一样,但是要求不要相同的. 3) 请编写程序,完成需求. ...

  7. java设计模式(五)--建造者模式(Builder)

    转载:http://zz563143188.iteye.com/blog/1847029 工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,所谓复合对象 ...

  8. 【设计模式 - 3】之建造者模式(Builder)

    1      模式简介 建造者模式也叫生成器模式,和抽象工厂模式相似,也是一种构建复杂对象的模式. 建造者模式中的角色分类: 抽象建造者Builder:接口类型,用于规范各个产品的组成部分: 具体建造 ...

  9. 2015-03-12---外观模式,建造者模式(附代码),观察者模式(附代码),boost库应用

    今天白天主要看了boost库的应用,主要是经常使用的一些库,array,bind,function,regex,thread,unordered,ref,smartpointers库,晚上看了看设计模 ...

  10. Java设计模式(5)——创建型模式之建造者模式(Builder)

    一.概述 概念 将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示.(与工厂类不同的是它用于创建复合对象) UML图   主要角色 抽象建造者(Builder)——规范建造方法与结果 ...

随机推荐

  1. React + 导入模块的一个错误

    导入模块的时候出现这个错误: Attempted import error: 'd3' does not contain a default export (imported as 'd3'). 把导 ...

  2. nio实现文件夹内容的监听

    参考的博客 package com.jp.filemonitor; import java.io.IOException; import java.nio.file.FileSystems; impo ...

  3. 蓝图before request

    方法1 @bp.before_request def test(): print("test") 方法2 def bp_before_request(): print(test) ...

  4. 菜鸡的Java笔记

    1.注释 在JAVA中对于注释有三种: 单行注释:// 多行注释:/*--*/ 文档注释:/**--*/ 2.关键字和标识符 在程序中描述类名称,方法名称,变量等概念就需要使用标识符来定义.而在JAV ...

  5. [cf1444D]Rectangular Polyline

    由于两种线段要交替出现,有解的必要条件即为$h=v$(以下均记为$n$) 进一步的,再假设两种线段依次对应于向量$(a_{i},0)$和$(0,b_{i})$,根据题意要求向量长度为给定值且和为0,那 ...

  6. [cf1178G]The Awesomest Vertex

    2020年论文题,这里给出了一个$o(n\log^{2}n+m\log^{3}n)$的做法,例题3即为原题 1.例题1 题面 给定$n$个一次函数$f_{i}(x)$,$m$次查询$F(x)=\max ...

  7. [bzoj3123]森林

    首先对于询问操作可以使用可持久化线段树来维护,对于连边操作对于两颗树中选取较小的树暴力练到另一个点上,点数可以用并查集然后只修改根的点数即可. 1 #include<bits/stdc++.h& ...

  8. [noi110]翘课

    发现加边操作不好处理,因此考虑先加完所有边后删边. 删去一对边x到y,如果两者中有一个不翘课显然没有意义,那么如果都翘课了那么就对他们进行判断,如果无法翘课就继续搜下去. 这样的时间复杂度看上去似乎是 ...

  9. [bzoj1415]聪聪与可可

    直接求出任意两点的距离后记忆化搜索,用f[i][j]表示聪聪在i,可可在j的期望步数,由于i和j的最短路单调递减,所以搜不到环 1 #include<bits/stdc++.h> 2 us ...

  10. [bzoj2257]瓶子和燃料

    先考虑选出k个后答案最小会是多少,容易发现其实就是所有的gcd(就是$ax+by=gcd(a,b)$的推广)然后相当于要最大化gcd,反过来可以将所有数的约数都打上+1标记,+1标记不少于k个且最大的 ...