///////////////////////////////////////////////////////////////////// // TestFactory.cpp - Demonstrates basic functionality of // // Loki::Factory // // // // Loki is a template-based library developed by the author of: // // Modern C++ Design, Andrei Alexandrescu, Addison-Wesley, 2001 // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2004 // ///////////////////////////////////////////////////////////////////// // Required Files: // // TestFactory.cpp, Factory.h, Singleton.h, Singleton.cpp, // // SmallObj.h, SmallObj.cpp, static_check.h, Threads.h, // // TypeManip.h, TypeList.h, NullType, LokiTypeInfo.h, // // AssocVector.h // ///////////////////////////////////////////////////////////////////// // // template // < // class AbstractProduct, // typename IdentifierType, // typename ProductCreator = AbstractProduct* (*)(), // template // class FactoryErrorPolicy = DefaultFactoryError // > // class Factory // : public FactoryErrorPolicy // #include "Factory.h" #include #include using namespace Loki; ///////////////////////////////////////////////////////////////////// // Product Interface and Product classes - created by factory struct IProduct { virtual ~IProduct() {} virtual void say()=0; }; class ProductA : public IProduct { public: static IProduct* Create() { return new ProductA(); } static std::string ID() { return "ProductA"; } void say() { std::cout << "\n " << ID(); } }; class ProductB : public IProduct { public: static IProduct* Create() { return new ProductB(); } static std::string ID() { return "ProductB"; } void say() { std::cout << "\n " << ID(); } }; // ///////////////////////////////////////////////////////////////////// // Demonstration void main() { std::cout << "\n Testing Factory " << "\n ================================================\n"; Factory factory; factory.Register(ProductA::ID(),ProductA::Create); factory.Register(ProductB::ID(),ProductB::Create); IProduct* pProd = factory.CreateObject(ProductA::ID()); pProd->say(); delete pProd; pProd = factory.CreateObject(ProductB::ID()); pProd->say(); delete pProd; std::cout << "\n\n"; }