Design mode adapter mode (Adapter pattern) C + + implementation

Source: Internet
Author: User

The adapter mode (Adapter pattern) is a bridge between two incompatible interfaces. This type of design pattern belongs to the structural pattern, which combines the functions of two separate interfaces.

Intent: To convert 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.

Main solution: The main solution in the software system, often to put some "existing objects" into the new environment, and the new environment requirements of the interface is not satisfied with the present object.

When to use: 1, the system needs to use the existing classes, and this class of interfaces do not meet the needs of the system. 2. You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future, that do not necessarily have a consistent interface. 3, through the interface transformation, one class is inserted into another class system. (such as tigers and birds, now more than a flying tiger, without increasing the demand for the entity, add an adapter, in the inside to contain a Tiger object, to achieve the flight interface. )

How to resolve: inheritance or dependency (recommended).

Pros: 1. You can let any two classes that are not associated run together. 2, improve the reuse of the class. 3, increase the transparency of the class. 4, good flexibility.

Disadvantage: 1, excessive use of the adapter, will make the system very messy, not easy to grasp the whole. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. So if it's not necessary, you can refactor the system without using the adapter. 2. Since JAVA inherits at most one class, only one adapter class can be fitted at most, and the target class must be an abstract class.

Usage Scenario: You should consider using adapter mode when you are motivated to modify the interface of a functioning system.

Note: The adapter is not added at the detailed design time, but instead solves the problem of the project in service.

implementation:

We have a MediaPlayer interface and an entity class Audioplayer that implements the MediaPlayer interface. By default, Audioplayer can play audio files in mp3 format.

We also have another interface Advancedmediaplayer and an entity class that implements the Advancedmediaplayer interface. This class can play files in VLC and MP4 format.

We want to have Audioplayer play audio files in other formats. To achieve this, we need to create an adapter class Mediaadapter that implements the MediaPlayer interface and use the Advancedmediaplayer object to play the desired format.

Audioplayer uses the adapter class Mediaadapter to pass the desired audio type without needing to know the actual class that can play the desired format audio.

Code:

Step 1 Create an interface for media players and more advanced media players.

MediaPlayer.h

#pragma once/*mediaplayer.h*/#include "stdafx.h" class mediaplayer{public:virtual void Play (string& audiotype, string& filename) = 0;virtual ~mediaplayer () {cout<< "This is MediaPlayer desconstructor." << Endl;};
AdvanceMediaPlayer.h
#pragma once/*advancemediaplayer.h*/#include "stdafx.h" class advancemediaplayer{public://here or do not write, It is either written as inline empty implementation form Advancemediaplayer () {}//advancemediaplayer (); virtual ~advancemediaplayer () {}virtual void playvcl ( string& filename) = 0;virtual void PlayMp4 (string& filename) = 0;};

Step 2

Create an entity class that implements the Advancedmediaplayer interface.

Mp4Player.h and its realization

#pragma once#include "Advancemediaplayer.h" class Mp4player:p ublic advancemediaplayer{public:mp4player (void); ~ Mp4player (void), virtual void playvcl (string& filename), virtual void playMp4 (string& filename); void Mp4player::p layvcl (string& filename) {//null}void mp4player::p layMp4 (string& filename) {cout << Playing MP4 media file.name "<< filename << Endl;}
VclPlayer.h and realization
#pragma once#include "Advancemediaplayer.h" class Vclplayer:p ublic advancemediaplayer{public:vclplayer (void); ~ Vclplayer (void), virtual void playvcl (string& filename), virtual void playMp4 (string& filename); void Vclplayer::p layvcl (string& filename) {cout << "playing VCL media file.name" << filename << Endl ;} void Vclplayer::p layMp4 (string& filename) {//null}

Step 3

Create an adapter class that implements the MediaPlayer interface.

AdapterMediaPlayer.h and AdapterMediaPlayer.cpp

#pragma once#include "mediaplayer.h" #include "AdvanceMediaPlayer.h" class Adaptermediaplayer:p ublic mediaplayer{ Public:adaptermediaplayer (string& audiotype); ~adaptermediaplayer (void); virtual void play (string& audioType , string& filename);p rivate:advancemediaplayer* advancemusicplayer;}; #include "StdAfx.h" #include "AdapterMediaPlayer.h" #include "Mp4Player.h" #include "VclPlayer.h" Adaptermediaplayer:: Adaptermediaplayer (string& audiotype) {if (Audiotype.compare ("vcl") = = 0) {This->advancemusicplayer = new Vclplayer ();} else if (Audiotype.compare ("mp4") = = 0) {This->advancemusicplayer = new Mp4player ();}} Adaptermediaplayer::~adaptermediaplayer (void) {if (Advancemusicplayer! = NULL) {delete advancemusicplayer; Advancemusicplayer = NULL;}} void Adaptermediaplayer::p Lay (string& audiotype, string& filename) {if (Audiotype.compare ("vcl") = = 0) {this- &GT;ADVANCEMUSICPLAYER-&GT;PLAYVCL (filename);} else if (Audiotype.compare ("mp4") = = 0) {This->advancemusicplayer->playmp4 (filename);}} 

Step 4

Create an entity class that implements the MediaPlayer interface.

AudioPlayer.h and AudioPlayer.cpp

#pragma once#include "mediaplayer.h" #include "AdapterMediaPlayer.h" class Audioplayer:p ublic mediaplayer{public: Audioplayer (void); ~audioplayer (void), void play (string& audiotype, string& filename);p rivate: Adaptermediaplayer* Mediaadapter;}; #include "StdAfx.h" #include "AudioPlayer.h" audioplayer::audioplayer (void) {mediaadapter = NULL;} Audioplayer::~audioplayer (void) {if (Mediaadapter! = NULL) {delete mediaadapter; mediaadapter =null;}} void Audioplayer::p Lay (string& audiotype, string& filename) {if (Audiotype.compare ("mp3") = = 0) {cout << " Playing mp3 file.name: "<< filename << Endl;} else if (!audiotype.compare ("VCL") | | |!audiotype.compare ("mp4")) {//There is an easy memory leak here. Mediaadapter = new Adaptermediaplayer (audiotype); Mediaadapter->play (audiotype,filename);} Else{cout << "Invalid media." << audiotype << "and this format isn't support." << Endl; if (mediaadapter! = NULL) {delete mediaadapter; mediaadapter =null;} Pointer release principle, construct new destructor Delete, function in the new function deLete, the final destruction and insurance delete. }

Step 5

Use Audioplayer to play different types of audio formats.

#include "stdafx.h" #include "AudioPlayer.h" int _tmain (int argc, _tchar* argv[]) {audioplayer* Audioplayer = new Audioplayer (); Audioplayer->play (String ("MP3"), String ("Beyond the Horizon.mp3"); Audioplayer->play (String (" MP4 "), String (" Alone.mp4 ")), Audioplayer->play (String (" VCL "), String (" Far Far Away.vcl ")); Audioplayer->play ( String ("Avi"), String ("Mind Me.avi"));d elete Audioplayer;audioplayer = Null;system ("pause"); return 0;}

Finally, the code is only a reference, the most important is the idea.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design mode adapter mode (Adapter pattern) C + + implementation

Related Article

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.