基本说明同 “设计模式五”,这里直接给实例。可以看出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])