/////////////////////////////////////////////////////////////////////////// // Client.cpp - User of Concrete Products through an Abstract Interface // // ver 1.2 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2004 // /////////////////////////////////////////////////////////////////////////// /* ver 1.2 - removed function useInterfaces ver 1.1 - added client deletion of pointers handed out by factory */ #include "AbstractProduct.h" #include "Factory.h" #include /////////////////////////////////////////////////////////////// // Limitations: // - Need one factory for each inheritance hierarcy. // - Clients have to use the same language to talk with every // object they use from a given factory. // - With the help of RTTI clients can actually do more than // that. // - They can use mixin interfaces by querying for additional // interfaces int main() { std::cout << "\n Demonstrating use of Factories and Interfaces " << "\n ===============================================\n"; Factory f; AbstractProduct* pPr; ///////////////////////////////////////////////////////////// // Create and Use Product 1 pPr = f.CreateProduct(1); if(pPr) { std::cout << "\n 1st product created is " << pPr->OpA() << ". Its size is " << pPr->OpB() << " bytes."; Interface1* pI1 = dynamic_cast(pPr); if (pI1) { std::cout << "\n " << pI1->OpC(); } else std::cout << "\n can't use Interface1"; Interface2* pI2 = dynamic_cast(pPr); if (pI2) { std::cout << "\n " << pI2->OpD(); } else std::cout << "\n can't use Interface2"; delete pI1; delete pI2; } std::cout << "\n\n"; ///////////////////////////////////////////////////////////// // Create and Use Product 2 pPr = f.CreateProduct(2); if(pPr) { std::cout << "\n 2nd product created is " << pPr->OpA() << ". Its size is " << pPr->OpB() << " bytes."; Interface1* pI1 = dynamic_cast(pPr); if (pI1) { std::cout << "\n " << pI1->OpC(); } else std::cout << "\n can't use Interface1"; Interface2* pI2 = dynamic_cast(pPr); if (pI2) { std::cout << "\n " << pI2->OpD(); } else std::cout << "\n can't use Interface2"; delete pI1; delete pI2; pPr = nullptr; pI1 = nullptr; pI2 = nullptr; } // Don't delete pPr. The product it pointed to has been deleted. std::cout << "\n\n"; return 0; }