我是靠谱客的博主 大胆芝麻,最近开发中收集的这篇文章主要介绍C++ 工厂模式示例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Java 参考 https://blog.csdn.net/u012156116/article/details/80857255

1、简单工厂模式

#include <iostream>
#include <map>
using namespace std;

class IPerson
{
public:
    virtual void printRole(){}
    static IPerson* create();
};

class TSaler:IPerson
{
public:
    virtual void printRole()
    {
        cout << "I'm a Saler." << endl;
    }
    static IPerson* create()
    {
        TSaler *pstSaler = new TSaler();        
        return (IPerson*)pstSaler;
    }
};

class TBuyer:IPerson
{
public:
    virtual void printRole()
    {
        cout << "I'm a Buyer." << endl;
    }
    static IPerson* create()
    {
        TBuyer *pstBuyer = new TBuyer();  
        return (IPerson*)pstBuyer;
    }
};

enum ROLE_E
{
    ROLE_SALER,
    ROLE_BUYER,
    ROLE_BUTT
};

const map<ROLE_E, IPerson*> g_personTable = {
    {ROLE_SALER, TSaler::create()},
    {ROLE_BUYER, TBuyer::create()}
};

class EasyFactory
{
public:
    static IPerson* createRole(ROLE_E role)
    {
        map<ROLE_E, IPerson*>::const_iterator itr = g_personTable.begin();
        for (; itr != g_personTable.end(); itr++)
        {
            if (role == itr->first)
            {
                return itr->second;
            }
        }
        return nullptr;
    }
};


int main()
{
    cout << "enter" << endl;

    IPerson *pstSaler = EasyFactory::createRole(ROLE_SALER);
    if (nullptr != pstSaler)
    {
        pstSaler->printRole();
        delete pstSaler;
        pstSaler = nullptr;
    }
    else
    {
        cout << "create saler failed." << endl;
    }

    IPerson *pstBuyer = EasyFactory::createRole(ROLE_BUYER);
    if (nullptr != pstBuyer)
    {
        pstBuyer->printRole();
        delete pstBuyer;
        pstBuyer = nullptr;
    }
    else
    {
        cout << "create buyer failed." << endl;
    }

    cout << "exit" << endl;
    return 0;
}

执行结果

PS E:workspacesampleFactory> g++ .EasyFactory.cpp
PS E:workspacesampleFactory> ./a
enter
I'm a Saler.
I'm a Buyer.
exit

2、工厂方法模式

#include <iostream>
using namespace std;

class IPerson
{
public:
    virtual void printRole(){}
};

class TSaler:IPerson
{
public:
    virtual void printRole()
    {
        cout << "I'm a Saler." << endl;
    } 
};

class TBuyer:IPerson
{
public:
    virtual void printRole()
    {
        cout << "I'm a Buyer." << endl;
    }  
};

class IFactory
{
public:
    virtual IPerson* createRole(){return nullptr;}
    static IFactory* create();
};

class TSalerFactory
{
public:
    virtual IPerson* createRole()
    {
        TSaler *pstSaler = new TSaler();
        return (IPerson*)pstSaler;
    }
    static IFactory* create()
    {
        TSalerFactory *pstFactory = new TSalerFactory();
        return (IFactory*)pstFactory;
    }
};

class TBuyerFactory
{
public:
    virtual IPerson* createRole()
    {
        TBuyer *pstBuyer = new TBuyer();
        return (IPerson*)pstBuyer;
    }    
    static IFactory* create()
    {
        TBuyerFactory *pstFactory = new TBuyerFactory();
        return (IFactory*)pstFactory;
    }
};

int main()
{
    cout << "enter" << endl;

    IFactory *pstSalerFactory = TSalerFactory::create();
    if (nullptr == pstSalerFactory)
    {
        cout << "new saler factory failed." << endl;
        return -1;
    }

    IPerson *pstSaler = pstSalerFactory->createRole();
    if (nullptr == pstSaler)
    {
        cout << "new saler failed." << endl;
        delete pstSalerFactory;
        pstSalerFactory = nullptr;
        return -1;
    }
    pstSaler->printRole();
    delete pstSaler;
    delete pstSalerFactory;
    pstSaler = nullptr;
    pstSalerFactory = nullptr;

    IFactory *pstBuyerFactory = TBuyerFactory::create();
    if (nullptr == pstBuyerFactory)
    {
        cout << "new buyer factory failed." << endl;
        return -1;
    }

    IPerson *pstBuyer = pstBuyerFactory->createRole();
    if (nullptr == pstBuyer)
    {
        cout << "new buyer failed." << endl;
        delete pstBuyerFactory;
        pstBuyerFactory = nullptr;
        return -1;
    }
    pstBuyer->printRole();
    delete pstBuyer;
    delete pstBuyerFactory;
    pstBuyer = nullptr;
    pstBuyerFactory = nullptr;

    cout << "exit" << endl;
    return 0;
}

执行结果

PS E:workspacesampleFactory> g++ .FactoryMethod.cpp
PS E:workspacesampleFactory> ./a
enter
I'm a Saler.
I'm a Buyer.
exit

3、抽象工厂模式

#include <iostream>
using namespace std;

class IPerson
{
public:
    virtual void printRole(){}
};

class TSaler:IPerson
{
public:
    virtual void printRole()
    {
        cout << "I'm a Saler." << endl;
    } 
};

class IProduct
{
public:
    virtual void printName(){}
};

class TBeer:IProduct
{
public:
    virtual void printName()
    {
        cout << "A dozen of beer." << endl;
    }
};

class IFactory
{
public:
    virtual IPerson* createRole(){return nullptr;}
    virtual IProduct* createProduct(){return nullptr;}
    static IFactory* create();
};

class TShopFactory
{
public:
    virtual IPerson* createRole()
    {
        TSaler *pstSaler = new TSaler();
        return (IPerson*)pstSaler;
    }

    virtual IProduct* createProduct()
    {
        TBeer *pstBeer = new TBeer();
        return (IProduct*)pstBeer;
    }

    static IFactory* create()
    {
        TShopFactory *pstFactory = new TShopFactory();
        return (IFactory*)pstFactory;
    }
};


int main()
{
    cout << "enter" << endl;

    IFactory *pstShopFactory = TShopFactory::create();
    if (nullptr == pstShopFactory)
    {
        cout << "create factory failed." << endl;
        return -1;
    }

    IPerson *pstSaler = pstShopFactory->createRole();
    if (nullptr == pstSaler)
    {
        delete pstShopFactory;
        pstShopFactory = nullptr;
        cout << "create saler failed." << endl;
        return -1;
    }
    pstSaler->printRole();
    delete pstSaler;
    pstSaler = nullptr;

    IProduct *pstBeer = pstShopFactory->createProduct();
    if (nullptr == pstBeer)
    {
        delete pstShopFactory;
        pstShopFactory = nullptr;
        cout << "create beer failed." << endl;
        return -1;
    }
    pstBeer->printName();
    delete pstBeer;
    pstBeer = nullptr;

    delete pstShopFactory;
    pstShopFactory = nullptr;

    cout << "exit" << endl;
    return 0;
}

执行结果

PS E:workspacesampleFactory> g++ .AbstractFactroy.cpp
PS E:workspacesampleFactory> ./a
enter
I'm a Saler.
A dozen of beer.
exit

最后

以上就是大胆芝麻为你收集整理的C++ 工厂模式示例的全部内容,希望文章能够帮你解决C++ 工厂模式示例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部