- . - factory ,
,
a) factory
b) .
. :
int main(int argc, char* argv[])
{
DerivedA::announce();
IInterface * prt = SingeltonFactory::create(DerivedA::_type);
delete prt;
return 0;
}
DerivedA :
class DerivedA :
public IInterface,
public StaticBase<DerivedA>
{
public:
using StaticBase::announce;
static IInterface * create(){ return new DerivedA; }
static const std::string _type;
};
const std::string DerivedA::_type=std::string("DerivedA");
, _type create, :
template<class TDerived>
class StaticBase
{
protected:
static void announce()
{
// register into factory:
SingeltonFactory::registerFun(TDerived::_type,TDerived::_create());
// The call of _type and _create implicitly forces the derived class to implement these, if it is deriving from this Base class
}
};
factory
std::map<std::string,tFunPtr>
typedef tFunPtr:
typedef IInterface * (*tFunPtr)(void);
" ", . , create
? factory?