PPT_python设计模式_建造者工厂模式和抽象工厂模式

         PPT_python设计模式_建造者工厂模式和抽象工厂模式

1、抽象工厂模式可以将建造者模式与工厂模式合一

       i  工厂模式是 定义一个factory  ,由用户输入生产条件,然后产出产品       factory 封装的产品类

      ii  建造者 是定义一个建造者,由用户输入要准备产出的类对象, 然后建造者开始生产。  建造者接收类对象。

      iii  抽象工厂模式 是对工厂模式更高集成,解决当有多个工厂的场景。

                                 先用工厂模式选择工厂类

                                 再用建造者模式 将产品类生产出来。

            抽象工厂模式

                     抽象工厂类 

        工厂类1    工厂类2   工厂类3

产品类1  产品类2 

   

2、具体例子   工厂模式

#-*- coding:utf-8 -*-
#! /usr/bin/env python

class Catproduct(object):
    def run(self):
        print "miaomiao"
class Dogproduct(object):
    def run(self):
        print "wangwang"

class Factory(object):
    def __init__(self):
        self.productdict = {"cat":Catproduct(),"dog":Dogproduct()}
    def produce(self,order):
        if self.productdict.get(order,None):
            self.productdict.get(order).run()
        return self.productdict.get(order,None)

factory = Factory()
cat = factory.produce("cat")
dog = factory.produce("dog")
print type(cat)

  工厂生产产品的菜单在工厂的生产方法中,依照输入的字符串进行索引返回生产好的类对象。

    建造者模式

     建造者将各个物品类对象传入自己的类中,然后编排物品的建造过程在自己的建造方法中。

#-*- coding:utf-8 -*-
#! /usr/bin/env python
class Catproduct(object):
    typename = 'cat'
    def buildHead(self):
        print "build %s hear"%(self.typename)
    def buildBody(self):
        print "build %s body"%(self.typename)
    def run(self):
        print "miaomiao"
class Dogproduct(object):
    typename = 'dog'
    def buildHead(self):
        print "build %s hear"%(self.typename)
    def buildBody(self):
        print "build %s body"%(self.typename)
    def run(self):
        print "wangwang"

class Builder(object):
    def __init__(self,org):
        self.inerorg = org
    def startBuild(self):
        self.inerorg.buildHead()
        self.inerorg.buildBody()
        self.inerorg.run()
cat = Catproduct()
dog = Dogproduct()

catbuild = Builder(cat)
dogbuild = Builder(dog)
catbuild.startBuild()
dogbuild.startBuild()

3、抽象工厂

     抽象工厂模式为工厂模式和建造者模式的组合

     

     

#-*- coding:utf-8 -*-
#! /usr/bin/env python

class WhiteCatproduct(object):
    typename = 'whitecat'
    def buildHead(self):
        print "build %s hear"%(self.typename)
    def buildBody(self):
        print "build %s body"%(self.typename)
    def run(self):
        print "miaomiao"
class BlackCatproduct(object):
    typename = 'blackcat'
    def buildHead(self):
        print "build %s hear"%(self.typename)
    def buildBody(self):
        print "build %s body"%(self.typename)
    def run(self):
        print "miaomiao"

class ZhangDogproduct(object):
    typename = 'zhangao'
    def buildHead(self):
        print "build %s hear"%(self.typename)
    def buildBody(self):
        print "build %s body"%(self.typename)
    def run(self):
        print "wangwang"
class SongshiDogproduct(object):
    typename = 'songshi'
    def buildHead(self):
        print "build %s hear"%(self.typename)
    def buildBody(self):
        print "build %s body"%(self.typename)
    def run(self):
        print "wangwang"

class CatFactory(object):
    def __init__(self,org):
        self.inerorg = org
    def startBuild(self):
        self.inerorg.buildHead()
        self.inerorg.buildBody()
        self.inerorg.run()
class DogFactory(object):
    def __init__(self,org):
        self.inerorg = org
    def startBuild(self):
        self.inerorg.buildHead()
        self.inerorg.buildBody()
        self.inerorg.run()
# class Builder(object):
#     def __init__(self,org):
#         self.inerorg = org
#     def startBuild(self):
#         self.inerorg.buildHead()
#         self.inerorg.buildBody()
#         self.inerorg.run()
class AbstractFactory(object):
    def __init__(self):
        self.factorydict = {"catfactory":CatFactory,"dogfactory":DogFactory}
    def abtPro(self,order):
        if self.factorydict.get(order,None):
            return self.factorydict.get(order,None)

abtfactory = AbstractFactory()

#--------------create a white cat
whitecatproduct = WhiteCatproduct()
catfactory = abtfactory.abtPro('catfactory')
whitecatfactory = catfactory(whitecatproduct)

whitecatfactory.startBuild()

 经典例子 http://www.jb51.net/article/91233.htm

  

  

posted on 2018-07-21 19:50  lexn  阅读(220)  评论(0编辑  收藏  举报

导航