简单工厂、工厂模式、抽象工厂模式

本文介绍了工厂模式在披萨制作程序中的应用,包括简单工厂、工厂方法及抽象工厂三种模式。通过实例展示了如何使用这些模式来创建不同类型的披萨。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单工厂其实不是一个设计模式,反而比较像是一种编程习惯

产品基类Pizza, 子类 CheesePizza、 PepperoniPizza、ClamPizza、VeggiePizza
顾客通过商店PizzaStore的order_pizza函数预订pizza。
而pizza的生产依赖SimpleFactory, 通过SimpleFactory的create_pizza获取实际的pizza 。
调用过程如下

pizza_store = PizzaStore(SimplePizzaFactory())
pizza_store.order_pizza('cheese')
pizza_store.order_pizza('clam')

完整代码如下:

# coding:utf-8
import inspect


# pizza接口 实际pizza子类
class Pizza(object):
    def __init__(self):
        print 'create a %s' % self

    def prepare(self):
        print '%s-->prepare' % self

    def bake(self):
        print '%s-->Bake' % self

    def cut(self):
        print '%s-->cut' % self

    def box(self):
        print '%s-->box' % self

    def __str__(self):
        return self.__class__.__name__


class CheesePizza(Pizza): pass


class ClamPizza(Pizza): pass


class SimplePizzaFactory:
    def create_pizza(self, type):
        if type == 'cheese':
            pizza = CheesePizza()
        elif type == 'clam':
            pizza = ClamPizza()
        else:
            pizza = None
        return pizza


class PizzaStore:
    def __init__(self, factory):
        self.factory = factory

    def order_pizza(self, type):
        pizza = self.factory.create_pizza(type)
        pizza.prepare()
        pizza.bake()
        pizza.cut()
        pizza.box()
        pizza.box()
        return pizza


pizza_store = PizzaStore(SimplePizzaFactory())
pizza_store.order_pizza('cheese')
pizza_store.order_pizza('clam')

这里写图片描述


工厂模式 通过让子类决定该创建的对象是什么, 达到将对象创建的过程封装的目的。

pizza的生产不再通过SimpleFactory , 而是把create_pizza放到PizzaStore中,并设置成抽象方法, 让继承PizzaStore的子类在根据实际需要实现create_pizza
NYPizzaStore、ChicagoPizzaStore继承PizzaStore,并实现对应的create_pizza

# coding:utf-8
import inspect

# pizza接口 实际pizza子类
class Pizza(object):
    def __init__(self):
        print 'create a %s' % self

    def prepare(self):
        print '%s-->prepare' % self

    def bake(self):
        print '%s-->Bake' % self

    def cut(self):
        print '%s-->cut' % self

    def box(self):
        print '%s-->box' % self

    def __str__(self):
        return self.__class__.__name__


class NYCheesePizza(Pizza):pass


class NYClamPizza(Pizza):pass


class ChicagoCheesePizza(Pizza):pass


class ChicagoClamPizza(Pizza):pass


# pizza商店 和实际子类商店
class PizzaStore(object):
    def order_pizza(self, type):
        pizza = self.create_pizza(type)
        pizza.prepare()
        pizza.bake()
        pizza.cut()
        pizza.box()
        return pizza

    def create_pizza(self, type):
        raise NotImplemented()


class NYPizzaStore(PizzaStore):
    def create_pizza(self, type):
        if type == 'cheese':
            pizza = NYCheesePizza()
        elif type == 'clam':
            pizza = NYClamPizza()
        else:
            pizza = None
        return pizza


class ChicagoPizzaStore(PizzaStore):
    def create_pizza(self, type):
        if type == 'cheese':
            pizza = ChicagoCheesePizza()
        elif type == 'clam':
            pizza = ChicagoClamPizza()
        else:
            pizza = None
        return pizza


pizza_store = NYPizzaStore()
pizza_store.order_pizza('cheese')
pizza_store = ChicagoPizzaStore()
pizza_store.order_pizza('cheese')

这里写图片描述


抽象工厂方法
提供一个接口, 用于创建相关或依赖对象的家族,而不需要指明具体类

在上个例子的基础上,增加了原料工厂

# coding:utf-8
import inspect


# 原料接口 和实际原料子类
class Dough(object):
    def get_name(self): raise NotImplemented


