3.抽象工厂模式

设计模式——抽象工厂模式

一、基本概念

1. 定义

抽象工厂模式(英语:Abstract factory pattern)是一种软件开发设计模式。抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来。

在正常使用中,客户端程序需要创建抽象工厂的具体实现,然后使用抽象工厂作为接口来创建这一主题的具体对象。客户端程序不需要知道(或关心)它从这些内部的工厂方法中获得对象的具体类型,因为客户端程序仅使用这些对象的通用接口。抽象工厂模式将一组对象的实现细节与他们的一般使用分离开来。

2. 优缺点

优点

  • 用户只需要知道具体工厂的名称就可得到所要的产品,无须知道产品的具体创建过程。
  • 灵活性增强,对于新产品的创建,只需多写一个相应的工厂类。
  • 典型的解耦框架。高层模块只需要知道产品的抽象类,无须关心其他实现类,满足迪米特法则、依赖倒置原则和里氏替换原则。
  • 可以在类的内部对产品族中相关联的多等级产品共同管理,而不必专门引入多个新的类来进行管理。
  • 当需要产品族时,抽象工厂可以保证客户端始终只使用同一个产品的产品组。
  • 抽象工厂增强了程序的可扩展性,当增加一个新的产品族时,不需要修改原代码,满足开闭原则。

缺点

  • 当产品族中需要增加一个新的产品时,所有的工厂类都需要进行修改。增加了系统的抽象性和理解难度。

3. 结构

抽象工厂模式的主要角色如下。

  • 抽象工厂(Abstract Factory):提供了创建产品的接口,它包含多个创建产品的方法 newProduct(),可以创建多个不同等级的产品;
  • 具体工厂(Concrete Factory):主要是实现抽象工厂中的多个抽象方法,完成具体产品的创建。
  • 抽象产品(Product):定义了产品的规范,描述了产品的主要特性和功能,抽象工厂模式有多个抽象产品。
  • 具体产品(ConcreteProduct):实现了抽象产品角色所定义的接口,由具体工厂来创建,它同具体工厂之间是多对一的关系。
classDiagram class AbstractFactory { <<abstract>> +createShape() Shape +createColor() Color } class ShapeFactory { +createShape() Shape } class ColorFactory { +createColor() Color } class Shape { <<abstract>> +draw() void } class Rectangle { +draw() void } class Circle { +draw() void } class Square { +draw() void } class Color { <<abstract>> +fill() void } class Red { +fill() void } class Green { +fill() void } class Blue { +fill() void } AbstractFactory <|-- ShapeFactory AbstractFactory <|-- ColorFactory Shape <|-- Rectangle Shape <|-- Circle Shape <|-- Square Color <|-- Red Color <|-- Green Color <|-- Blue ShapeFactory ..> Rectangle : 创建 ShapeFactory ..> Circle : 创建 ShapeFactory ..> Square : 创建 ColorFactory ..> Red : 创建 ColorFactory ..> Green : 创建 ColorFactory ..> Blue : 创建

二、代码实现

AbstractFactory.hpp:

#pragma once
#include <memory>
#include <string>
class Shape;
class Color;
/*
* 抽象工厂
* */
class AbstractFactory {
public:
virtual ~AbstractFactory() = default;
virtual std::unique_ptr<Color> getColor(const std::string& color) = 0;
virtual std::unique_ptr<Shape> getShape(const std::string& shape) = 0;
};

ShapeFactory.hpp

#pragma once
#include <iostream>
#include <memory>
#include <string>
#include "AbstractFactory.hpp"
/*
* 形状工厂
* */
class Shape {
public:
virtual ~Shape() = default;
virtual void creatShape() = 0;
};
class Rectangle : public Shape {
public:
void creatShape() override {
std::cout << "创建一个矩形..." << std::endl;
}
};
class Circle : public Shape {
public:
void creatShape() override {
std::cout << "创建一个圆形..." << std::endl;
}
};
class Square : public Shape {
public:
void creatShape() override {
std::cout << "创建一个正方形..." << std::endl;
}
};
/*
* 创建继承自 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象
* */
class ShapeFactory : public AbstractFactory {
public:
std::unique_ptr<Color> getColor(const std::string& color) override {
return nullptr;
}
std::unique_ptr<Shape> getShape(const std::string& shape) override {
if (shape == "circle") {
return std::make_unique<Circle>();
} else if (shape == "rectangle") {
return std::make_unique<Rectangle>();
} else if (shape == "square") {
return std::make_unique<Square>();
} else {
return nullptr;
}
}
};

ColorFactory.hpp:

#pragma once
#include <iostream>
#include <memory>
#include <string>
#include "AbstractFactory.hpp"
/*
* 颜色工厂
* */
class Color {
public:
virtual ~Color() = default;
virtual void fillColor() = 0;
};
class Red : public Color {
public:
void fillColor() override {
std::cout << "喷涂红色..." << std::endl;
}
};
class Green : public Color {
public:
void fillColor() override {
std::cout << "喷涂绿色..." << std::endl;
}
};
class Blue : public Color {
public:
void fillColor() override {
std::cout << "喷涂蓝色..." << std::endl;
}
};
class ColorFactory : public AbstractFactory {
public:
std::unique_ptr<Color> getColor(const std::string& color) override {
if (color == "red") {
return std::make_unique<Red>();
} else if (color == "green") {
return std::make_unique<Green>();
} else if (color == "blue") {
return std::make_unique<Blue>();
} else {
return nullptr;
}
}
std::unique_ptr<Shape> getShape(const std::string& shape) override {
return nullptr;
}
};

FactoryProducer.hpp:

#pragma once
#include <memory>
#include <string>
#include "ColorFactory.hpp"
#include "ShapeFactory.hpp"
class FactoryProducer {
/*
* 创建一个工厂生成器类,通过传递形状或颜色信息来获取工厂
* */
public:
static std::unique_ptr<AbstractFactory> getFactory(const std::string& choice) {
if (choice == "color") {
return std::make_unique<ColorFactory>();
} else if (choice == "shape") {
return std::make_unique<ShapeFactory>();
} else {
return nullptr;
}
}
};

main.cpp:

#include "FactoryProducer.hpp"
int main() {
/*
* 客户
* */
auto sf = FactoryProducer::getFactory("shape");
auto circle = sf->getShape("circle");
circle->creatShape();
return 0;
}

参考:


Thanks for reading!

3.抽象工厂模式

2026-01-22
1258 字 · 6 分钟

已复制链接

评论区

目录