Adapter Mode Adapter

Source: Internet
Author: User

Adapter mode, adjust to conform to the other interface

Class 1 Adapter pattern, subclass inherits Adaptee class

#include <IOSTREAM>using namespacestd;//Class Adapterclasstarget{ Public:    Virtual voidAction () =0;};classadaptee{ Public:    voidadapteraction () {cout<<"Hello World"<<Endl; }};classAdapter: Publictarget,adaptee{ Public:    voidaction () { This-adapteraction ();    }}; intMainintargcChar*argv[]) {Target* p =NewAdapter; P-action (); Deletep; return 0;}

2 Object Adapter pattern, the Adaptee class is present in the adapter class as an object, and the operation of Adaptee is invoked in an interface that is inherited by Target in the adapter class

//Object Adapterclasstarget{ Public:    Virtual voidAction () =0;};classadaptee{ Public:    voidadapteraction () {cout<<"Hello World"<<Endl; }};classAdapter: Publictarget{ Public:    voidaction () {p-adapteraction (); } Adapter () {p=Newadaptee; }    ~Adapter () {Deletep; }Private: Adaptee*p;}; intMainintargcChar*argv[]) {Target* p =NewAdapter; P-action (); Deletep; return 0;}

3. Default adapter mode,

We refer to this intermediate transition class as the default adaptation class, and the default adaptation mode provides a default implementation for an interface
#include <IOSTREAM>using namespacestd;//Default Adapterclasstarget{ Public:    Virtual voidAction1 () =0; Virtual voidAction2 () =0; Virtual voidAction3 () =0;};classDefaultadapter: Publictarget{ Public:    voidAction1 () {cout<<"Action1"<<Endl; }    voidAction2 () {cout<<"Action2"<<Endl; }    voidAction3 () {cout<<"Action3"<<Endl; }};classAdapter: Publicdefaultadapter{ Public:    voidAction1 () {cout<<"Only want Action1"<<Endl;    }}; intMainintargcChar*argv[]) {Target* p =NewAdapter; P-Action1 (); Deletep; return 0;}

Adapter Mode Adapter

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.