#include <iostream>
#include <string>
#include <map>
using namespace std;
class IAnimal //工厂创建出的接口
{
public:
virtual int GetNumberOfLegs() const = 0;
virtual void Speak() = 0;
virtual void Free() = 0;
};
typedef IAnimal* (__stdcall *CreateAnimalFn)(void); //创建指针
// IAnimal implementations
class Cat : public IAnimal
{
public:
int GetNumberOfLegs() const { return 4; }
void Speak() { cout << "Meow" << endl; }
void Free() { delete this; }
static IAnimal * __stdcall Create() { return new Cat(); } //创建函数
};
class Dog : public IAnimal
{
public:
int GetNumberOfLegs() const { return 4; }
void Speak() { cout << "Woof" << endl; }
void Free() { delete this; }
static IAnimal * __stdcall Create() { return new Dog(); }
};
class Spider : public IAnimal // Yeah it isn’t really an animal…
{
public:
int GetNumberOfLegs() const { return 8; }
void Speak() { cout << endl; }
void Free() { delete this; }
static IAnimal * __stdcall Create() { return new Spider(); }
};
class Horse : public IAnimal
{
public:
int GetNumberOfLegs() const { return 4; }
void Speak() { cout << "A horse is a horse, of course, of course." << endl; }
void Free() { delete this; }
static IAnimal * __stdcall Create() { return new Horse(); }
};
// Factory for creating instances of IAnimal
class AnimalFactory
{
private:
AnimalFactory();
AnimalFactory(const AnimalFactory &);
AnimalFactory &operator=(const AnimalFactory &);
typedef map<string, CreateAnimalFn> FactoryMap;
FactoryMap m_FactoryMap;
public:
~AnimalFactory() { m_FactoryMap.clear(); }
static AnimalFactory *Get()
{
static AnimalFactory instance;
return &instance;
}
void Register(const string &animalName, CreateAnimalFn pfnCreate);
IAnimal *CreateAnimal(const string &animalName);
};
/* Animal factory constructor.
Register the types of animals here.
*/
AnimalFactory::AnimalFactory()
{
Register("Horse", &Horse::Create);
Register("Cat", &Cat::Create);
Register("Dog", &Dog::Create);
Register("Spider", &Spider::Create);
}
void AnimalFactory::Register(const string &animalName, CreateAnimalFn pfnCreate)
{
m_FactoryMap[animalName] = pfnCreate;
}
IAnimal *AnimalFactory::CreateAnimal(const string &animalName)
{
FactoryMap::iterator it = m_FactoryMap.find(animalName);
if( it != m_FactoryMap.end() )
return it->second(); //加()就调用了指向的函数
return NULL;
}
int main( int argc, char **argv )
{
IAnimal *pAnimal = NULL;
string animalName;
while( pAnimal == NULL )
{
cout << "Type the name of an animal or ‘q’ to quit: ";
cin >> animalName;
if( animalName == "q" )
break;
IAnimal *pAnimal = AnimalFactory::Get()->CreateAnimal(animalName);
if( pAnimal )
{
cout << "Your animal has " << pAnimal->GetNumberOfLegs() << " legs." << endl;
cout << "Your animal says: ";
pAnimal->Speak();
}
else
{
cout << "That animal doesn’t exist in the farm! Choose another!" << endl;
}
if( pAnimal )
pAnimal->Free();
pAnimal = NULL;
animalName.clear();
}
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Operation
{
public:
virtual ~Operation(){}
virtual double operate() = 0;
virtual void free() = 0;
void setNum1(double value){ m_fNum1 = value; }
void setNum2(double value){ m_fNum2 = value; }
protected:
double m_fNum1;
double m_fNum2;
};
class AddOperation : public Operation
{
public:
double operate()
{
return m_fNum1 + m_fNum2;
}
void free()
{
delete this;
}
};
class SubOperation : public Operation
{
public:
double operate()
{
return m_fNum1 - m_fNum2;
}
void free()
{
delete this;
}
};
class MultiOperation : public Operation
{
public:
double operate()
{
return m_fNum1 * m_fNum2;
}
void free()
{
delete this;
}
};
class DivOperation : public Operation
{
public:
double operate()
{
if (m_fNum2 < 0.000001)
{
cout<< "除数不能为零!" <<endl;
return 0;
}
return m_fNum1 / m_fNum2;
}
void free()
{
delete this;
}
};
class OperationFactory
{
public:
static OperationFactory *Get()
{
static OperationFactory oOperFac;
return &oOperFac;
}
Operation *CreateOperate(char c)
{
switch(c)
{
case '+':
return new AddOperation();
case '-':
return new SubOperation();
case '*':
return new MultiOperation();
case '/':
return new DivOperation();
default:
break;
}
}
private:
OperationFactory(){}
OperationFactory(const OperationFactory &);
OperationFactory & operator=(const OperationFactory&);
};
int main()
{
OperationFactory *pFac = OperationFactory::Get();
Operation *oper = pFac->CreateOperate('+');
oper->setNum1(100);
oper->setNum2(200);
cout<<oper->operate()<<endl;
oper->free();
oper = pFac->CreateOperate('*');
oper->setNum1(18);
oper->setNum2(20);
cout<<oper->operate()<<endl;
oper->free();
return 0;
}
factory 简单工厂
最新推荐文章于 2022-02-28 11:49:34 发布