深入解析Python設計模式編程中建造者模式的使用
建造者模式:將一個復雜對象的構建與他的表示分離,使得同樣的構建過程可以創(chuàng)建不同的表示。
基本思想
某類產(chǎn)品的構建由很多復雜組件組成;
這些組件中的某些細節(jié)不同,構建出的產(chǎn)品表象會略有不同;
通過一個指揮者按照產(chǎn)品的創(chuàng)建步驟來一步步執(zhí)行產(chǎn)品的創(chuàng)建;
當需要創(chuàng)建不同的產(chǎn)品時,只需要派生一個具體的建造者,重寫相應的組件構建方法即可。
代碼結構
class Builder(object):
"""基類"""
def Part1(self):
# 不同類型的產(chǎn)品,該步驟的細節(jié)可能不同
raise NotImplementedError()
def Part2(self):
# 不同類型的產(chǎn)品,該步驟的細節(jié)可能不同
raise NotImplementedError()
class Builder1(Builder):
"""派生類,生產(chǎn)builder1類型的產(chǎn)品"""
def Part1(self):
print 'builder1 Part1'
def Part2(self):
print 'builder1 Part2'
class Builder2(Builder):
"""派生類,生產(chǎn)builder2類型的產(chǎn)品"""
def Part1(self):
print 'builder2 Part1'
def Part2(self):
print 'builder2 Part2'
class Director(object):
"""指揮者,負責組織產(chǎn)品的構建過程"""
def Build(self, builder):
builder.Part1()
builder.Part2()
def client():
director = Director()
director.Build(Builder1())
director.Build(Builder2())
這里有一個疑問,指揮者這個角色有什么用呢。感覺除了增加client的調用負擔外,似乎沒什么用處。為什么不把產(chǎn)品構建過程放在Builder基類中呢,像下面這樣:
class Builder(object):
"""基類"""
def Part1(self):
raise NotImplementedError()
def Part2(self):
raise NotImplementedError()
def Build(self):
self.Part1()
self.Part2()
class Builder1(Builder):
def Part1(self):
print 'builder1 Part1'
def Part2(self):
print 'builder1 Part2'
class Builder2(Builder):
def Part1(self):
print 'builder2 Part1'
def Part2(self):
print 'builder2 Part2'
def client():
Builder1().Build()
Builder2().Build()
沒錯,上面就是典型的模板方法模式的實現(xiàn)套路,回顧一下模板方法模式的定義: > 模板方法模式:定義一個工作流或算法的基本骨架,而將一些特定步驟的實現(xiàn)延遲到子類中。
模板方法模式更多的關注于算法流程,而建造者模式更多的關注于復雜對象的創(chuàng)建,模板模式應用場景比建造者模式更多一些,寫起來也更自然一些。
類圖

實例
#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實現(xiàn)采用進度條實時顯示處理進度的方法
這篇文章主要介紹了Python實現(xiàn)采用進度條實時顯示處理進度的方法,涉及Python數(shù)學運算結合時間函數(shù)顯示進度效果的相關操作技巧,需要的朋友可以參考下2017-12-12
python中計算一個列表中連續(xù)相同的元素個數(shù)方法
今天小編就為大家分享一篇python中計算一個列表中連續(xù)相同的元素個數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
使用Python中的pytesseract模塊實現(xiàn)抓取圖片中文字
最近同事用網(wǎng)上提供掃描軟件進行掃描識別文字,每天上線只能夠做兩次掃描,請求我研發(fā)一個小工具幫助解決識別圖片的中文字,最終我選擇使用pytesseract模塊可以解決這個需求問題,本文給大家分享實現(xiàn)代碼操作感興趣的朋友跟隨小編一起看看吧2022-11-11

