Designpatterns Study Notes: C + + language implementation---2.2 Adapter

Source: Internet
Author: User

Designpatterns Study Notes: C + + language implementation---2.2 Adapter

2016-07-22

(WWW.CNBLOGS.COM/ICMZN)

Pattern understanding

1. Adapter definition
Transforms the interface of one class into another form of interface that the client expects, so that the adaptee of the adapter can be used in the environment of target targets.
This can generally be divided into the following roles:
(1) Target role:
Defines the target interface to which other classes or objects are converted. Typically an abstract class (interface).
(2) adaptee role:
Defines the class that is being converted, which is a class or object that already exists
(3) Adapte role:
Match target target by inheriting adaptee or associative combination adaptee
Usually the adapter is actually in system maintenance later system extension application.

2. Adapter Advantages
(1) Strong two non-related classes fit together to use. Improve the reuse degree, easy to maintain the system, good flexibility.
(2) To increase the transparency, that is, the Taget interface implementation, entrusted to the specific adaptee to complete.

3. Adapter Adaptation conditions
(1) Later in the system expansion, using an existing class or the original class, to adapt to the new interface or the original interface.

4. Adapter discussion and attention issues
(1) Two ways to implement the adapter mode difference: Class inherits Adapter mode and Object Adapter mode
This is an important concept in the field of object-oriented.
Class-inheriting adapters are interfaces and implementations that obtain adaptee by inheriting Adaptee mode (private),
That is, if you inherit from private, you only get the implementation, and if you inherit from public, you also get the interface, and the parent class interface can provide the service in the subclass.
You can implement interface inheritance through pure virtual class inheritance.
The object adapter method is to obtain the corresponding interface content through the Adaptee member through the adapter combination, and then the interface of the target object.
(2) class inheritance adapters are inheritance relationships between classes, whereas object adapters are association relationships that are formed between classes by composition.

Program Implementation (c + +)

Adapte.h

1 #pragmaOnce2 3 //Adapter Definition4 /*5 Two types of adapter definitions6 (1) class Adapter mode: Adapter private inherits Adaptee (or abstract class) and implements Target's interface (inheriting abstract class)7 (2) object Adapter mode: Datapter combine Adaptee object and implement target interface (inherit abstract class)8 */9#include"TargetAdptee.h"Ten  One classCclassadepter: PublicCadbtarger,PrivateCadeptee A { -  Public: -Cclassadepter (intAge ): Cadeptee (age) {}; the  Public: -     voidRequest ()Override -     { -         //the adapter that invokes the class adaptor + Domyway (); -     } +  A }; at  -  - classCobjectadatper: PublicCadbtarger - { -  Public: - Cobjectadatper () in     { -M_padaptee =nullptr; to     }; +  -Cobjectadatper (cadeptee*padaptee) the     { *M_padaptee =padaptee; $     }Panax Notoginseng~Cobjectadatper () -     { the Delete m_padaptee; +     } A  Public: the     voidRequest ()Override +     { - _assert (m_padaptee); $         //the adapter that invokes the class adaptor $M_padaptee->Domyway (); -     } -  the  - Private:Wuyicadeptee*m_padaptee; the  -};

Targetadptee

1 #pragmaOnce2 3#include <iostream>4 using namespacestd;5 //Target class, adapted class6 classCadbtarger7 {8  Public:9     Virtual voidRequest () =0;Ten }; One 
A classCadeptee - { - Public: theCadeptee (intAge ): M_iage (age) {}; - Public: - //methods to be adapted - voidDomyway () + { -cout <<"my excuses are realized in a adeptee way ...."<< M_iage <<Endl; + } A Private: at intM_iage; -};

(1) Template application

Main.cpp

1 //Adapter.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"AdepterDef.h"6#include <iostream>7 using namespacestd;8 9 int_tmain (intARGC, _tchar*argv[])Ten { Onecout <<"Class Adapter Mode"<<Endl; Acclassadepter* Padapter =NewCclassadepter ( -); -Padapter->request (); - Delete padapter; thecout <<"Object Adapter Mode"<<Endl; -Cobjectadatper *pobj =NewCobjectadatper (NewCadeptee ( -)); -Pobj->request (); - Delete pObj; +  -System"Pause"); +     return 0; A}

(2) Output display

Designpatterns Study Notes: C + + language implementation---2.2 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.