工厂模式的例子-转载

下面以女娲造黑人,白人,黄种人的例子来介绍一下工厂模式。

1.工厂的接口,相当于造人工厂总部。

class IHumanFactory
{
public:
    IHumanFactory(void)
    {
    
    }
    ~IHumanFactory(void)
    {
    
    }
    virtual IHuman* CreateHuman() = 0;

};

2.造人各个的部门

class WhiteHumanFactory: public IHumanFactory
{
public:
    WhiteHumanFactory(void)
    {

    }
    ~WhiteHumanFactory(void)
    {
    
    }
    IHuman *CreateHuman()
    {
        return new WhiteHuman();
    }

};

class YellowHumanFactory: public IHumanFactory
{
public:
    YellowHumanFactory(void)
    {

    }
    ~YellowHumanFactory(void)
    {
    
    }
    IHuman *CreateHuman()
    {
        return new YellowHuman();
    }

};

class BlackHumanFactory: public IHumanFactory
{
public:
    BlackHumanFactory(void)
    {

    }
    ~BlackHumanFactory(void)
    {
    
    }
    IHuman *CreateHuman()
    {
        return new BlackHuman();
    }

};

3.各种人的特征。

class IHuman
{
public:
    IHuman(void)
    {

    }
    ~IHuman()
    {
    
    }
    virtual void Laugh() = 0;
    virtual void Cry() = 0;
    virtual void Talk() = 0;

};

class WhiteHuman : public IHuman
{
public:
    WhiteHuman(void)
    {
    
    }
    ~WhiteHuman(void)
    {
    
    }
    void Laugh()
    {
        std::cout << "白种人笑!" << std::endl;
    }
    void Cry()
    {
        std::cout << "白种人哭!" <<std::endl;
    }
    void Talk()
    {
        std::cout << "白种人说话!" <<std::endl;
    }
};

class YellowHuman : public IHuman
{
public:
    YellowHuman(void)
    {
    
    }
    ~YellowHuman(void)
    {
    
    }
    void Laugh()
    {
        std::cout << "黄种人笑!" << std::endl;
    }
    void Cry()
    {
        std::cout << "黄种人哭!" <<std::endl;
    }
    void Talk()
    {
        std::cout << "黄种人说话!" <<std::endl;
    }
};

class BlackHuman : public IHuman
{
public:
    BlackHuman(void)
    {
    
    }
    ~BlackHuman(void)
    {
    
    }
    void Laugh()
    {
        std::cout << "黑种人笑!" << std::endl;
    }
    void Cry()
    {
        std::cout << "黑种人哭!" <<std::endl;
    }
    void Talk()
    {
        std::cout << "黑种人说话!" <<std::endl;
    }
};

4.主函数

int main()
{
    std::cout << "#1.制造黄种人"<<std::endl;
    IHumanFactory *pHumanFactory = new YellowHumanFactory();
    IHuman * pHuman = pHumanFactory->CreateHuman();
    pHuman->Cry();
    pHuman->Laugh();
    pHuman->Talk();
    delete pHuman;
    delete pHumanFactory;

    std::cout << "#1.制造白种人"<<std::endl;
    IHumanFactory *pHumanFactory2 = new WhiteHumanFactory();
    IHuman * pHuman2 = pHumanFactory->CreateHuman();
    pHuman->Cry();
    pHuman->Laugh();
    pHuman->Talk();
    delete pHuman2;
    delete pHumanFactory2;

    std::cout << "#1.制造黑种人"<<std::endl;
    IHumanFactory *pHumanFactory3 = new BlackHumanFactory();
    IHuman * pHuman3 = pHumanFactory->CreateHuman();
    pHuman->Cry();
    pHuman->Laugh();
    pHuman->Talk();
    delete pHuman3;
    delete pHumanFactory3;

    getchar();
    return 0;

    
}

输出结果:

#1.制造黄种人
黄种人哭!
黄种人笑!
黄种人说话!
#1.制造白种人
白种人哭!
白种人笑!
白种人说话!
#1.制造黑种人
黑种人哭!
黑种人笑!
黑种人说话

来自:https://www.cnblogs.com/onlycxue/p/3428075.html

猜你喜欢

转载自blog.csdn.net/gaoenyang760525/article/details/84195082