// 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;
}
//
#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;
}