Python实现的建造者模式示例

yipeiwu_com5年前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读取图片并修改格式与大小的方法

本文实例为大家分享了python读取图片并修改文件大小的具体代码,供大家参考,具体内容如下 # Author:NDK # -*- coding:utf-8 -*- from PIL...

详解程序意外中断自动重启shell脚本(以Python为例)

详解程序意外中断自动重启shell脚本(以Python为例)

我们经常需要在后台运行一些python脚本,来监控系统或者做一些其他事情;但是 由于各种各样的原因,排除python脚本代码的问题,脚本运行过程中会挂掉。为了不天天耗在上面等重启,可以制...

Python下使用Psyco模块优化运行速度

今天介绍下Psyco模块,Psyco模块可以使你的Python程序运行的像C语言一样快。 都说Python语言易用易学,但性能上跟一些编译语言(如C语言)比较要差不少,这里可以用C语言和...

python条件和循环的使用方法

前面已经介绍过几种基本语句(print,import,赋值语句),下面我们来介绍条件语句,循环语句。一. print和import的更多信息1.1 使用逗号输出A.打印多个表达式,用逗号...

Python实现压缩和解压缩ZIP文件的方法分析

本文实例讲述了Python实现压缩和解压缩ZIP文件的方法。分享给大家供大家参考,具体如下: 有时我们需要在 Python 中使用 zip 文件,而在1.6版中,Python 就已经提供...