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; 
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值