C++ 工厂模式和策略模式以及单件模式小练习

// Factory.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#pragma warning(disable:4786)
#include <map>
#include <iostream>
using namespace std;
class IFactory;


enum Method
{
    ADD,
    DEC,
    MULT  
};


typedef enum Method TYPE;


class Calc
{
public:
    virtual int Reqeust(int a, int b) = 0;
};


class CalcA : public Calc
{
public:
    virtual int Reqeust(int a, int b)
    {
        return a + b;
    }
};


class CalcB : public Calc
{
public:
    virtual int Reqeust(int a, int b)
    {
        return a - b;
    }
};


class CalcC : public Calc
{
public:
    virtual int Reqeust(int a, int b)
    {
        return a * b;
    }
};


class IFactory
{
public:
    ~IFactory()
    {
        typedef map<TYPE, IFactory*>::iterator ITER;
        map<TYPE, IFactory*>& thismap = IFactory::GetMap();
        for (ITER it = thismap.begin(); it != thismap.end(); ++it)
        {
            IFactory* pFactory = it->second;
            it = thismap.erase(it);
            delete pFactory;
            pFactory = NULL;
        }   
    }
public:
    virtual Calc* CreateObject() = 0;
    static map<TYPE, IFactory*>& GetMap();
private:
    static map<TYPE, IFactory*> g_map;
};


map<TYPE, IFactory*> IFactory::g_map;
map<TYPE, IFactory*>& IFactory::GetMap(){return IFactory::g_map;}
class FactoryA : public IFactory
{
private:
    FactoryA(){}
public:
    virtual Calc* CreateObject()
    {
        return new CalcA;
    }
public:
    static IFactory* Create();
private:
    static IFactory* m_pointer;
};


IFactory* FactoryA::m_pointer = FactoryA::Create();


IFactory* FactoryA::Create()
{
    map<TYPE, IFactory*>& thismap = IFactory::GetMap();


    IFactory* p =  new FactoryA;
    thismap[ADD] = p;
    return p;
}


class FactoryB : public IFactory
{
private:
    FactoryB(){}
public: 
    virtual Calc* CreateObject()
    {
        return new CalcB;
    }
public:
    static IFactory* Create();
private:
    static IFactory* m_point;
};


IFactory* FactoryB::m_point = FactoryB::Create();


IFactory* FactoryB::Create()
{
    map<TYPE, IFactory*>& thismap = IFactory::GetMap();
    IFactory* p =  new FactoryB;
    thismap[DEC] = p;
    return p;
}


class FactoryC : public IFactory
{
private:
    FactoryC(){}
public: 
    virtual Calc* CreateObject()
    {
        return new CalcC;
    }
public:
    static IFactory* Create();
private:
    static IFactory* m_point;
};


IFactory* FactoryC::m_point = FactoryC::Create();


IFactory* FactoryC::Create()
{
    map<TYPE, IFactory*>& thismap = IFactory::GetMap();
    IFactory* p =  new FactoryC;
    thismap[MULT] = p;
    return p;
}


class Strategy
{
public:
    Strategy()
    {
        m_pFactory = NULL;
    }
public:
    virtual int dowork(TYPE t, int a, int b)
    {
        map<TYPE, IFactory*>& thismap = IFactory::GetMap();
        m_pFactory = thismap[t];


        if (m_pFactory != NULL)
        {
            Calc* pCalc = m_pFactory->CreateObject();


            if (pCalc != NULL)
            {
                int nResult =  pCalc->Reqeust(a,b);
                delete pCalc;
                pCalc = NULL;
                return nResult;
            }
        }
        return 0;
    }
private:
     IFactory* m_pFactory;
};


int main(int argc, char* argv[])
{
    Strategy c;
    int nResult = c.dowork(ADD, 22,2);
    cout << nResult << endl;
    Strategy d;
    nResult = d.dowork(DEC, 22,2);
    cout << nResult << endl;
    Strategy e;
    nResult = e.dowork(MULT, 22,2);
    cout << nResult << endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值