首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python实现工厂模式的两个例子

设计模式在Java里面这个是必须的中高阶内容。而很少看到Python里面刻意去讲这个,关于Python实现的设计模式,一直以来是自己比较好奇而且想深入学习的一个点。

需要吐槽的是自己买了本纸质书,按照书名是可以精通Python设计模式了,里面有流程图,有代码,部分还有例子和执行结果。但是自己看起来总是感觉有些吃力,不是完全技术层面的困难,而是理解上的困难。

而换个思路,看看国内的一些朋友写的一些设计模式的总结,一看就懂。我都纳闷是不是文化上的差异导致的。

这个比较清晰。可以关注下。

https://github.com/w392807287/Design_pattern_of_python

这个的star都超过14000个了。

https://github.com/faif/python-patterns/blob/master/creational/borg.py

我们先来简单看下工厂模式

如下是工厂方法的实现,里面用到了字典来做键值的映射。

#!/usr/bin/python

# coding:utf8

'''

Factory Method

'''

classChinaGetter:

"""A simple localizer a la gettext"""

def__init__(self):

self.trans =dict(jianrong=u"杨建荣",jianrong_notes=u"学习笔记")

defget(self,msgid):

"""We'll punt if we don't have a translation"""

try:

returnself.trans[msgid]

exceptKeyError:

returnstr(msgid)

classEnglishGetter:

"""Simply echoes the msg ids"""

def__init__(self):

self.trans =dict(jeanron100=u"jeanron100...",mysql=u"MySQL",oracle=u"Oracle")

defget(self,msgid):

try:

returnself.trans[msgid]

exceptKeyError:

returnstr(msgid)

defget_localizer(language="English"):

"""The factory method"""

languages =dict(English=EnglishGetter,China=ChinaGetter)

returnlanguages[language]()

# Create our localizers

e,g = get_localizer("English"),get_localizer("China")

# Localize some text

formsgidin"jeanron100 jianrong mysql oracle".split():

print(e.get(msgid),g.get(msgid))

执行结果如下:

(u'jeanron100...', 'jeanron100')

('jianrong', u'\u6768\u5efa\u8363')

(u'MySQL', 'mysql')

(u'Oracle', 'oracle')

而使用抽象工厂,使用了random来做一个动态匹配。这个例子参考了开篇的第一个链接的例子,里面的getFactory方法会随机得到一个工厂的实例化对象,而对于应用来说这个匹配的过程是透明的。

#!/usr/bin/python

# coding:utf8

'''

Abstract Factory

'''

importrandom

classPetShop:

"""A pet shop"""

def__init__(self,animal_factory=None):

"""pet_factory is our abstract factory.

We can set it at will."""

self.pet_factory = animal_factory

defshow_pet(self):

"""Creates and shows a pet using the

abstract factory"""

pet =self.pet_factory.get_pet()

print("This is a lovely",str(pet))

print("It says",pet.speak())

print("It eats",self.pet_factory.get_food())

# Stuff that our factory makes

classDog:

defspeak(self):

return"woof"

def__str__(self):

return"Dog"

classCat:

defspeak(self):

return"meow"

def__str__(self):

return"Cat"

# Factory classes

classDogFactory:

defget_pet(self):

returnDog()

defget_food(self):

return"dog food"

classCatFactory:

defget_pet(self):

returnCat()

defget_food(self):

return"cat food"

# Create the proper family

defget_factory():

"""Let's be dynamic!"""

returnrandom.choice([DogFactory,CatFactory])()

# Show pets with various factories

if__name__ =="__main__":

shop = PetShop()

foriinrange(3):

shop.pet_factory = get_factory()

shop.show_pet()

print("="*20)

#print random.choice([1,2,3,4,5])

执行结果如下:

('This is a lovely', 'Cat')

('It says', 'meow')

('It eats', 'cat food')

====================

('This is a lovely', 'Cat')

('It says', 'meow')

('It eats', 'cat food')

====================

('This is a lovely', 'Dog')

('It says', 'woof')

('It eats', 'dog food')

====================

有了这些铺垫,对于Python设计模式的实现就有了一个初步的认识和理解,后续做一些改进就可以循序渐进了。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180302B0016500?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券