Python实现的建造者模式示例

yipeiwu_com6年前Python基础

本文实例讲述了Python实现的建造者模式。分享给大家供大家参考,具体如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-
#建造者基类
class PersonBuilder():
  def BuildHead(self):
    pass
  def BuildBody(self):
    pass
  def BuildArm(self):
    pass
  def BuildLeg(self):
    pass
#胖子
class PersonFatBuilder(PersonBuilder):
  type = u'胖子'
  def BuildHead(self):
    print u'构建%s的头' % self.type
  def BuildBody(self):
    print u'构建%s的身体' % self.type
  def BuildArm(self):
    print u'构建%s的手' % self.type
  def BuildLeg(self):
    print u'构建%s的脚' % self.type
#瘦子
class PersonThinBuilder(PersonBuilder):
  type = u'瘦子'
  def BuildHead(self):
    print u'构建%s的头' % self.type
  def BuildBody(self):
    print u'构建%s的身体' % self.type
  def BuildArm(self):
    print u'构建%s的手' % self.type
  def BuildLeg(self):
    print u'构建%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相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python自定义一个类实现字典dict功能的方法

如下所示: import collections class Mydict(collections.UserDict): def __missing__(self, ke...

python中合并两个文本文件并按照姓名首字母排序的例子

前段时间前在网上看到一段面试题,要求如下: employee文件中记录了工号和姓名复制代码 代码如下:    cat employee.txt: ...

在Python中marshal对象序列化的相关知识

有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上。Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle,...

Python带动态参数功能的sqlite工具类

本文实例讲述了Python带动态参数功能的sqlite工具类。分享给大家供大家参考,具体如下: 最近在弄sqlite和python 在网上参考各教程后,结合以往java jdbc数据库工...

python使用writerows写csv文件产生多余空行的处理方法

初次接触python,学艺不精,第一次实战写一个文本处理的小程序时便遇到了头疼的问题。 先看代码: 生成的.CSV文件每两行之间都会多出一行空格(如下图),具体原因可参看点击打开链接...