When I read COM, I wrote a line of code and found that it was the factory method mode...
No more nonsense
# Include "stdafx. H" # include <iostream> using namespace STD; Class interfacea {public: interfacea (); Virtual ~ Interfacea (); Virtual void func1 () = 0; virtual void func2 () = 0 ;}; interfacea: interfacea () {} interfacea ::~ Interfacea () {} class componetb: Public interfacea {public: componetb (); Virtual ~ Componetb (); Virtual void func1 (); Virtual void func2 () ;}; componetb: componetb () {cout <"component B creation" <Endl;} componetb: :~ Componetb () {cout <"component B destructor" <Endl;} void componetb: func1 () {cout <"compoentb :: func1 is called \ n ";} void componetb: func2 () {cout <" componetb: func2 is called \ n ";} class componetc: Public interfacea {public: componetc (); Virtual ~ Componetc (); Virtual void func1 (); Virtual void func2 () ;}; componetc: componetc () {cout <"component C creation" <Endl;} componetc: :~ Componetc () {cout <"component C destructor" <Endl;} void componetc: func1 () {cout <"componentc :: fucn1 is called \ n ";} void componetc: func2 () {cout <" componetc: func2 is called \ n ";} class ifacloud {public: ifacloud (); virtual ~ Ifacloud (); Virtual interfacea * createinstance () = 0; protected: interfacea * m_ptria;}; ifacloud: ifacloud (): m_ptria (null) {} ifacloud ::~ Ifacloud () {If (null! = M_ptria) delete m_ptria; m_ptria = NULL;} class factoryb: Public ifacloud {public: factoryb (); Virtual ~ Factoryb (); Virtual interfacea * createinstance () ;}; factoryb: factoryb (): ifactory () {} factoryb ::~ Factoryb () {If (null! = M_ptria) delete m_ptria; m_ptria = NULL;} interfacea * factoryb: createinstance () {return (m_ptria = new componetb ();} class factoryc: Public ifacloud {public: factoryc (); Virtual ~ Factoryc (); Virtual interfacea * createinstance () ;}; factoryc: factoryc (): ifactory () {} factoryc ::~ Factoryc () {If (null! = M_ptria) delete m_ptria; m_ptria = NULL;} interfacea * factoryc: createinstance () {return (m_ptria = new componetc ();} int _ tmain (INT argc, _ tchar * argv []) {ifactory * pfactory = new factoryb (); interfacea * pinterface = pfactory-> createinstance (); pinterface-> func1 (); cout <Endl; pinterface-> func2 (); cout <Endl; Delete pfactory; pfactory = new factoryc (); pinterface = pfactory-> createinstance (); pinterface-> func1 (); cout <Endl; pinterface-> func2 (); Delete pfactory; return 0 ;}