Adapter (C ++ implementation)

Source: Internet
Author: User

// Adapter. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include <iostream>

Using namespace STD;

 

Class target
{
Public:
Target (){}
Virtual ~ Target (){}
 Virtual void request () = 0;
};

Class adaptee
{
Public:
Adaptee (){}
Virtual ~ Adaptee (){}
Void specialrequset ()
{
Cout <"specialrequset of adaptee" <Endl;
}
};

 

//////////////////////////////////////// //////////////////////

 

Class adapter_class: Private adaptee, public target
{
Public:
Adapter_class ()
{
Cout <"Construction of adapter_class" <Endl;
}
Virtual ~ Adapter_class ()
{
Cout <"destruction of adapter_class" <Endl;
}
Void request ()
{
Specialrequset ();
}
};

 

 

//////////////////////////////////////// //////////////////////

 

Class adpater_object: public target
{
Public:
Adpater_object (adaptee * m_p): m_padaptee (m_p)
{
Cout <"Construction of adapter_objetc" <Endl;
}
Virtual ~ Adpater_object ()
{
Delete m_padaptee;
M_padaptee = NULL;
Cout <"destruction of adapter_class" <Endl;
}

 

 Void request ()
{
M_padaptee-> specialrequset ();
}

PRIVATE:
Adaptee * m_padaptee;


};

 

Int _ tmain (INT argc, _ tchar * argv [])
{// Client1
Target * t = new adapter_class ();
T-> request ();
Delete T;
 
// Client2
Adaptee * P = new adaptee ();
Target * T1 = new adpater_object (P );
T1-> request ();

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.