美文网首页
Python创建型模式

Python创建型模式

作者: 汪二小 | 来源:发表于2017-02-18 20:55 被阅读0次

抽象工厂模式

模式特点:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类。
程序实例:提供对不同主题的信件和简历。
     Letter和Resume是两种不同的抽象产品,它们都有Fancy和Modern这两种不同的主题;DocumentCreator是产生Letter和Resume的抽象工厂,根据具体实现(FancyDocumentCreator和ModernDocumentCreator)产生对应的具体的对象(ModernLetter与ModernResume,或者FancyLetter与FancyResume)

class Letter:
    def getInfo(self):
        pass

class Resume:
    def getInfo(self):
        pass

class FancyLetter(Letter):
    def getInfo(self):
        print 'I am FancyLetter'
        
class FancyResume(Resume):
    def getInfo(self):
        print 'I am FancyResume'
        
class ModernLetter(Letter):
    def getInfo(self):
        print 'I am ModernLetter'
        
class ModernResume(Resume):
    def getInfo(self):
        print 'I am ModernResume'

        
class DocumentCreator:
    def createLetter(self):
        pass
    def createResume(self):
        pass

class FancyDocumentCreator(DocumentCreator):
    def createLetter(self):
        temp = FancyLetter()
        return temp
    def createResume(self):
        temp = FancyResume()
        return temp
    
class ModernDocumentCreator(DocumentCreator):
    def createLetter(self):
        temp = ModernLetter()
        return temp
    def createResume(self):
        temp = ModernResume()
        return temp
    

def create(Factory):
    letter = Factory.createLetter()
    resume = Factory.createResume()
    letter.getInfo()
    resume.getInfo()
    
def main():
    create(ModernDocumentCreator())
    create(FancyDocumentCreator())

if __name__ == '__main__':
    main()

建造者模式

模式特点:将一个复杂对象的构建(Director)与它的表示(Builder)分离,使得同样的构建过程可以创建不同的表示(ConcreteBuilder)。

程序实例:“画”出一个四肢健全(头身手腿)的小人

图片.png
class Person:
    def CreateHead(self):
        pass
    def CreateHand(self):
        pass
    def CreateBody(self):
        pass
    def CreateFoot(self):
        pass

class ThinPerson(Person):
    def CreateHead(self):
        print "thin head"
    def CreateHand(self):
        print "thin hand"
    def CreateBody(self):
        print "thin body"
    def CreateFoot(self):
        print "thin foot"

class ThickPerson(Person):
    def CreateHead(self):
        print "thick head"
    def CreateHand(self):
        print "thick hand"
    def CreateBody(self):
        print "thick body"
    def CreateFoot(self):
        print "thick foot"

class Director:
    def __init__(self,temp):
        self.p = temp
    def Create(self):
        self.p.CreateHead()
        self.p.CreateBody()
        self.p.CreateHand()
        self.p.CreateFoot()

if __name__ == "__main__":
    p = ThickPerson()
    d = Director(p)
    d.Create()
建造者模式与工厂模式的区别:
  • 工厂模式一般都是创建一个产品,注重的是把这个产品创建出来就行,只要创建出来,不关心这个 产品的组成部分。从代码上看,工厂模式就是一个方法,用这个方法就能生产出产品。

  • 建造者模式也是创建一个产品,但是不仅要把这个产品创建出来,还要关系这个产品的组成细节, 组成过程。从代码上看,建造者模式在建造产品时,这个产品有很多方法,建造者模式会根据这些相同 方法但是不同执行顺序建造出不同组成细节的产品。

  • 可以比较两个模式的example代码,一比较就会比较出来,<b>工厂模式关心整体,建造者模式关心细节</b>,比如上面代码中我可以创建一个没有手的人,但是工厂模式只关心这个对象的创建。

工厂方法模式

模式特点:定义一个用于创建对象的接口,让子类决定实例化哪一个类。这使得一个类的实例化延迟到其子类。

程序实例:基类键盘类,派生出微软键盘类和罗技键盘类,由这两种子类完成生成键盘工作。子类的创建由键盘工厂的对应的子类完成。

图片.png
class Keyboard:
    def Create(self):
        print " keyboard"

class MicrKeyboard(Keyboard):
    def Create(self):
        print "microsoft keyboard"

class LogiKeyboard(Keyboard):
    def Create(self):
        print "logitech keyboard"

class KeyboardFactory:
    def CreateKeyboard(self):
        temp = KeyBoard()
        return temp

class MicrFactory(KeyboardFactory):
    def CreateKeyboard(self):
        temp = MicrKeyboard()
        return temp

class LogiFactory(KeyboardFactory):
    def CreateKeyboard(self):
        temp = LogiKeyboard()
        return temp

