///////////////////////////////////////////////////////////////////////// // policy.cpp - demonstrate programming to interfaces, policy level // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2002 // ///////////////////////////////////////////////////////////////////////// #include #include "creatable.h" // include object factory #include "implemInterface.h" // include implementation interface ///////////////////////////////////////////////////////////////////// // Note that policy does not include any implementation layer headers. // It includes just the factory and interface headers, which contain // no implementation details. // void main() { std::cout << "\n Demonstate Programming to Interfaces " << "\n ======================================\n"; ////////////////////////////////////////////////////////////////// // Create an implementation factory and use its interface to // register its objects with the factory. objFactory testerFactory; tester::registerObjects(testerFactory); ///////////////////////////////////////////////////////////////// // Policy needs to know how to access the ids of the implementation // classes it uses, but does not need to bind to the class names. tester *pTester1 = testerFactory.makeObj(tester::type(1)); pTester1->announcer(); // polymorphic call delete pTester1; tester *pTester2 = testerFactory.makeObj(tester::type(2)); pTester2->announcer(); // polymorphic call delete pTester2; 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"; }