Python設計模式-建造者模式
程式碼基於3.5.2,程式碼如下;
class Burger():
name = ""
price = 0.0
def getPrice(self):
return self.price
def setPrice(self,price):
self.price = price
def getName(self):
return self.name
class cheeseBurger(Burger):
def __init__(self):
self.name = "cheese burger"
self.price = 10.0
class spicyChickenBurger(Burger):
def __init__(self):
self.name = "spicy chicken burger"
self.price = 15.0
class Snack():
name = ""
price = 0.0
type = "SNACK"
def getPrice(self):
return self.price
def setPrice(self,price):
self.price = price
def getName(self):
return self.name
class chips(Snack):
def __init__(self):
self.name = "chips"
self.price = 6.0
class chickenWings(Snack):
def __init__(self):
self.name = "chicken wings"
self.price = 12.0
class Beverage():
name = ""
price = 0.0
type = "BEVERAGE"
def getPrice(self):
return self.price
def setPrice(self, price):
self.price = price
def getName(self):
return self.name
class coke(Beverage):
def __init__(self):
self.name = "coke"
self.price = 4.0
class milk(Beverage):
def __init__(self):
self.name = "milk"
self.price = 5.0
class order():
burger = ""
snack = ""
beverage = ""
def __init__(self,orderBuilder):
self.burger = orderBuilder.bBurger
self.snack = orderBuilder.bSnack
self.beverage = orderBuilder.bBeverage
def show(self):
print("Burger:%s"%self.burger.getName())
print("Snack:%s"%self.snack.getName())
print("Beverage:%s"%self.beverage.getName())
class orderBuilder():
bBurger = ""
bSnack = ""
bBeverage = ""
def addBurger(self,xBurger):
self.bBurger = xBurger
def addSnack(self, xSnack):
self.bSnack = xSnack
def addBeverage(self, xBeverage):
self.bBeverage = xBeverage
def build(self):
return order(self)
if __name__ == "__main__":
order_builder = orderBuilder()
order_builder.addBurger(spicyChickenBurger())
order_builder.addSnack(chips())
order_builder.addBeverage(milk())
order_builder.build().show()
建造者模式分析與解讀
建造者模式
建造者模式,將一個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示,作用是將"構建"和"表示"分離,以達到解耦的作用。
程式碼解讀
該例子基於的需求:客戶點餐系統。
1、先定義了Burger類,所有的主餐都是由該類繼承而來(cheeseBurger,spicyChickenBurger),定義了Snack類,所有的小吃類都是由該類繼承而來(chips,chickenWings),定義了Beverage類,所有的飲料都是由該類繼承而來(coke,milk);
2、定義了order類,該類主要生成訂單;
3、定義了orderBuilder訂單建造者類,分別實現了新增主食addBurger、新增小吃addSnack和新增飲料addBeverage方法,當新增完成後呼叫build方法來生成訂單,從而達到建造訂單的目的。
程式碼執行結果如下:
Burger:spicy chicken burger
Snack:chips
Beverage:milk
建造者模式應用場景:
1、目標物件由元件構成的場景中,適合建造者模式;
2、在具體場景中,物件內部介面需要根據不同的引數而呼叫順序有所不同時,可以使用建造者模式。
優缺點分析
優點
1、封裝性好,使用者可以不知道物件的內部構造和細節,就可以直接建造物件;
2、系統擴充套件容易;
3、建造者模式易於使用,非常靈活,在構造性的場景中很容易實現"流水線";
4、便於控制細節。
缺點
1、封裝的過程對使用者不透明。
備註
class product():
def __init__(self):
self.partsList = []
def add(self,part):
self.partsList.append(part)
def show(self):
for part in self.partsList:
print(part)
class builder():
def buildPartA(self):
raise NotImplementedError
def buildPartB(self):
raise NotImplementedError
def getResult(self):
raise NotImplementedError
class ConcreteBuilder1(builder):
def __init__(self):
self.product = product()
def buildPartA(self):
self.product.add("1 A")
def buildPartB(self):
self.product.add("1 B")
def getResult(self):
self.product.show()
class ConcreteBuilder2(builder):
def __init__(self):
self.product = product()
def buildPartA(self):
self.product.add("2 A")
def buildPartB(self):
self.product.add("2 B")
def getResult(self):
self.product.show()
class director():
def construct(self,builder):
builder.buildPartA()
builder.buildPartB()
if __name__ == "__main__":
de = director()
b1 = ConcreteBuilder1()
b2 = ConcreteBuilder2()
de.construct(b1)
b1.getResult()
de.construct(b2)
b2.getResult()