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程序设计有所帮助。

相关文章

Window 64位下python3.6.2环境搭建图文教程

Window 64位下python3.6.2环境搭建图文教程

python3.6.2环境安装配置图文教程,具体如下 一、需要下载的软件 》python3.6.2.exe (也可以选择更新的版本) ---- -网址 》Anaconda3-4.4.0...

python获取文件真实链接的方法,针对于302返回码

使用模块requests 方式代码如下: import requests url_string="https://******" r = requests.head(url_st...

python opencv实现切变换 不裁减图片

python opencv实现切变换 不裁减图片

本文实例为大家分享了python opencv实现切变换的具体代码,供大家参考,具体内容如下 # -*- coding:gb2312 -*- import cv2 from math...

Python中的集合介绍

Python中的集合介绍

1.集合的定义 集合的元素是不可重复的 s = {1,2,3,1,2,3,4,5} print(s) print(type(s)) s1 = {1} print(s1) print(...

如何在mac环境中用python处理protobuf

这篇文章主要介绍了如何在mac环境中用python处理protobuf,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 br...