Adapter mode for C + + design mode

Source: Internet
Author: User

The object adapter has the following characteristics:

    1. Sometimes, you will find that it is not easy to construct an object of the adaptee type;
    2. When adding a new abstract method to the Adaptee, the adapter class does not need to make any adjustments and can perform the action correctly;
    3. You can invoke methods of the Adaptee class subclass in the adapter class using a polypeptide.

Code implementation:

1 //Adapter.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <string>7 using namespacestd;8 9 Ten classTarget One { A  Public: - Target () {} -     Virtual~Target () {} the     Virtual voidRequest () -     { -cout <<"target::request"<<Endl; -     } + }; - classAdaptee + { A  Public: at     voidspecificrequest () -     { -cout <<"adaptee::specificrequest"<<Endl; -     } - }; -  in classAdapter: PublicTarget - { to  Public: +Adapter (): M_adaptee (Newadaptee) {} -~Adapter () the     { *         if(M_adaptee! =NULL) $         {Panax Notoginseng             Deletem_adaptee; -M_adaptee =NULL; the         } +     } A     voidRequest () the     { +M_adaptee->specificrequest (); -     } $  $ Private: -Adaptee *m_adaptee; - }; the int_tmain (intARGC, _tchar*argv[]) - {Wuyicout<<"Adapter Mode:"<<Endl; theTarget *targetobj =NewAdapter (); -Targetobj->Request (); Wu     Deletetargetobj; -Targetobj =NULL; AboutSystem"Pause"); $     return 0; -}

Adapter mode for C + + design mode

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.