脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python实现的建造者模式示例

(编辑:jimmy 日期: 2025/9/18 浏览:3 次 )

本文实例讲述了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实现购物评论文本情感分析操作【基于中文文本挖掘库snownlp】
下一篇:Django中日期处理注意事项与自定义时间格式转换详解
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap