Abstract Factory Model
/*************************************** * ******************************** // * Design mode Abstract Factory the Mode abstraction factory mode is very similar to the factory method mode: A specific factory class corresponds to a specific product class, each product class has only one specific product difference: a specific factory class corresponds to several specific product classes, each product category has several types of ownership, such as the example of imported auto parts in this article, the two parts [wheel] and [engine ], the two products, wheel and engine, have different ownership, that is, wheel: Mercedes-Benz wheel, BMW wheel, and engine ]: as mentioned in [big talk], the user table and department table are different product categories. Logically, the classification of products in different categories is equivalent. For example, the table [user] and [Department] mentioned above, and the [user] and [Department] tables can be in different databases [sqlserver And [Oracle. /*************************************** *********************************/
Demo
This article takes imported auto parts as an Example
Two products: [wheel] [Engine]
Some of these two products belong to: [Mercedes-Benz] [BMW]
Code on
[Wheel], [Mercedes-Benz wheel], [BMW wheel]
// [Wheel class] class iwheel {public: iwheel (); Virtual ~ Iwheel (); Virtual void import () = 0 ;}; iwheel: iwheel () {} iwheel ::~ Iwheel () {}// [Benz wheel class] class cbenzwheel: Public iwheel {public: cbenzwheel (); Virtual ~ Cbenzwheel (); Virtual void import () ;}; cbenzwheel: cbenzwheel () {} cbenzwheel ::~ Cbenzwheel () {} void cbenzwheel: Import () {cout <"Introduction of Mercedes-Benz brand wheel" <Endl ;}// [BMW wheel class] class cbmwwheel: public iwheel {public: cbmwwheel (); Virtual ~ Cbmwwheel (); Virtual void import () ;}; cbmwwheel: cbmwwheel () {} cbmwwheel ::~ Cbmwwheel () {} void cbmwwheel: Import () {cout <"Introducing BMW brand wheel" <Endl ;}
[Engine], [Benz engine], [BMW engine]
// [Engine class] class iengine {public: iengine (); Virtual ~ Iengine (); Virtual void import () = 0;}; iengine: iengine () {} iengine ::~ Iengine () {}// [Benz engine class] class cbenzengine: Public iengine {public: cbenzengine (); Virtual ~ Cbenzengine (); Virtual void import () ;}; cbenzengine: cbenzengine () {} cbenzengine ::~ Cbenzengine () {} void cbenzengine: Import () {cout <"Introduction of benz-branded engines" <Endl ;}// [BMW engine class] class cbmwengine: public iengine {public: cbmwengine (); Virtual ~ Cbmwengine (); Virtual void import () ;}; cbmwengine: cbmwengine () {} cbmwengine ::~ Cbmwengine () {} void cbmwengine: Import () {cout <"Introducing BMW-branded engines" <Endl ;}
[Abstract Factory], [Mercedes-Benz factory], and [BMW factory]
// [Abstract factory class] class ifacloud {public: ifacloud (); Virtual ~ Ifacloud (); Virtual iwheel * createwheel () = 0; virtual iengine * createengine () = 0; protected: iwheel * m_ptrwheel; iengine * m_ptrengine;}; ifacloud: ifacloud (): m_ptrengine (null), m_ptrwheel (null) {} ifacloud ::~ Ifacloud () {If (null! = M_ptrwheel) {Delete m_ptrwheel; m_ptrwheel = NULL;} If (null! = M_ptrengine) {Delete m_ptrengine; m_ptrengine = NULL ;}}// [Benz factory class] class cbenzfactory: Public ifacfactory {public: cbenzfactory (); Virtual ~ Cbenzfactory (); Virtual iwheel * createwheel (); Virtual iengine * createengine ();}; cbenzfactory: cbenzfactory (): ifacloud () {} cbenzfactory ::~ Cbenzfactory () {} iwheel * cbenzfactory: createwheel () {return (m_ptrwheel = new cbenzwheel ();} iengine * cbenzfactory: createengine () {return (m_ptrengine = new cbenzengine ();} // [BMW factory class] class cbmwfactory: Public ifacloud {public: cbmwfactory (); Virtual ~ Cbmwfactory (); Virtual iwheel * createwheel (); Virtual iengine * createengine ();}; cbmwfactory: cbmwfactory (): ifacloud () {} cbmwfactory ::~ Cbmwfactory () {} iwheel * cbmwfactory: createwheel () {return (m_ptrwheel = new cbmwwheel ();} iengine * cbmwfactory: createengine () {return (m_ptrengine = new cbmwengine ());}
Client
int _tmain(int argc, _TCHAR* argv[]){ IFactory *pFactory = new CBenzFactory(); IWheel* pWheel = pFactory->CreateWheel(); pWheel->Import(); cout<<endl; IEngine* pEngine = pFactory->CreateEngine(); pEngine->Import(); cout<<endl; cout<<endl<<endl; delete pFactory; pFactory = new CBMWFactory(); pWheel = pFactory->CreateWheel(); pWheel->Import(); cout<<endl; pEngine = pFactory->CreateEngine(); pEngine->Import(); cout<<endl<<endl; delete pFactory; return 0;}
Running result