设计模式六(建造者模式,采用python实现)

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值