Several lines of code are handwritten

Source: Internet
Author: User

 

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.