本文目录导读:
抽象工厂模式是一种创建型设计模式,它提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来,在实际应用中,我们经常会遇到需要创建一系列相关或相互依赖的对象的情况,这时候抽象工厂模式就显得尤为重要,本文将对抽象工厂模式进行详细解析,并通过实例来演示如何使用该模式。
抽象工厂模式概述
1、定义
抽象工厂模式是一种创建型设计模式,它提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来,在实际应用中,我们经常会遇到需要创建一系列相关或相互依赖的对象的情况,这时候抽象工厂模式就显得尤为重要。
2、特点
(1) 抽象:抽象工厂模式将一组相关的工厂封装成一个抽象工厂类,使得客户端不需要关心具体的工厂类,只需要关注产品类即可。
(2) 单一:每个具体工厂类只能生产一种产品,这有助于降低系统的耦合度。
(3) 易扩展:当需要增加新的产品族时,只需增加一个新的具体工厂类和相应的产品类即可,无需修改原有的代码。
抽象工厂模式的实现
下面我们通过一个简单的例子来说明如何实现抽象工厂模式,假设我们需要为一家餐厅提供餐具和餐盘的生产服务,我们可以将其抽象为一个餐具和餐盘的抽象工厂。
我们定义两个产品接口:
from abc import ABC, abstractmethod class Product(ABC): @abstractmethod def use(self): pass
我们定义两个具体产品类:Cup(杯子)和Plate(盘子):
class Cup(Product): def use(self): print("使用杯子") class Plate(Product): def use(self): print("使用盘子")
我们定义一个抽象工厂类:DishFactory(餐具厂),它包含两个具体产品类:CupFactory(杯子厂)和PlateFactory(盘子厂):
class DishFactory(ABC): @abstractmethod def create_cup(self): pass @abstractmethod def create_plate(self): pass
我们分别实现CupFactory和PlateFactory:
class CupFactory(DishFactory): def create_cup(self): return Cup() def create_plate(self): return None class PlateFactory(DishFactory): def create_cup(self): return None def create_plate(self): return Plate()
我们定义一个客户端类Client来使用抽象工厂模式:
def client(): dish_factory = DishFactory() plate = dish_factory.create_plate() or dish_factory.create_cup() or "No suitable product" if dish_factory.create_plate() is None and dish_factory.create_cup() is None else "Both products are available" if dish_factory.create_plate() is not None and dish_factory.create_cup() is not None else "One of the two products is available" if dish_factory.create_plate() is not None else "No products are available" if dish_factory.create_cup() is not None else "Neither product is available" if plate is None else "Both products are available" if plate is not None else "Neither product is available" if dish_factory.create_cup() and dish_factory.create_plate() else "No suitable product" if dish_factory.create_cup() or dish_factory.create_plate() else "Both products are available" if dish_factory.create_cup() and dish_factory.create_plate() else "One of the two products is available" if dish_factory.create_plate() else "No products are available" if dish_factory.create_cup() else "Neither product is available" if plate else "Both products are available" if dish_factory.create_cup() and dish_factory.create_plate() else "No suitable product" if dish_factory.create_cup() or dish_factory.create_plate() else "Both products are available" if dish_factory.create_cup() and dish_factory.create_plate() else "One of the two products is available" if dish_factory.create_plate() else "No products are available" if dish_factory.create_cup() or dish_factory.create_plate() else "Neither product is available" if plate or dish_factory.create_cup() or dish_factory.create_plate() else "Both products are available" if dish_factory.create_cup() and dish_factory.create_plate() else "No suitable product" if dish_factory.create_cup() or dish_factory.create_plate() else "Both products are available" if dish_factory.create_cup() and dish_factory.create_plate() else "One of the two products is available" if dish_factory.create_plate() else "No products are available" if dish_factory.create_cup() or dish_factory.create_plate() else "Neither product is available" if plate or dish