深入解析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的調(diào)用負擔外,似乎沒什么用處。為什么不把產(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();
相關文章
網(wǎng)易2016研發(fā)工程師編程題 獎學金(python)
這篇文章主要為大家詳細介紹了網(wǎng)易2016研發(fā)工程師編程題:獎學金(python),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06
Pytorch模型遷移和遷移學習,導入部分模型參數(shù)的操作
這篇文章主要介紹了Pytorch模型遷移和遷移學習,導入部分模型參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03

