C++工厂类和单例模式的结合使用

来源:互联网 发布:apache flink 安装 编辑:程序博客网 时间:2024/05/16 17:02

单例模式:
简单来说一个类只有一个实例且封装性好。这里用宏定义实现。

animal_singleton.h

#pragma once#include <iostream>#define IMPLEMENTION_SINGLETON_CLASS( Type ) \public:                                      \    static Type* GetInstance()               \    {                                        \        static Type oInstance;               \        return &oInstance;                   \    }                                        \private:                                     \    Type( const Type& )                      \    {                                        \    }                                        \                                             \    Type& operator= ( const Type& )          \    {                                        \        return *this;                        \    }

工厂模式:
简单来说,工厂模式减少文件之间的依赖关系,一是可以优化编译,即实现部分改变了而客户不需要重新编译自己的文件;二是实现开放和封闭原则,有利于维护和扩展。
一个animal的例子,animal作为一个接口,由cat和dog来实现,并用factory对cat和dog进行封装,客户端只要有animal接口和factory头文件即可实现cat和dog的具体内容,实现了分离并且有利于维护。

animal_interface.h

#pragma once#include <iostream>namespace FactoryAndInstance{    class animalIF    {    public:        animalIF(void)        {        }        virtual ~animalIF(void)        {        }        virtual void LikeEat() = 0;    };}

animal_cat.h

#pragma onece#include "animal_singleton.h"#include "animal_interface.h"namespace FactoryAndInstance{    class CCat:public animalIF    {        IMPLEMENTION_SINGLETON_CLASS(CCat);    public:        CCat();        ~CCat();        void LikeEat();    };}

animal_cat.cpp

#include "animal_cat.h"#include <iostream>using namespace std;namespace FactoryAndInstance{    CCat::CCat()    {    }    CCat::~CCat()    {    }    void CCat::LikeEat()    {        cout<<"cat like eating fish"<<endl;    }}

animal_dog.h

#pragma onece#include "animal_singleton.h"#include "animal_interface.h"namespace FactoryAndInstance{    class CDog:public animalIF    {        IMPLEMENTION_SINGLETON_CLASS(CDog);    public:        CDog();        ~CDog();    public:        void LikeEat();    };}

animal_dog.cpp

#include "animal_dog.h"#include <iostream>using namespace std;namespace FactoryAndInstance{    CDog::CDog()    {    }    CDog::~CDog()    {    }    void CDog::LikeEat()    {        cout<<"dog like eating meat"<<endl;    }}

animal_factory.h

#pragma once#include "animal_singleton.h"namespace FactoryAndInstance{    class animalIF;    class CAnimalFactory    {        IMPLEMENTION_SINGLETON_CLASS(CAnimalFactory);    private:        CAnimalFactory()        {        }    public:        ~CAnimalFactory()        {        }        animalIF* GetCatInstance();        animalIF* GetDogInstance();    };}

animal_factory.cpp

#include "animal_factory.h"#include "animal_cat.h"#include "animal_dog.h"namespace FactoryAndInstance{    animalIF* CAnimalFactory::GetCatInstance()    {        return CCat::GetInstance();    }    animalIF* CAnimalFactory::GetDogInstance()    {        return CDog::GetInstance();    }}

客户实现部分:
main

#include <iostream>#include "animal_factory.h"#include "string"#include "animal_interface.h"using namespace std;using namespace FactoryAndInstance;int main() {    animalIF* pCat = CAnimalFactory::GetInstance()->GetCatInstance();    pCat->LikeEat();    animalIF* pDog = CAnimalFactory::GetInstance()->GetCatInstance();    pDog->LikeEat();    system("PAUSE");    return 0; }
0 0
原创粉丝点击