# 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设计模式---创建型之工厂模式的更多相关文章

  1. 【python设计模式-创建型】工厂方法模式

    工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻 ...

  2. python设计模式---创建型之单例模式

    数据结构和算法是基本功, 设计模式是最佳实现. 作为程序员,必须有空了就练一练哈. # coding = utf-8 """ # 经典单例 class Singleton ...

  3. Python设计模式 - 创建型 - 单例模式(Singleton) - 十种

    对于很多开发人员来说,单例模式算是比较简单常用.也是最早接触的设计模式了,仔细研究起来单例模式似乎又不像看起来那么简单.我们知道单例模式适用于提供全局唯一访问点,频繁需要创建及销毁对象等场合,的确方便 ...

  4. 设计模式之单例模式与工厂模式的Python实现(二)

    2. 工厂模式 工厂模式是创建型设计模式的一种.核心的思想是,通过传递给类或函数某种产品的信息来创建产品并返回.当我们想得到产品a对象,只需把产品a的名字传递给工厂函数就能得到产品a对象.而核心思想的 ...

  5. java设计模式-----1、简单工厂模式

    简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例,简单来说 ...

  6. headfirst设计模式(5)—工厂模式体系分析及抽象工厂模式

    先编一个这么久不写的理由 上周我终于鼓起勇气翻开了headfirst设计模式这本书,看看自己下一个设计模式要写个啥,然后,我终于知道我为啥这么久都没写设计模式了,headfirst的这个抽象工厂模式, ...

  7. Java设计模式(1)工厂模式(Factory模式)

    工厂模式定义:提供创建对象的接口. 为何使用工厂模式 工厂模式是我们最常用的模式了,著名的Jive论坛,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因 ...

  8. python设计模式之模型-视图-控制器模式

    python设计模式之模型-视图-控制器模式 关注点分离( Separation of Concerns, SoC)原则是软件工程相关的设计原则之一. SoC原则背后的思想是将一个应用切分成不同的部分 ...

  9. 【JS设计模式】温习简单工厂模式、工厂方法模式、抽象工厂模式概念

    注:空心箭头表示的是种继承关系,工厂类和产品类之间是一种依赖关系.是用箭头加虚线表示的,以下的模型图是用的实线是不正确(时间不够用,在这里我偷懒了.这个习惯不好,呵呵) 简单工厂模式(Simple F ...

随机推荐

  1. 其他综合-fdisk一键分区操作-无需脚本

    fdisk一键操作分区-无需脚本(根据自己的实际环境操作) 为了让在系统里能够显示新添加的硬盘已知有两种操作方法 : 1.重启 2.输入echo "- - -" > /sys ...

  2. CSS代码检查工具stylelint

    前面的话 CSS不能算是严格意义的编程语言,但是在前端体系中却不能小觑. CSS 是以描述为主的样式表,如果描述得混乱.没有规则,对于其他开发者一定是一个定时炸弹,特别是有强迫症的人群.CSS 看似简 ...

  3. 【CH4302】Interval GCD

    题目大意:给定一个长度为 N 的序列,M 个操作,支持区间加,区间查询最大公约数. 题解: 先来看一个子问题,若是单点修改,区间最大公约数,则可以发现,每次修改最多改变 \(O(logn)\) 个答案 ...

  4. Linux基本命令总结(六)

    接上篇: 27,diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的输出被称为补丁 (patch),因为Linux系统中还有一个patch程序,可以根据diff的输 ...

  5. 剑指Offer_编程题_23

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. class Solution { public: ...

  6. Docker:macvlan实现容器跨主机通信 [十四]

    一.什么是macvlan 1.macvlan 本身是 linux kernel 模块,其功能是允许在同一个物理网卡上配置多个 MAC 地址, 2.即多个 interface,每个 interface ...

  7. WEB内容换行

    word-wrap:break-word 单词间换行 word-break:break-all 单词内也可以换行 white-space属性指定元素内的空白怎样处理 normal 默认.空白会被浏览器 ...

  8. Beamer中左边画图, 右边文字解释

    \begin{columns} \column{.4\textwidth} \begin{figure} \centering % Requires \usepackage{graphicx} \in ...

  9. [Linux容器]当我们谈容器的时候,我们在谈什么

    Docker在当下很火,那么,当我们谈Docker,谈容器的时候,我们在谈什么? 或者说,你对Docker,对容器了解吗?容器,到底是怎么一回事儿? 这篇文章着重来讲一下Linux容器,为什么强调Li ...

  10. 简单对比git pull和git pull --rebase的使用

    使用下面的关系区别这两个操作:git pull = git fetch + git mergegit pull --rebase = git fetch + git rebase 现在来看看git m ...