首页 > 技术文章 > 抽象工厂模式(Abstract Factory Pattern)

dreammmz 2020-08-14 21:39 原文

抽象工厂模式是围绕一个超级工厂创建其他工厂。

在抽象工厂模式中,接口是负责创建一个相关对象的工程,不需要显示指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

意图:提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类。

解决:主要解决接口选择的问题。

如何解决:在一个产品族里,定义多个产品。

代码:在一个工厂里聚合多个同类产品。

实例:

优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的Creator里加代码,又要在具体的里面加代码。

使用场景:1、QQ换皮肤,一整套一起换。

注意事项:产品族难扩展,产品等级易扩展。

 

 

 

C++实现:

1、为形状创建一个接口

Shape.h

#pragma once
class Shape
{
public:
    virtual void draw() {}
};

 

2、创建实现接口的实体类

 Rectangle.h

#pragma once
#include<iostream>
#include "Shape.h"

class Rectangle :public Shape
{
public:
    void draw()
    {
        std::cout << "Rectangle::draw() method." << std::endl;
    }
};

 Square.h

#pragma once
#include<iostream>
#include "Shape.h"

class Square :public Shape
{
public:
    void draw()
    {
        std::cout << "Square::draw() method." << std::endl;
    }
};

 Circle.h

#pragma once
#include<iostream>
#include "Shape.h"

class Circle :public Shape
{
public:
    void draw()
    {
        std::cout << "Circle::draw() method." << std::endl;
    }
};

 

3、为颜色创建一个接口

 Color.h

#pragma once
class Color 
{
public:
    virtual void fill(){}
};

 

4、创建实现接口的实体类

Blue.h

#pragma once
#include<iostream>
#include"Color.h"

class Blue :public Color
{
public:
    void fill()
    {
        std::cout << "Blue::fill() method." << std::endl;
    }
};

Red.h

#pragma once
#include<iostream>
#include"Color.h"

class Red :public Color
{
public:
    void fill()
    {
        std::cout << "Red::fill() method." << std::endl;
    }
};

Green.h

#pragma once
#include<iostream>
#include"Color.h"

class Green :public Color
{
public:
    void fill()
    {
        std::cout << "Green::fill() method." << std::endl;
    }
};

 

5、为Color和Shape对象创建抽象类来获取工厂

 AbstractFactory.h

#pragma once
#include"Color.h"
#include"Shape.h"
#include<string>
#include<iostream>
class AbstractFactory
{
public:
    virtual Color* getColor(std::string color) = 0;
    virtual Shape* getShape(std::string shape) = 0;
};

 

6、创建扩张了AbstractFactory的工厂类,基于给定的信息生成实体类的对象

ColorFactory.h

#pragma once
#include<iostream>
#include<string>
#include"Blue.h"
#include"Green.h"
#include"Red.h"
#include"AbstractFactory.h"


class ColorFactory :public AbstractFactory
{
    Shape* getShape(std::string shapeType)
    {
        return NULL;
    }

    Color* getColor(std::string color)
    {
        if (color == " ") {
            return NULL;
        }
        if (color == "RED")
        {
            return new Red();
        }
        else if (color == "GREEN")
        {
            return new Green();
        }
        else if (color == "BLUE")
        {
            return new Blue();
        }
        return NULL;
    }
};

ShapeFactory.h

#pragma once
#include<iostream>
#include<string>
#include"Circle.h"
#include"Rectangle.h"
#include"Square.h"
#include"AbstractFactory.h"


class ShapeFactory :public AbstractFactory
{
    Shape* getShape(std::string shapeType)
    {
        if (shapeType == " ") {
            return NULL;
        }
        if (shapeType == "CIRCLE")
        {
            return new Circle();
        }
        else if (shapeType == "RECTANGLE")
        {
            return new Rectangle();
        }
        else if (shapeType == "SQUARE")
        {
            return new Square();
        }
        return NULL;
    }

    Color* getColor(std::string color)
    {
        return NULL;
    }
};

 

7、创建一个工厂创造器/生成器,通过传递形状或颜色信息来获取工厂

FactoryProducer.h

#pragma once
#include<iostream>
#include<string>
#include"AbstractFactory.h"
#include"ShapeFactory.h"
#include"ColorFactory.h"
class FactoryProducer 
{
public:
    static AbstractFactory* getFactory(std::string choice)
    {
        if (choice == "SHAPE")
        {
            return new ShapeFactory();
        }
        else if (choice == "COLOR")
        {
            return new ColorFactory();
        }
        return NULL;
    }
};

 

8、使用FactoryProducer来获取AbatractFactory,通过传递类型信息来获取实体类的对象

AbstractFactoryPatternDemo.cpp

#include<iostream>
#include"AbstractFactory.h"
#include"FactoryProducer.h"


using namespace std;
int main()
{
    //获取形状工厂
    AbstractFactory* shapeFactory = FactoryProducer::getFactory("SHAPE");
    //获取形状为 Circle 的对象
    Shape* shape1 = shapeFactory->getShape("CIRCLE");
    //调用 Circle 的 draw 方法
    shape1->draw();
    
    //获取颜色工厂
    AbstractFactory* colorFactory = FactoryProducer::getFactory("COLOR");

    //获取颜色为 Red 的对象
    Color* color1 = colorFactory->getColor("RED");

    //调用 Red 的 fill 方法
    color1->fill();
    
    system("pause");
    return 0;
}

 

9、实验结果

 

 

 

参考:https://www.runoob.com/design-pattern/abstract-factory-pattern.html

 

推荐阅读