C++实现设计模式——抽象工厂模式

感觉抽象工厂模式有点像你去买电脑的时候,先告诉卖电脑的你要买联想的,然后再告诉卖电脑的你要个鼠标,最后你喜提联想的鼠标回家了。
这里也感谢菜鸟教程,提供免费的学习资料。
优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。
使用场景: 1、QQ 换皮肤,一整套一起换。 2、生成不同操作系统的程序。
注意事项:产品族难扩展,产品等级易扩展。
这里写图片描述

根据上面的UML图编码:

color.h

#ifndef _COLOR_
#define _COLOR_

#include "iostream"
using namespace std;

class color
{
public:
    virtual void fill() = 0;
};

class red:public color  
{
public:
    void fill()
    {
        cout << "fill red." << endl;
    }
}; 

class green:public color
{
public:
    void fill()
    {
        cout << "fill green." << endl;
    }
};

class blue:public color
{
public:
    void fill()
    {
        cout << "fill blue." << endl;
    }
};

#endif

shape.h

#ifndef _SHAPE_
#define _SHAPE_

#include "iostream"
using namespace std;

class shape
{
public:
    virtual void draw() = 0;
};

class circle:public shape
{
public:
    void draw()
    {
        cout << "draw circle." << endl;
    }
};

class square:public shape
{
public:
    void draw()
    {
        cout << "draw square." << endl;
    }
};

class rectangle:public shape
{
public:
    void draw()
    {
        cout << "draw rectangle." << endl;
    }
};

#endif

abstract_factory.h

#ifndef _ABSTRACT_FACTORY_
#define _ABSTRACT_FACTORY_

#include "color.h"
#include "shape.h"
#include "iostream"
#include "string"
using namespace std;

class abstract_factory
{
public:
    virtual shape* get_shape(string shape_type) = 0;
    virtual color* get_color(string specific_color) = 0;
};

class shape_factory:public abstract_factory
{
public:
    shape* get_shape(string shape_type)
    {
        if (shape_type == "CIRCLE")
            return new circle;
        else if (shape_type == "SQUARE")
            return new square;
        else if (shape_type == "RECTANGLE")
            return new rectangle;
        else
        {
            cout << "there is no such shape." << endl;
            return NULL;
        }
    }
    color* get_color(string specific_color)
    {
        return NULL;
    }
};

class color_factory:public abstract_factory
{
public:
    color* get_color(string specific_color)
    {
        if (specific_color == "RED")
            return new red;
        else if (specific_color == "GREEN")
            return new green;
        else if (specific_color == "BLUE")
            return new blue;
        else
        {
            cout << "there is no such color." << endl;
            return NULL;
        }
    }
    shape* get_shape(string shape_type)
    {
        return NULL;
    }
};

class factory_producer
{
public:
    abstract_factory* get_factory(string factory_type)
    {
        if (factory_type == "SHAPE")
            return new shape_factory;
        else if (factory_type == "COLOR")
            return new color_factory;
        else 
        {
            cout << "there is no such factory." << endl;
            return NULL;
        }
    }
};

#endif 

abstract_factory_mode.cpp

#include "iostream"
#include "string"
#include "string.h"
#include "color.h"
#include "shape.h"
#include "abstract_factory.h"

using namespace std;

int main(int argc, char const *argv[])
{
    factory_producer f_p;
    abstract_factory *fac = NULL;
    shape *t_s = NULL;
    color *t_c = NULL;
    fac = f_p.get_factory("SHAPE");
    if (fac != NULL)
    {
        t_s = fac->get_shape("CIRCLE");
        if (t_s != NULL)
        {
            t_s->draw();
            delete t_s;
            t_s = NULL;
        }
        t_s = fac->get_shape("SQUARE");
        {
            t_s->draw();
            delete t_s;
            t_s = NULL;
        }
    }
    delete fac;
    fac = NULL;

    fac = f_p.get_factory("COLOR");
    if (fac != NULL)
    {
        t_c = fac->get_color("RED");
        if (t_c != NULL)
        {
            t_c->fill();
            delete t_c;
            t_c = NULL;
        }
        t_c = fac->get_color("WHITE");
        if (t_c != NULL)
        {
            t_c->fill();
            delete t_c;
            t_c = NULL;
        }
    }
    delete fac;
    fac = NULL;
    return 0;
}

打印效果
这里写图片描述

这里做个小笔记

这里写图片描述
抽象类无法实例化对象,当时可给我郁闷的呀,我这不是抽象类呀,我实例化的明明是派生类,搞什么鬼噢?!
度娘真相了,百度到一篇大佬的csdn帖子
记住:如果你的抽象类的派生类有任何一个纯虚函数没有实现,那么不管其它做的再多,这个类仍然是一个抽象类。
abstract_factory是一个抽象类,然而我写派生类shape_factory和color_factory的时候并没有实现与它本身不想关的虚函数。

猜你喜欢

转载自blog.csdn.net/LonelyGambler/article/details/80862768
今日推荐