设计模式之工厂模式

来源:互联网 发布:域名有什么作用 编辑:程序博客网 时间:2024/06/03 23:41

一般来说,有三种工厂模式,即简单工厂模式(simple factory),工厂方法模式(factory method),抽象工厂模式(abstract factory)

工厂模式属于创建型模式,主要是为了解决直接用new来生成对象是产生的object与client之间的紧耦合,直接用new那么你必须要熟知每个object,采用工厂模式,你就用一个抽象的接口来创建各个不同的object,采用接口就很好的去除了这种耦合,下面是三种工厂模式的C++实现,代码可在本人github网页上下载:https://github.com/hugewave/DesignPattern

一、简单工厂模式

/* * Example of 'Simple Factory' design pattern. * Copyright (C) 2016 Leo Wang  * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */#include <iostream>#include <vector>using namespace std;class CProduct{    public:    virtual void Specification(){};};class CProductA:public CProduct{    public:    void Specification()    {        cout<<"I'm ProductA!"<<endl;    };};class CProductB:public CProduct{    public:    void Specification()    {        cout<<"I'm ProductB!"<<endl;    };};class CFactory{public:    CProduct* MakeProduct(char producttype)    {        CProduct* cp_product=NULL;        switch (producttype)        {        case 'A':            cp_product=new CProductA();            break;        case 'B':            cp_product=new CProductB();            break;        default:            break;        }        return cp_product;    };};void main(){    vector<CProduct*> v_product;    CFactory* cp_factory=new CFactory();    CProduct* cp_product;    cp_product=cp_factory->MakeProduct('A');    v_product.push_back(cp_product);    cp_product=cp_factory->MakeProduct('B');    v_product.push_back(cp_product);    vector<CProduct*>::iterator itr=v_product.begin();    for(;itr!=v_product.end();itr++)    {        (*itr)->Specification();    };};

二、工厂方法模式

/* * Example of 'Factory Method' design pattern. * Copyright (C) 2016 Leo Wang  * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */#include <iostream>#include <vector>using namespace std;class CProduct{    public:    virtual void Specification(){};};class CProductA:public CProduct{    public:    void Specification()    {        cout<<"I'm ProductA!"<<endl;    };};class CProductB:public CProduct{    public:    void Specification()    {        cout<<"I'm ProductB!"<<endl;    };};class CFactory{public:    virtual CProduct* MakeProduct()=NULL;};class CFactoryA:public CFactory{public:    CProduct* MakeProduct()    {        return new CProductA();    };};class CFactoryB:public CFactory{public:    CProduct* MakeProduct()    {        return new CProductB();    };};void main(){    vector<CProduct*> v_product;    CFactory* cp_factoryA=new CFactoryA();    CFactory* cp_factoryB=new CFactoryB();    CProduct* cp_product;    cp_product=cp_factoryA->MakeProduct();    v_product.push_back(cp_product);    cp_product=cp_factoryB->MakeProduct();    v_product.push_back(cp_product);    vector<CProduct*>::iterator itr=v_product.begin();    for(;itr!=v_product.end();itr++)    {        (*itr)->Specification();    };};

三、抽象工厂模式

/* * Example of 'Abstract Factory' design pattern. * Copyright (C) 2016 Leo Wang  * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */#include <iostream>#include <vector>using namespace std;class CProduct{public:    virtual void Specification(){};};class CProductA1:public CProduct{public:    void Specification()    {        cout<<"I'm ProductA1!"<<endl;    };};class CProductA2:public CProduct{public:    void Specification()    {        cout<<"I'm ProductA2!"<<endl;    };};class CProductB1:public CProduct{public:    void Specification()    {        cout<<"I'm ProductB1!"<<endl;    };};class CProductB2:public CProduct{public:    void Specification()    {        cout<<"I'm ProductB2!"<<endl;    };};class CFactory{public:    virtual CProduct* MakeProductA()=NULL;    virtual CProduct* MakeProductB()=NULL;};class CFactoryA:public CFactory{public:    CProduct* MakeProductA()    {        return new CProductA1();    };    CProduct* MakeProductB()    {        return new CProductB1();    };};class CFactoryB:public CFactory{public:    CProduct* MakeProductA()    {        return new CProductA2();    };    CProduct* MakeProductB()    {        return new CProductB2();    };};void main(){    vector<CProduct*> v_product;    CFactory* cp_factoryA=new CFactoryA();    CFactory* cp_factoryB=new CFactoryB();    CProduct* cp_product;    cp_product=cp_factoryA->MakeProductA();    v_product.push_back(cp_product);    cp_product=cp_factoryA->MakeProductB();    v_product.push_back(cp_product);    cp_product=cp_factoryB->MakeProductA();    v_product.push_back(cp_product);    cp_product=cp_factoryB->MakeProductB();    v_product.push_back(cp_product);    vector<CProduct*>::iterator itr=v_product.begin();    for(;itr!=v_product.end();itr++)    {        (*itr)->Specification();    };};
0 0
原创粉丝点击