建造者模式:
將乙個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示。
**:
#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 建造者模式
usr bin python coding utf 8 建造者基類 class personbuilder def buildhead self pass def buildbody self pass def buildarm self pass def buildleg self pass 胖子...
Python設計模式 建造者模式
需求,畫人物,要求畫乙個人的頭,左手,右手,左腳,右腳和身體,畫乙個瘦子,乙個胖子 不使用設計模式 encoding utf 8 author kevinlu1010 qq.com if name name print 畫左手 print 畫右手 print 畫左腳 print 畫右腳 print ...
python設計模式 建造者模式
一 什麼是建造者模式我們想要建立乙個由多個部分構成的物件,而且它的構成需要一步接一步地完成。只有當各個部分都建立好,這個物件才算是完整的。這正是建造者設計模式。二 工廠模式和建造者模式的區別 三 應用案例引入了乙個建造者computerbuilder 乙個指揮者hardwareengineer以及 ...