(V) Adapter Mode-c++ Implementation

Source: Internet
Author: User

Transforms the interface of one class into another interface that the customer Wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work Together.

There are three types of roles in this mode:

1, Target: is an abstract class, it is the interface that the customer wants to use

2, the Adapter: the adapter is an existing abstract class or interface, need to be adapted

3. Adapter: the adapter is a class that inherits the target and contains a reference to the Adapter. Its role is to adapt the adaptation to the Target.

In this example, the Three-phase current is the target, now has a washing machine, can directly use Three-phase current, tv, can use two-phase current, now to adapt to the TV can directly use Three-phase Current. The implementation is as Follows:

(1) Target.h

1 #ifndef _target_h_2 #define_target_h_3#include <iostream>4#include <string>5 using namespacestd;6 //the goal (which is to fit into this, all use This)7 classthreeelectricoutlet{8  public:9     Virtual voidConnectelectriccurrent () =0;Ten  one }; a  - //the Adapter (cannot Use it now, adapt it to be used) - classtwoelectricoutlet{ the  public: -     Virtual voidConnectelectriccurrent () =0; - }; -  + //Adapter - classThreeelectricadapter: publicthreeelectricoutlet{ + Private: aTwoelectricoutlet *myoutlet; at  -  public: -Threeelectricadapter (twoelectricoutlet *outlet); -     voidConnectelectriccurrent ()Override; - }; -  in //Specific Objectives - classWash: publicthreeelectricoutlet{ to  public: + Wash (); -Wash (strings); the     voidConnectelectriccurrent ()Override; *  $ Private:Panax Notoginseng     stringname; -  the  + }; a  the //specifically adapted by + classTv: publictwoelectricoutlet{ -  public: $ Tv (); $Tv (strings); -     voidConnectelectriccurrent ()Override; - Private: the     stringname; - Wuyi }; the  -  wu  - #endif
View Code

(2) Target.cpp

1#include"Target.h"2 3Threeelectricadapter::threeelectricadapter (twoelectricoutlet *Outlet)4 {5Myoutlet =outlet;6 }7 8 voidthreeelectricadapter::connectelectriccurrent ()9 {TenMyoutlet->connectelectriccurrent (); one } a  - Wash::wash () - { theName ="Washing Machine"; - } -Wash::wash (stringS) - { +Name =s; - } +  a voidwash::connectelectriccurrent () at { -cout << name <<"Begin Wash"<<endl; - } -  - TV::TV () - { inName ="TV"; - } to  +TV::TV (stringS) - { theName =s; * } $ Panax Notoginseng voidtv::connectelectriccurrent () - { thecout << name <<"begin play Video"<<endl; +}
View Code

(3) AdapterTest.cpp

1#include"Target.h"2 3 intMain ()4 {5Threeelectricoutlet *outlet;//now the only targets that can be used are the others that will fit into this goal .6Wash *wash =NewWash ();//the washing machine is originally used three-phase electricity (that is, inherited Threeelectricoutlet)7Outlet =wash;8cout <<"use three electric:"<<endl;9Outlet->connectelectriccurrent ();Ten  oneTv *TV =NewTv ();//the TV can only use two-phase electricity and now it fits into a three-phase aThreeelectricadapter *adapter =NewThreeelectricadapter (tv); -Outlet =adapter; -cout <<"use three electric:"<<endl; theOutlet->connectelectriccurrent (); -     return 0; -}
View Code

The key to the adapter pattern is to establish an adapter that inherits the target interface and contains a reference to the appropriate Person. It is also possible to adapt two-way, that is, using multiple inheritance, inheriting both the target and the ligand, and containing references to the target and the Ligand.

(V) Adapter Mode-c++ Implementation

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.