C++工廠類和單例模式的結合使用

單例模式:
簡單來說一個類只有一個實例且封裝性好。這裏用宏定義實現。

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; 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章