python设计模式---创建型之工厂模式

# coding = utf-8
from abc import ABCMeta, abstractmethod
# 简单工厂模式
class Animal(metaclass=ABCMeta):
@abstractmethod
def do_say(self):
pass
class Dog(Animal):
def do_say(self):
print('Bhow Bhow!!!')
class Cat(Animal):
def do_say(self):
print('Meow Meow!!!')
class ForestFactory:
def make_sound(self, object_type):
return eval(object_type)().do_say()
ff = ForestFactory()
ff.make_sound('Dog')
ff.make_sound('Cat')
# 工厂方法模式
class Section(metaclass=ABCMeta):
@abstractmethod
def describe(self):
pass
class PersonalSection(Section):
def describe(self):
print('Personal Section')
class AlbumSection(Section):
def describe(self):
print('Album Section')
class PatentSection(Section):
def describe(self):
print('Patent Section')
class PublicationSection(Section):
def describe(self):
print('Publication Section')
class Profile(metaclass=ABCMeta):
def __init__(self):
self.sections = []
self.create_profile()
@abstractmethod
def create_profile(self):
pass
def get_sections(self):
return self.sections
def add_sections(self, section):
self.sections.append(section)
class LinkedIn(Profile):
def create_profile(self):
self.add_sections(PersonalSection())
self.add_sections(PatentSection())
self.add_sections(PublicationSection())
class FaceBook(Profile):
def create_profile(self):
self.add_sections(PersonalSection())
self.add_sections(AlbumSection())
profile_type = 'LinkedIn'
profile = eval(profile_type)()
print('Creating Profile...', type(profile).__name__)
print('Profile has sections ---', profile.get_sections())
for item in profile.get_sections():
item.describe()
profile_type = 'FaceBook'
profile = eval(profile_type)()
print('Creating Profile...', type(profile).__name__)
print('Profile has sections ---', profile.get_sections())
for item in profile.get_sections():
item.describe()
# 抽象工厂模式
class PizzaFactory(metaclass=ABCMeta):
@abstractmethod
def create_veg_pizza(self):
pass
@abstractmethod
def create_non_veg_pizza(self):
pass
class IndianPizzaFactory(PizzaFactory):
def create_veg_pizza(self):
return DeluxVeggiePizza()
def create_non_veg_pizza(self):
return ChickenPizza()
class USPizzaFactory(PizzaFactory):
def create_veg_pizza(self):
return MexicanVegPizza()
def create_non_veg_pizza(self):
return HamPizza()
class VegPizza(metaclass=ABCMeta):
@abstractmethod
def prepare(self, veg_pizza):
pass
class NonVegPizza(metaclass=ABCMeta):
@abstractmethod
def serve(self, non_veg_pizza):
pass
class DeluxVeggiePizza(VegPizza):
def prepare(self):
print('Prepare ', type(self).__name__)
class ChickenPizza(NonVegPizza):
def serve(self, veg_pizza):
print(type(self).__name__, 'is served with chicken on ',
type(veg_pizza).__name__)
class MexicanVegPizza(VegPizza):
def prepare(self):
print('Prepare ', type(self).__name__)
class HamPizza(NonVegPizza):
def serve(self, veg_pizza):
print(type(self).__name__, 'is served with ham on ',
type(veg_pizza).__name__)
class PizzaStore:
def __init__(self):
pass
def make_pizzas(self):
for factory in [IndianPizzaFactory(), USPizzaFactory()]:
self.factory = factory
self.non_veg_pizza = self.factory.create_non_veg_pizza()
self.veg_pizza = self.factory.create_veg_pizza()
self.veg_pizza.prepare()
self.non_veg_pizza.serve(self.veg_pizza)
pizza = PizzaStore()
pizza.make_pizzas()
C:\Users\Sahara\.virtualenvs\test\Scripts\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py Bhow Bhow!!! Meow Meow!!! Creating Profile... LinkedIn Profile has sections --- [<__main__.PersonalSection object at 0x0000000001E8E9E8>, <__main__.PatentSection object at 0x0000000001E8EA20>, <__main__.PublicationSection object at 0x0000000001E8EA58>] Personal Section Patent Section Publication Section Creating Profile... FaceBook Profile has sections --- [<__main__.PersonalSection object at 0x0000000001E8EB38>, <__main__.AlbumSection object at 0x0000000001E8EB00>] Personal Section Album Section Prepare DeluxVeggiePizza ChickenPizza is served with chicken on DeluxVeggiePizza Prepare MexicanVegPizza HamPizza is served with ham on MexicanVegPizza Process finished with exit code
python设计模式---创建型之工厂模式的更多相关文章
- 【python设计模式-创建型】工厂方法模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻 ...
- python设计模式---创建型之单例模式
数据结构和算法是基本功, 设计模式是最佳实现. 作为程序员,必须有空了就练一练哈. # coding = utf-8 """ # 经典单例 class Singleton ...
- Python设计模式 - 创建型 - 单例模式(Singleton) - 十种
对于很多开发人员来说,单例模式算是比较简单常用.也是最早接触的设计模式了,仔细研究起来单例模式似乎又不像看起来那么简单.我们知道单例模式适用于提供全局唯一访问点,频繁需要创建及销毁对象等场合,的确方便 ...
- 设计模式之单例模式与工厂模式的Python实现(二)
2. 工厂模式 工厂模式是创建型设计模式的一种.核心的思想是,通过传递给类或函数某种产品的信息来创建产品并返回.当我们想得到产品a对象,只需把产品a的名字传递给工厂函数就能得到产品a对象.而核心思想的 ...
- java设计模式-----1、简单工厂模式
简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例,简单来说 ...
- headfirst设计模式(5)—工厂模式体系分析及抽象工厂模式
先编一个这么久不写的理由 上周我终于鼓起勇气翻开了headfirst设计模式这本书,看看自己下一个设计模式要写个啥,然后,我终于知道我为啥这么久都没写设计模式了,headfirst的这个抽象工厂模式, ...
- Java设计模式(1)工厂模式(Factory模式)
工厂模式定义:提供创建对象的接口. 为何使用工厂模式 工厂模式是我们最常用的模式了,著名的Jive论坛,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因 ...
- python设计模式之模型-视图-控制器模式
python设计模式之模型-视图-控制器模式 关注点分离( Separation of Concerns, SoC)原则是软件工程相关的设计原则之一. SoC原则背后的思想是将一个应用切分成不同的部分 ...
- 【JS设计模式】温习简单工厂模式、工厂方法模式、抽象工厂模式概念
注:空心箭头表示的是种继承关系,工厂类和产品类之间是一种依赖关系.是用箭头加虚线表示的,以下的模型图是用的实线是不正确(时间不够用,在这里我偷懒了.这个习惯不好,呵呵) 简单工厂模式(Simple F ...
随机推荐
- T66597 小xzy的任务 题解
T66597 小xzy的任务 题目背景 今天,小xzy的班主任交给他一个严肃的任务,匹配羽毛球运动员! ! ! 题目描述 羽毛球队有男女运动员各n人.给定2个n×n矩阵P和Q.Pij是男运动员i和女 ...
- LODOP直线px转换mm变斜线
LODOP中打印项顶边距左边距,宽高,可以选择的单位很多,详细可在LODOP官网下载参考LODOP技术手册. 关于LODOP打印直线和虚线,可查看本博客相关博文:Lodop如何打印直线.Lodop打印 ...
- Fixing “Did you mean to run dotnet SDK commands?” error when running dotnet –version
I recently installed the dotnet 1.11.0 Windows Server Hosting package which apparently installs the ...
- mongoDB 数据库简介
背景概念-数据库类型 对比关系型数据库 优点 : 逻辑清晰.容易理解,结构类似表格 使用 sql 语句技术成熟,使用方便 数据一致性好,完整性好 关系型数据库比较成熟,可以使用一些复杂操作 缺点 : ...
- CF1131D Gourmet choice
题目链接 题意 有两组菜,第一组有\(n\)种,第二组有\(m\)种.给出一个\(n\times m\)的矩阵,第\(i\)行第\(j\)列表示第一组中的第\(i\)种菜与第二组中的第\(j\)种菜好 ...
- 51nod1812树的双直径(换根树DP)
传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...
- Springboot 1.简介 及第一个demo
按照官网上的新建一个maven项目,然后将类引入pom.xml文件中 <?xml version="1.0" encoding="UTF-8"?> ...
- SQL随记(一)
1.关于define表示定义 2.sql%rowcount用于记录修改的条数,必须放在一个CUD语句后面执行,无法在select中使用. 3.两种调用过程的关键字:exec和call 两者区别: (1 ...
- chrome headless 无头浏览器 应用
1. 根据html生成pdf chrome.exe --headless --disable-gpu --print-to-pdf ...../index.html 2. puppeteer --- ...
- BOM:浏览器对象模型之浏览器剖析入门
BOM简介 BOM与DOM的关系 BOM对象包含的内容 重新认识浏览器 一.分裂的BOM和被收服的DOM BOM定义:是browser object model的缩写,简称浏览器对象模型. 主要处理浏 ...