Implementing Abstract Factory as an STL Container by Jason Shankel Example 1: set > myIntSet; for (int val=0;val<10;val++) myIntSet.insert(val); for (set >::iterator it = myIntSet.begin(); it!=myIntSet.end(); it++) cout << *it; Example 2: set >::iterator find_it; //Object in set //(*find_it) == 5 find_it = myIntSet.find(5); //Object not in set //find_it == myIntSet.end() find_it = myIntSet.find(20); Listing One IFooFactory *g_theFooMaker; void main() { g_theFooMaker = MakeFooFactory(); IFoo *aNewFoo = g_theFooMaker->MakeNewObject(); } Listing Two class IWindow {...}; class CMacWindow : public IWindow {...}; class CWindowsWindow : public IWindow {...}; class IWindowFactory { ... virtual IWindow *MakeWindow() = 0; }; class CMacWindowFactory : public IWindowFactory { ... virtual IWindow *MakeWindow(){ return new CMacWindow; } }; class CWindowsWindowFactory : public IWindowFactory { ... virtual IWindow *MakeWindow(){ return new CWindowsWindow; } }; Listing Three class CWindowFactory : public IWindowFactory { ... public: CWindowFactory(IWindow *aWindow){ m_prototype = aWindow; }; virtual IWindow *MakeWindow(){ return m_prototype->MakeCopy(); }; private: IWindow *m_prototype; }; Listing Four template class IAbstractFactory { ... virtual I *MakeNewObject()=0; virtual void ReleaseObject() = 0; }; //C inherits from I template class CConcreteFactory : public IAbstractFactory { ... virtual I *MakeNewObject(){ return new C; }; virtual void ReleaseObject(I *anObject){ delete anObject; }; }; Listing Five typedef CConcreteFactory CWindowsWindowFactory; typedef CConcreteFactory CMacWindowFactory; Listing Six typedef CConcreteFactory CWinsockFactory; typedef CConcreteFactory CDirectPlayFactory; Listing Seven template class IAbstractFactory { ... typedef set > containerType; containerType m_objects; }; template class CConcreteFactory : public IAbstractFactory { ... virtual I *MakeNewObject(){ C *newObj = new C; if (newObj) m_objects.insert(newObj); return newObj; } virtual void DestroyObject(I *anObject){ if (m_objects.find(anObject) != m_objects.end()){ m_objects.erase(anObject); delete anObject; } else assert(false); }; }; Listing Eight //The Iterators typedef containerType::iterator iterator; typedef containerType::const_iterator const_iterator; typedef containerType::reverse_iterator reverse_iterator; typedef containerType::const_reverse_iterator const_reverse_iterator; //The Sequences iterator begin(){ return m_objects.begin(); } iterator end() { return m_objects.end(); } reverse_iterator rbegin(){ return m_objects.rbegin(); } reverse_iterator rend(){ return m_objects.rend(); } //Typedefs typedef containerType::size_type size_type; typedef containerType::value_type value_type; typedef containerType::reference reference; typedef containerType::const_reference const_reference; typedef containerType::difference_type difference_type; Listing Nine for ( IAbstractFactory::iterator it = g_theWindowFactory->begin(); it!= g_theWindowFactory->end(); it++) { (*it)->Refresh(); } Listing Ten typedef struct _tagGUIFactory { IAbstractFactory * WindowFactory; IAbstractFactory * ScrollBarFactory; IAbstractFactory * MenuFactory; } GUIFactory; Listing Eleven GUIFactory g_theGUIFactory; SetupGUIFactory(&g_theGUIFactory); IWindow *theMainWindow = g_theGUIFactory.WindowFactory-> MakeNewObject(); 5