Python版

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

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
*What is this pattern about? In Java and other languages, the Abstract Factory Pattern serves to provide an interface for
creating related/dependent objects without need to specify their
actual class.
>>在Java和其他语言中,抽象工厂模式为创建相关联的对象提供借口,而不用制定他们的实体类 The idea is to abstract the creation of objects depending on business
logic, platform choice, etc.
>>这是为了抽象创建基于业务逻辑和用户选择而创建的 In Python, the interface we use is simply a callable, which is "builtin" interface
in Python, and in normal circumstances we can simply use the class itself as
that callable, because classes are first class objects in Python.
>>在python中,我们使用的接口是可调用的,是一个可创建的借口
>>在python中,通常情况下,我们只使用类本身作为可调用的接口,因为类是第一个对象 *What does this example do?
This particular implementation abstracts the creation of a pet and
does so depending on the factory we chose (Dog or Cat, or random_animal)
This works because both Dog/Cat and random_animal respect a common
interface (callable for creation and .speak()).
>>这个抽象的实现了创造一个宠物,然后基于我们选择的工厂类型(狗或者猫或者其他随意的动物)
>>这个可以实现因为猫,狗或者其他的动物都遵照这个接口(可调用创造和说话)
Now my application can create pets abstractly and decide later,
based on my own criteria, dogs over cats.
>>现在我的应用可以抽象创造宠物然后再根据我个人的准早来创造狗,而不是猫 *Where is the pattern used practically? *References:
https://sourcemaking.com/design_patterns/abstract_factory
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ *TL;DR80
Provides a way to encapsulate a group of individual factories.
""" import random class PetShop(object): """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 def show_pet(self):
"""Creates and shows a pet using the abstract factory""" pet = self.pet_factory()
print("We have a lovely {}".format(pet))
print("It says {}".format(pet.speak())) class Dog(object): def speak(self):
return "woof" def __str__(self):
return "Dog" class Cat(object): def speak(self):
return "meow" def __str__(self):
return "Cat" # Additional factories: # Create a random animal
def random_animal():
"""Let's be dynamic!"""
return random.choice([Dog, Cat])() # Show pets with various factories
if __name__ == "__main__": # A Shop that sells only cats
cat_shop = PetShop(Cat)
cat_shop.show_pet()
print("") # A shop that sells random animals
shop = PetShop(random_animal)
for i in range(3):
shop.show_pet()
print("=" * 20) ### OUTPUT ###
# We have a lovely Cat
# It says meow
#
# We have a lovely Dog
# It says woof
# ====================
# We have a lovely Cat
# It says meow
# ====================
# We have a lovely Cat
# It says meow
# ====================

Python转载版

最新文章

  1. 利用Levenshtein Distance (编辑距离)实现文档相似度计算
  2. 关于Hibernate的Dialect
  3. 设计模式(四)抽象工厂模式(Abstract Factory Pattern)
  4. 配置sublime text 3 的Python开发环境
  5. 【JavaScript忍者秘籍】定时器
  6. Servlet3.0学习总结(二)——使用注解标注过滤器(Filter)
  7. HBase从hdfs导入数据
  8. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息]
  9. 由linux内核某个片段(container_of)引发的对于C语言的深入理解
  10. Android环境rm命令
  11. Asynchronous
  12. 使用JS的FormData对象
  13. SQL点滴2—重温sql语句中的join操作
  14. H5微信通过百度地图API实现导航方式一
  15. python链接MySQLdb报错:2003
  16. ab使用命令
  17. ArcGIS API for JavaScript 4.x 本地部署之Nginx法
  18. 末学者笔记--shell编程上 1 玄
  19. C++编程入门
  20. kubernetes常用命令

热门文章

  1. js实现一个小游戏(飞翔的jj)
  2. logstash的filter之grok
  3. Maven 依赖调解源码解析(一):开篇
  4. Node.js实现前后端交换——用户登陆
  5. [atAGC054C]Roughly Sorted
  6. [hdu7081]Pty loves book
  7. 【原创】【自制系列】自制stack类型(泛型)
  8. Codeforces 1332G - No Monotone Triples(数据结构综合)
  9. 【豆科基因组】普通豆/菜豆/四季豆Common bean (Phaseolus vulgaris L.) 683个自然群体重测序2020NG
  10. R 语言实战-Part 5-2笔记