class ThickCrustDough(Dough):
    def get_name(self):
        return 'Thick Crust Dough'


class ThinCrustDough(Dough):
    def get_name(self):
        return 'Thin Crust Dough'


class Sauce(object):
    def get_name(self): raise NotImplemented


class PlumTomatoSauce(object):
    def get_name(self):
        return 'Plum Tomato Sauce'


class MainaraSauce(object):
    def get_name(self):
        return 'Mainara Sauce'


#原料工厂 和实际原料工厂子类
class PizzaIngredientFactory(object):
    def createDough(self):  raise NotImplemented()

    def createSauce(self): raise NotImplemented()


class NYPizzaIngredientFactory(PizzaIngredientFactory):
    def createDough(self):
        return ThickCrustDough()

    def createSauce(self):
        return PlumTomatoSauce()


class ChicagoPizzaIngredientFactory(PizzaIngredientFactory):
    def createDough(self):
        return ThinCrustDough()

    def createSauce(self):
        return MainaraSauce()

# pizza接口 实际pizza子类
class Pizza(object):
    def __init__(self):
        print 'create a %s' % self
        self.dough = None
        self.sauce = None

    def prepare(self):
        raise NotImplemented()

    def show_ingredient(self):
        print 'has ingredient:', self.sauce.get_name(), ';', self.dough.get_name()

    def bake(self):
        print '%s-->Bake' % self

    def cut(self):
        print '%s-->cut' % self

    def box(self):
        print '%s-->box' % self

    def __str__(self):
        return self.__class__.__name__


class NYCheesePizza(Pizza):
    def __init__(self, ingredientFactory):
        self.ingredientFactory = ingredientFactory
        super(NYCheesePizza, self).__init__()

    def prepare(self):
        print 'NYCheesePizza Pareparing ...'
        self.dough = self.ingredientFactory.createDough()
        self.sauce = self.ingredientFactory.createSauce()


class NYClamPizza(Pizza):
    def __init__(self, ingredientFactory):
        self.ingredientFactory = ingredientFactory
        super(NYClamPizza, self).__init__()

    def prepare(self):
        print 'NYClamPizza Pareparing ...'
        self.dough = self.ingredientFactory.createDough()
        self.sauce = self.ingredientFactory.createSauce()


class ChicagoCheesePizza(Pizza):
    def __init__(self, ingredientFactory):
        self.ingredientFactory = ingredientFactory
        super(ChicagoCheesePizza, self).__init__()

    def prepare(self):
        print 'ChicagoCheesePizza Pareparing ...'
        self.dough = self.ingredientFactory.createDough()
        self.sauce = self.ingredientFactory.createSauce()


class ChicagoClamPizza(Pizza):
    def __init__(self, ingredientFactory):
        self.ingredientFactory = ingredientFactory
        super(ChicagoClamPizza, self).__init__()

    def prepare(self):
        print 'ChicagoClamPizza Pareparing ...'
        self.dough = self.ingredientFactory.createDough()
        self.sauce = self.ingredientFactory.createSauce()


# pizza商店 和实际子类商店
class PizzaStore(object):
    def order_pizza(self, type):
        pizza = self.create_pizza(type)
        pizza.prepare()
        pizza.show_ingredient()
        pizza.bake()
        pizza.cut()
        pizza.box()
        return pizza

    def create_pizza(self, type):
        raise NotImplemented()


class NYPizzaStore(PizzaStore):
    def create_pizza(self, type):
        ingreientfactory = NYPizzaIngredientFactory()

        if type == 'cheese':
            pizza = NYCheesePizza(ingreientfactory)
        elif type == 'clam':
            pizza = NYClamPizza(ingreientfactory)
        else:
            pizza = None
        return pizza


class ChicagoPizzaStore(PizzaStore):
    def create_pizza(self, type):
        ingreientfactory = ChicagoPizzaIngredientFactory()
        if type == 'cheese':
            pizza = ChicagoCheesePizza(ingreientfactory)
        elif type == 'clam':
            pizza = ChicagoClamPizza(ingreientfactory)
        else:
            pizza = None
        return pizza


pizza_store = NYPizzaStore()
pizza_store.order_pizza('cheese')
pizza_store = ChicagoPizzaStore()
pizza_store.order_pizza('cheese')

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值