if __name__ == "__main__":
    mf = MicrFactory()
    m=mf.CreateKeyboard()
    m.Create()
    lf = LogiFactory()
    l=lf.CreateKeyboard()
    l.Create()
抽象工厂模式-与-工厂方法模式区别 :
  • 工厂方法模式:<b>一个抽象产品类,可以派生出多个具体产品类。</b> 一个抽象工厂类,可以派生出多个具体工厂类。 每个具体工厂类只能创建一个具体产品类的实例。

  • 抽象工厂模式:<b>多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。</b>一个抽象工厂类,可以派生出多个具体工厂类。 每个具体工厂类可以创建多个具体产品类的实例。

  • 比如罗技A、微软B工厂都生产鼠标,则A,B就是抽象工厂,
    每个工厂生产的鼠标和键盘就是产品,对应于工厂方法。

  • 用了工厂方法模式,你替换生成键盘的工厂方法,就可以把键盘从罗技换到微软。

  • 但是用了抽象工厂模式,你只要换家工厂,就可以同时替换鼠标和键盘一套。如果你要的产品有几十个,当然用抽象工厂模式一次替换全部最方便(这个工厂会替你用相应的工厂方法)

原型模式

模式特点:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

程序实例:从点原型,生成新的点
Python中有7中方法实现

图片.png
import copy

class Point:
    __slots__ = ("x","y")
    
    def __init__(self,x,y):
        self.x = x
        self.y = y
        
de make_object(Class, *args, **kwargs):
    return Class(*args, **kwargs)

if __name__ = '__main__':
    point1 = Point(1,2)
    point2 = eval("{}({},{})".format("Point", 2, 4))
    point3 = getattr()["Point"](3,6)
    point4 = globals()["Point"](4,8)
    point5 = make_object(5,10)
    point6 = copy.deepcopy(point5)
    point6.x = 6
    point6.y = 12
    point7 = point1.__class__(7, 14)
单例模式

模式特点:保证类仅有一个实例,并提供一个访问它的全局访问点。

说明: 为了实现单例模式费了不少工夫,后来查到一篇博文对此有很详细的介绍,而且实现方式也很丰富,通过对代码的学习可以了解更多Python的用法。以下的代码出自GhostFromHeaven的专栏,地址:http://blog.csdn.net/ghostfromheaven/article/details/7671853。不过正如其作者在Python单例模式终极版所说:

我要问的是,Python真的需要单例模式吗?我指像其他编程语言中的单例模式。

答案是:不需要!

因为,Python有模块(module),最pythonic的单例典范。

模块在在一个应用程序中只有一份,它本身就是单例的,将你所需要的属性和方法,直接暴露在模块中变成模块的全局变量和方法即可!

相关文章

  • Python设计模式之工厂模式

    工厂模式 创建型设计模式处理对象创建相关的问题,目标是当直接创建对象(在Python中是通过init()函数实现的...

  • 设计模式 - 创建型模式(工厂模式)

    简介 创建型设计模式处理对象创建相关的问题,目标是当直接创建对象(Python中是通过__init__()函数实现...

  • 23种设计模式总结一

    23 种经典设计模式共分为 3 种类型,分别是创建型、结构型和行为型。 一、创建型设计模式 创建型设计模式包括:单...

  • 设计模式之工厂模式

    设计模式中主要分为三大类:创建型、结构型、行为型 工厂模式属于创建型,顾名思义,创建型模式关注对象的创建过程,它将...

  • 创建型设计模式总结

    创建型设计模式总结 Intro 前面几篇文章已经把创建型设计模式都介绍了,来做一个简单的总结。 创建型设计模式,就...

  • 设计模式--分类

    一、设计模式的分类设计模式可以概括为23种,按照特点可以将其分为三大类型:创建型、结构型、行为型。1、创建型创建型...

  • 《设计模式之美》- 23种设计模式

    学习《设计模式之美》笔记。 23 种经典设计模式共分为 3 种类型,分别是创建型、结构型和行为型 创建型模式 创建...

  • 设计模式简单总结(待完善)

    设计模式简单总结 设计模式可以分为:创建型,结构型,行为型三种模式。 1 创建型模式 1.1 单例模式 用来指定某...

  • 手绘设计模式结构图

    GoF的设计模式一共23个,可以分为3大类:创建型、结构型和行为型,这篇文章主要讨论创建型。 创建型的设计模式包括...

  • 设计模式(行为型)-- 观察者模式

    我们常把 23 种经典的设计模式分为三类:创建型、结构型、行为型。创建型设计模式主要解决“对象的创建”问题,结构型...

网友评论

      本文标题:Python创建型模式

      本文链接:https://www.haomeiwen.com/subject/pxrlwttx.html