深入解析Python设计模式编程中建造者模式的使用

yipeiwu_com6年前Python基础

建造者模式:将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。

基本思想
某类产品的构建由很多复杂组件组成;
这些组件中的某些细节不同,构建出的产品表象会略有不同;
通过一个指挥者按照产品的创建步骤来一步步执行产品的创建;
当需要创建不同的产品时,只需要派生一个具体的建造者,重写相应的组件构建方法即可。

代码结构

class Builder(object):
  """基类"""
  def Part1(self):
    # 不同类型的产品,该步骤的细节可能不同
    raise NotImplementedError()

  def Part2(self):
    # 不同类型的产品,该步骤的细节可能不同
    raise NotImplementedError()

class Builder1(Builder):
  """派生类,生产builder1类型的产品"""
  def Part1(self):
    print 'builder1 Part1'

  def Part2(self):
    print 'builder1 Part2'

class Builder2(Builder):
  """派生类,生产builder2类型的产品"""
  def Part1(self):
    print 'builder2 Part1'

  def Part2(self):
    print 'builder2 Part2'

class Director(object):
  """指挥者,负责组织产品的构建过程"""
  def Build(self, builder):
    builder.Part1()
    builder.Part2()

def client():
  director = Director()
  director.Build(Builder1())
  director.Build(Builder2())

这里有一个疑问,指挥者这个角色有什么用呢。感觉除了增加client的调用负担外,似乎没什么用处。为什么不把产品构建过程放在Builder基类中呢,像下面这样:

class Builder(object):
  """基类"""
  def Part1(self):
    raise NotImplementedError()

  def Part2(self):
    raise NotImplementedError()

  def Build(self):
    self.Part1()
    self.Part2()

class Builder1(Builder):
  def Part1(self):
    print 'builder1 Part1'

  def Part2(self):
    print 'builder1 Part2'

class Builder2(Builder):
  def Part1(self):
    print 'builder2 Part1'

  def Part2(self):
    print 'builder2 Part2'

def client():
  Builder1().Build()
  Builder2().Build()

没错,上面就是典型的模板方法模式的实现套路,回顾一下模板方法模式的定义: > 模板方法模式:定义一个工作流或算法的基本骨架,而将一些特定步骤的实现延迟到子类中。

模板方法模式更多的关注于算法流程,而建造者模式更多的关注于复杂对象的创建,模板模式应用场景比建造者模式更多一些,写起来也更自然一些。

类图

201632105858217.gif (622×286)

实例

#encoding=utf-8 
# 
#by panda 
#建造者模式 
 
 
def printInfo(info): 
  print unicode(info, 'utf-8').encode('gbk') 
 
#建造者基类 
class PersonBuilder(): 
  def BuildHead(self): 
    pass 
   
  def BuildBody(self): 
    pass 
   
  def BuildArm(self): 
    pass 
 
  def BuildLeg(self): 
    pass 
   
#胖子 
class PersonFatBuilder(PersonBuilder): 
  type = '胖子' 
  def BuildHead(self): 
    printInfo("构建%s的头" % self.type) 
   
  def BuildBody(self): 
    printInfo("构建%s的身体" % self.type) 
   
  def BuildArm(self): 
    printInfo("构建%s的手" % self.type) 
 
  def BuildLeg(self): 
    printInfo("构建%s的脚" % self.type) 
   
 
#瘦子 
class PersonThinBuilder(PersonBuilder): 
  type = '瘦子' 
  def BuildHead(self): 
    printInfo("构建%s的头" % self.type) 
   
  def BuildBody(self): 
    printInfo("构建%s的身体" % self.type) 
   
  def BuildArm(self): 
    printInfo("构建%s的手" % self.type) 
 
  def BuildLeg(self): 
    printInfo("构建%s的脚" % self.type) 
 
#指挥者 
class PersonDirector(): 
  pb = None; 
  def __init__(self, pb): 
    self.pb = pb 
   
  def CreatePereson(self): 
    self.pb.BuildHead() 
    self.pb.BuildBody() 
    self.pb.BuildArm() 
    self.pb.BuildLeg() 
 
def clientUI(): 
  pb = PersonThinBuilder() 
  pd = PersonDirector(pb) 
  pd.CreatePereson() 
   
  pb = PersonFatBuilder() 
  pd = PersonDirector(pb) 
  pd.CreatePereson() 
  return 
 
 
if __name__ == '__main__': 
  clientUI(); 

相关文章

python脚本实现音频m4a格式转成MP3格式的实例代码

python脚本实现音频m4a格式转成MP3格式的实例代码

前言 群里看到有人询问:谁会用python将微信音频文件后缀m4a格式转成mp3格式,毫不犹豫回了句:我会。 然后就私下聊起来了 解决方法介绍如下: 工具:windows系统,pytho...

如何将 awk 脚本移植到 Python

将一个 awk 脚本移植到 Python 主要在于代码风格而不是转译。 脚本是解决问题的有效方法,而 awk 是编写脚本的出色语言。它特别擅长于简单的文本处理,它可以带你完成配置文件的某...

IntelliJ IDEA安装运行python插件方法

IntelliJ IDEA安装运行python插件方法

IDEA 工具是我们常用的开发工具,全称:IntelliJ IDEA,它的功能强大就在于我们可以添加各种插件来编写不同的代码,当然也可以用来编写python,这篇文章我们来讲解,如何用I...

Python批量查询域名是否被注册过

Python批量查询域名是否被注册过

step1. 找一个单词数据库 这里有一个13万个单词的 http://download.csdn.net/detail/u011004567/9675906 新建个mysql数据库wo...

浅谈pycharm使用及设置方法

浅谈pycharm使用及设置方法

一、Pycharm 是什么? PyCharm是一种PythonIDE,其带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具。 二、pycharm 的安装 1.下载 :&n...