///////////////////////////////////////////////////////////////////////// // creatable.cpp - defines object factory and provides a test stub // // ver 2.0 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////////// #include "creatable.h" #include ///////////////////////////////////////////////////////////////////////// // Note: // // The objFactory class has no physical dependency on any class that // // wishes to use it. You can see that by observing that all the // // factory code comes before any class code for classes that will be // // created by the factory, so it never sees, nor needs, any of those // // definitions. When we add a new class to be stored, we do not need // // to re-compile the objFactory. // ///////////////////////////////////////////////////////////////////////// #ifdef TEST_CREATABLE //----< test stub >-------------------------------------------- /////////////////////////////////////////////////////////////// // test0 class declaration - example creatable class // create() returns a pointer to test0 class test0 : public creator { public: void announcer(); }; //----< virtual function displays class identity >------------- void test0::announcer() { std::cout << "\n object of class " << id(); } /////////////////////////////////////////////////////////////// // tester abstract class declaration // - provides protocol for derived classes test1 and test2 class tester { public: virtual ~tester() { } virtual void announcer() = 0; }; // /////////////////////////////////////////////////////////////// // test1 class declaration - example creatable class // create() returns pointer to tester class test1 : public tester, public creator { public: virtual void announcer(); //////////////////////////////////// // see note below // static tester* create(); // static classID id(); }; //----< virtual function displays class identity >------------- void test1::announcer() { std::cout << "\n object of class " << id(); } /////////////////////////////////////////////////////////////// // You could use the two functions, below, instead of // inheriting from creator // //----< factory function - creates object >-------------------- // //tester* test1::create() { return new test1; } // //----< provide class identifier string >---------------------- // //classID test1::id() { return "test1"; } // ///////////////////////////////////////////////////////////////////////// // test2 class declaration - example creatable class class test2 : public tester , public creator { public: virtual void announcer(); }; //----< virtual function displays class identity >------------- void test2::announcer() { std::cout << "\n object of class " << id(); } // //----< entry point >---------------------------------------------------- void main() { std::cout << "\n Demonstate Object Factory and Creatable Objects " << "\n =================================================\n"; ////////////////////////////////////////////////////////////////// // show that factory can return pointers to new objects of any one // class derived from creatable objFactory test0Factory; // returns test0 pointer to test0 object // this is just a very complicated new operator test0Factory.reg(test0::id(),test0::create); test0 *pNewTest0 = test0Factory.makeObj(test0::id()); pNewTest0->announcer(); delete pNewTest0; std::cout << std::endl; std::cout << "\n The test0Factory delivers, via test0 pointers," << "\n only test0 objects."; std::cout << "\n\n"; ////////////////////////////////////////////////////////////////// // show that factory can return base pointers to new objects // of any class derived from base, provided that they provide // create() and id() functions. objFactory testerFactory; // returns base pointer attached to derived object // this is really what the factory is intended to do testerFactory.reg(test1::id(),test1::create); testerFactory.reg(test2::id(),test2::create); tester *pNewTester = testerFactory.makeObj(test1::id()); pNewTester->announcer(); // polymorphic call delete pNewTester; pNewTester = testerFactory.makeObj(test2::id()); pNewTester->announcer(); // polymorphic call delete pNewTester; std::cout << std::endl; std::cout << "\n The testerFactory delivers, via tester pointers," << "\n objects that derive from tester. The tester interface" << "\n provides a protocol for clients of all derived class" << "\n objects, like test1 and test2, to use, without needing" << "\n to know the derived class of the object they are using."; std::cout << "\n\n"; } #endif