python创建模式对象_Python创建型模式

抽象工厂模式

模式特点:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类。

程序实例:提供对不同主题的信件和简历。

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

工厂方法模式

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

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

图片.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()

抽象工厂模式-与-工厂方法模式区别 :

工厂方法模式:一个抽象产品类,可以派生出多个具体产品类。 一个抽象工厂类,可以派生出多个具体工厂类。 每个具体工厂类只能创建一个具体产品类的实例。

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

比如罗技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的单例典范。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值