设计模式六(建造者模式,采用python实现)
2012-12-10 15:45
453 查看
基本说明同 “设计模式五”,这里直接给实例。可以看出python语言的某种魅力
代码:
代码:
####################################################### # # codes.py # Python implementation of the Class Builder # Generated by Enterprise Architect # Created on: 10-十二月-2012 15:10:15 # ####################################################### from __future__ import division from __future__ import print_function from __future__ import unicode_literals from future_builtins import * import sys class Builder(object): """This class specifies an abstract interface for creating parts of a Product object. """ def __init__(self): super(Builder,self).__init__() pass def Builder(self): pass def BuildPart(self): pass class ConcreteBuilderA(Builder): """This class (a) constructs and assembles parts of the product by implementing the Builder interface, (b) defines and keeps track of the representation it creates, and (c) provides an interface for retrieving the product. """ def __init__(self): super(ConcreteBuilderA,self).__init__() self.product=Product() pass def AddBody(self): str='get body from export depot' self.product.Add(str) pass def AddCase(self): str='get case from export depot' self.product.Add(str) pass def BuildPart(self): self.AddBody(); self.AddCase(); self.check() pass def check(self): str='pass for export market' self.product.Add(str) pass def GetResult(self): return self.product pass class ConcreteBuilderB(Builder): """This class (a) constructs and assembles parts of the product by implementing the Builder interface, (b) defines and keeps track of the representation it creates, and (c) provides an interface for retrieving the product. """ def __init__(self): super(ConcreteBuilderB,self).__init__() self.product=Product() pass def AddBody(self): str='get body from domestic depot' self.product.Add(str) pass def AddCase(self): str='get case from domestic depot' self.product.Add(str) pass def BuildPart(self): self.AddBody(); self.AddCase(); self.check() pass def check(self): str='pass for domestic market' self.product.Add(str) pass def GetResult(self): return self.product pass class Director(object): """This class constructs an object using the Builder interface. """ m_Builder= Builder() def __init__(self): super(Director,self).__init__() pass def Construct(self,builder): # for all objects in structure { # builder->BuildPart() # } self.m_Builder=builder self.m_Builder.BuildPart() pass class Product(object): """This class (a) represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled, and (b) includes classes that define the constituent parts, including interfaces for assembling the parts into the final result. """ def __init__(self): super(Product,self).__init__() self.log=list() def Add(self,str): self.log.append(str) pass def getLog(self): return self.log pass # 客户端: if (__name__=="__main__"): builder=ConcreteBuilderA() director=Director(); director.Construct(builder) product=builder.GetResult() log=product.getLog() for i in xrange(len(log)): print(log[i])
相关文章推荐
- 设计模式四(装饰模式,采用python 实现)
- 设计模式一(简单工厂,采用python实现)
- 设计模式二(策略模式,采用python实现)
- 设计模式五(建造者模式,采用C++实现)
- 流畅的python 第六章: 使用一等函数实现设计模式
- 全面解析设计模式中的建造者模式及相关C++实现
- Python实现设计模式--01.单例模式(Singleton Pattern)
- 设计模式C++实现(6)——建造者模式
- 23种设计模式之python实现--Iterator模式
- [python实现设计模式]-4.观察者模式-吃食啦!
- Python的创建型设计模式之建造者模式
- Python设计模式——建造者模式
- [python实现设计模式]-5.迭代器模式-一起撸串嗨皮啦
- 设计模式之建造者模式(C++实现)
- 二十三种设计模式及其python实现
- 23种设计模式之python实现--Memento模式
- 设计模式之单例模式与工厂模式的Python实现(一)
- Python设计模式-建造者模式
- 设计模式之python版(九)--建造者模式
- 设计模式--单例模式python实现