python风格的抽象工厂模式
抽象工厂模式:
提供一个接口,用户创建多个相关或依赖对象,而不需要指定具体类。
原则:
依赖抽象,不依赖具体类。
实例:
用不同原材料制作不同口味的披萨,创建不同原材料的工厂,不同实体店做出口味不同的披萨。创建一个产品家族(Dough、Sauce、Cheese和Clam)的抽象类型(PizzaIngredientFactory),这个类型的子类(NYPizzaIngredientFactory和ChicagoPizzaIngredientFactory)定义了产品被产生的方法。
工厂模式和抽象工厂模式的区别:
工厂模式是在派生类中定义一个工厂的抽象接口,然后基类负责创建具体对象;抽象工厂模式是维护一个产品家族,由基类定义产品被生产的方法,客户根据派生类的接口进行开发。
代码:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8') '''
披萨
'''
class Pizza:
name = ""
dough = None
sauce = None
cheese = None
clam = None def prepare(self):
pass def bake(self):
print "烘烤25分钟在350。".decode('utf-8') def cut(self):
print "切割成对角线切片。".decode('utf-8') def box(self):
print "放在官方的盒子中。".decode('utf-8') def get_name(self):
return self.name def set_name(self, name):
self.name = name def to_string(self):
string = "%s:\n" % self.name
string += " 面团: %s\n" % self.dough.to_string() if self.dough else ""
string += " 酱汁: %s\n" % self.sauce.to_string() if self.sauce else ""
string += " 奶酪: %s\n" % self.cheese.to_string() if self.cheese else ""
string += " 文蛤: %s\n" % self.clam.to_string() if self.clam else ""
return string '''
什么类别的披萨
'''
class CheesePizza(Pizza):
def __init__(self, ingredient_factory):
self.ingredient_factory = ingredient_factory def prepare(self):
print "准备: %s" % self.name
self.dough = self.ingredient_factory.create_dough()
self.sauce = self.ingredient_factory.create_sauce()
self.cheese = self.ingredient_factory.create_cheese() class ClamPizza(Pizza):
def __init__(self, ingredient_factory):
self.ingredient_factory = ingredient_factory def prepare(self):
print "准备: %s" % self.name
self.dough = self.ingredient_factory.create_dough()
self.sauce = self.ingredient_factory.create_sauce()
self.clam = self.ingredient_factory.create_clam() '''
披萨店
'''
class PizzaStore:
def order_pizza(self, pizza_type):
self.pizza = self.create_pizza(pizza_type)
self.pizza.prepare()
self.pizza.bake()
self.pizza.cut()
self.pizza.box()
return self.pizza def create_pizza(self, pizza_type):
pass '''
纽约披萨实体店1
'''
class NYPizzaStore(PizzaStore):
def create_pizza(self, pizza_type):
ingredient_factory = NYPizzaIngredientFactory() if pizza_type == "cheese":
pizza = CheesePizza(ingredient_factory)
pizza.set_name("纽约风格芝士披萨".decode('utf-8'))
elif pizza_type == "clam":
pizza = ClamPizza(ingredient_factory)
pizza.set_name("纽约风格文蛤披萨".decode('utf-8'))
else:
pizza = None return pizza '''
芝加哥披萨实体店2
'''
class ChicagoPizzaStore(PizzaStore):
def create_pizza(self, pizza_type):
ingredient_factory = ChicagoPizzaIngredientFactory() if pizza_type == "cheese":
pizza = CheesePizza(ingredient_factory)
pizza.set_name("芝加哥风格芝士披萨".decode('utf-8'))
elif pizza_type == "clam":
pizza = ClamPizza(ingredient_factory)
pizza.set_name("芝加哥风格文蛤披萨".decode('utf-8'))
else:
pizza = None return pizza '''
生产披萨的工厂
'''
class PizzaIngredientFactory:
def create_dough(self):
pass def create_sauce(self):
pass def create_cheese(self):
pass def create_clam(self):
pass '''
生产披萨的实体工厂1
'''
class NYPizzaIngredientFactory(PizzaIngredientFactory):
def create_dough(self):
return ThinDough() def create_sauce(self):
return MarinaraSauce() def create_cheese(self):
return FreshCheese() def create_clam(self):
return FreshClam() '''
生产披萨的实体工厂2
'''
class ChicagoPizzaIngredientFactory(PizzaIngredientFactory):
def create_dough(self):
return ThickDough() def create_sauce(self):
return MushroomSauce() def create_cheese(self):
return BlueCheese() def create_clam(self):
return FrozenClam() class Dough:
def to_string(self):
pass class ThinDough(Dough):
def to_string(self):
return "薄的面团" class ThickDough(Dough):
def to_string(self):
return "厚的生面团" class Sauce:
def to_string(self):
pass class MarinaraSauce(Sauce):
def to_string(self):
return "番茄酱" class MushroomSauce(Sauce):
def to_string(self):
return "蘑菇酱" class Cheese:
def to_string(self):
pass class FreshCheese(Cheese):
def to_string(self):
return "新鲜的奶酪" class BlueCheese(Cheese):
def to_string(self):
return "蓝纹奶酪" class Clam:
def to_string(self):
pass class FreshClam(Clam):
def to_string(self):
return "新鲜的文蛤" class FrozenClam(Clam):
def to_string(self):
return "冷冻的文蛤" if __name__ == "__main__":
# 创建了两个披萨实体店
ny_store = NYPizzaStore()
chicago_store = ChicagoPizzaStore() # 在第一个披萨对象中订购了一个cheese风味的披萨
pizza = ny_store.order_pizza("cheese")
print pizza.to_string()
print "迈克订购了一个 %s" % pizza.get_name()
print pizza = chicago_store.order_pizza("clam")
print pizza.to_string()
print "约翰订购了一个%s" % pizza.get_name()
结果:
准备: 纽约风格芝士披萨
烘烤25分钟在350。
切割成对角线切片。
放在官方的盒子中。
纽约风格芝士披萨:
面团: 薄的面团
酱汁: 番茄酱
奶酪: 新鲜的奶酪 迈克订购了一个 纽约风格芝士披萨 准备: 芝加哥风格文蛤披萨
烘烤25分钟在350。
切割成对角线切片。
放在官方的盒子中。
芝加哥风格文蛤披萨:
面团: 厚的生面团
酱汁: 蘑菇酱
文蛤: 冷冻的文蛤 约翰订购了一个芝加哥风格文蛤披萨
python风格的抽象工厂模式的更多相关文章
- Python—程序设计:抽象工厂模式
抽象工厂模式 内容:定义一个工厂类接口,让工厂子类来创建一系列相关或相互依赖的对象. 例:生产一部手机,需要手机壳.CPU.操作系统三类对象进行组装,其中每类对象都有不同的种类.对每个具体工厂,分别生 ...
- [Python编程实战] 第一章 python的创建型设计模式1.1抽象工厂模式
注:关乎对象的创建方式的设计模式就是“创建型设计模式”(creational design pattern) 1.1 抽象工厂模式 “抽象工厂模式”(Abstract Factory Pattern) ...
- 抽象工厂模式(python版)
http://blog.csdn.net/ponder008/article/details/6886039 抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.优点:易 ...
- 设计模式(Python)-简单工厂,工厂方法和抽象工厂模式
本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...
- 大话设计模式Python实现- 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类 下面是一个抽象工厂的demo: #!/usr/bin/env pyth ...
- 浅谈Python设计模式 - 抽象工厂模式
声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 在上一篇我们对工厂模式中的普通工厂模式有了一定的了解,其实抽象工作就是 表示针对 ...
- python 设计模式之工厂模式 Factory Pattern (简单工厂模式,工厂方法模式,抽象工厂模式)
十一回了趟老家,十一前工作一大堆忙成了狗,十一回来后又积累了一大堆又 忙成了狗,今天刚好抽了一点空开始写工厂方法模式 我看了<Head First 设计模式>P109--P133 这25页 ...
- [Python设计模式] 第15章 如何兼容各种DB——抽象工厂模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 如何让一个程序,可以灵活替换数据库? 基础版本 class User(): ...
- 设计模式 -创建型模式 ,python工厂模式 抽象工厂模式(1)
工厂模式 import xml.etree.ElementTree as etree import json class JSONConnector: def __init__(self, filep ...
随机推荐
- android 6.0 动态权限
Android 6.0 动态权限: 除了要在AndroidManifest.xml中申请外,还需使用时,请求用户允许授权. 以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整 ...
- tomcat溢出
http://blog.csdn.net/qq_15653597/article/details/42753269?locationNum=10
- Zookeeper学习笔记——2 Shell和Java API的使用
ZooKeeper的使用一般都接触不到,因为平时工作甚少直接使用ZK.但是通过手动操作一下ZK,还是能对其中的门道了解各一二. shell 常用命令 help 查看所有支持的命令 [zk: local ...
- [Python] 糗事百科文本数据的抓取
[Python] 糗事百科文本数据的抓取 源码 https://github.com/YouXianMing/QiuShiBaiKeText import sqlite3 import time im ...
- 从安装node js到构建一个vue并启动它
1.安装node js 下载地址:http://nodejs.cn/download/2.安装完成后运行Node.js command prompt(node -v查看安装版本) 3.安装npm(由于 ...
- JavaScript变量作用域(Variable Scope)和闭包(closure)的基础知识
在这篇文章中,我会试图讲解JavaScript变量的作用域和声明提升,以及许多隐隐藏的陷阱.为了确保我们不会碰到不可预见的问题,我们必须真正理解这些概念. 基本定义 作用范围是个“木桶”,里面装着变量 ...
- 基于php5.5使用PHPMailer-5.2发送邮件
PHPMailer - A full-featured email creation and transfer class for PHP. 在PHP环境中可以使用PHPMailer来创建和发送邮件. ...
- How to make PostgreSQL functions atomic?
Question: How to make PostgreSQL functions atomic? Assume I have some PostgreSQL functions like the ...
- MySql之查询基础与进阶
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8283547.html 一:基本查询 SELECT [DISTINCT] 列1,列2,列3... FROM 表 ...
- [转] Dangers of using dlsym() with RTLD_NEXT
There are times when you want to wrap a library function in order to provide some additional functio ...