设计模式_工厂模式

    工厂模式包括简单工厂模式、工厂方法模式和抽象工厂模式。

1 简单工厂模式:

优点:

  • 客户端只需要知道传入工厂类的参数的含义,对如何创建工厂对象不需要关心。
  • 只需要创建一个工厂

  举个栗子来加深理解:假如有一个工厂生产两中型号的产品,分别为产品A和产品B。那么很容易我们就可以写出如下代码:

#include<iostream>
using namespace std;

class Product{   //抽象类
    public:
        virtual void show() = 0;
};

class Product_A : public Product{
    public:
        void show(){
            cout << "Product_A" << endl;
        }
};

class Product_B : public Product{
    public:
        void show(){
            cout << "Product_B" << endl;
        }
};

class Factory{
    public:
        Product* Create(int i){
            switch (i){
                case 1:
                    return new Product_A;
                    break;
                case 2:
                    return new Product_B;
                    break;
                default:
                    return NULL;
                    break;
            }
        }
};
int main(){
    Factory *factory = new Factory();
    factory->Create(1)->show();
    factory->Create(2)->show();
    return 0;
}

    从上面我们可以看出,简单工厂模式需要就是一个工厂生产多种产品,需要告诉工厂生产什么产品。但是考虑到如果工厂需要增加一种产品,那么就需要改工厂类的代码,这就违反了开放封闭原则:类、模板、函数可以扩展,但是最好不要修改。所以就出现了工厂方法模式。

2 工厂方法模式

    优点:克服了简单工厂违背开放封闭原则的缺点,又保留了封装对象创建过程的优点,降低客户端和工厂的耦合性,可以说“工厂方法模式”是简单工厂模式的进一步抽象和推广。

    举个栗子:有多个工厂,多个产品,每一个工厂生产一种产品。相对于简单工厂模式,每增加一种产品,就增加一个工厂,增加工厂属于扩展,不会修改以前工厂类和产品的任何代码。具体代码如下:

#include<iostream>
using namespace std;

class Product{
    public:
        virtual void show() = 0;
};

class Product_A : public Product{
    public:
        void show(){
            cout << "Product_A" << endl;
        }
};

class Product_B : public Product{
    public:
        void show(){
            cout << "Product_B" << endl;
        }
};

class Factory{
    public:
        virtual Product* create() = 0;
};

class Factory_A : public Factory{
    public:
        Product* create(){
            return new Product_A;
        }
};

class Factory_B : public Factory{
    public:
        Product* create(){
            return new Product_B;
        }
};

int main(){
    Factory* productA = new Factory_A();
    Factory* productB = new Factory_B();

    productA->create()->show();
    productB->create()->show();
    return 0;
}

    从上面代码我们可以看出,工厂方法模式,每增加一种产品,就需要增加一个对象的工厂,比较繁琐。然后,我们再考虑这样一种情况,如果每种产品有多种型号怎么办,是不是工厂方法模式又不适合了呢,那就用接下来介绍的抽象工厂模式。

3 抽象工厂模式

举个栗子:多个工厂,多个产品,并且每个产品可以包含多个型号。具体代码如下:

#include <iostream>
using namespace std;

//定义抽象类
class product_1{//型号1
    public:
        virtual void show() = 0;
};

//定义具体类
class product_A1 :public product_1{
    public:
        void show(){ cout << "product A1" << endl; }
};

class product_B1 :public product_1{
    public:
        void show(){ cout << "product B1" << endl; }
};

//定义抽象类
class product_2{//型号2
    public:
        virtual void show() = 0;
};

//定义具体类
class product_A2 :public product_2{
    public:
        void show(){ cout << "product A2" << endl; }
};

class product_B2 :public product_2{
    public:
        void show(){ cout << "product B2" << endl; }
};

class Factory{
    public:
        virtual product_1 *creat1() = 0;
        virtual product_2 *creat2() = 0;
};

class FactoryA : Factory{
    public:
        product_1 *creat1(){ return new product_A1(); }
        product_2 *creat2(){ return new product_A2(); }
};

class FactoryB : Factory{
    public:
        product_1 *creat1(){ return new product_B1(); }
        product_2 *creat2(){ return new product_B2(); }
};

int main(){
    FactoryA *factoryA = new FactoryA();
    factoryA->creat1()->show();
    factoryA->creat2()->show();

    FactoryB *factoryB = new FactoryB();
    factoryB->creat1()->show();
    factoryB->creat2()->show();

    return 0;
}


猜你喜欢

转载自blog.csdn.net/u011074149/article/details/80949580