design mode (vi) adapter mode (Adapter)

Source: Internet
Author: User

We often encounter this situation in development: there are some features that cost a great deal of their own development, but the existing third-party libraries are less mature and we can use them temporarily. However, once the need to modify the dependent library, the source code also needs to be modified on a large scale, there is no way to minimize the extent of the changes? At this point we can consider using adapter mode.


First, the definition

The adapter mode is a structured pattern. Its purpose is to convert a class's interface into another interface that the customer wants, and the adapter pattern makes it possible for classes that would otherwise not work together because of incompatible interfaces.


Second, the structure

There are two types of adapter modes


1. Object Adapter Mode

In this mode, the adapter class contains an instance of the adaptation object so that the adapter class can invoke the method of the adaptation object.



2. Class Adaptation mode

This type of adapter is implemented through multiple inheritance or implementations of multiple existing and expected implementations (compatible) classes/interfaces. For Java, a language that does not support multiple inheritance, the object to be adapted is generally declared as interface.


Parameter description:

Existing classes/interfaces used by target:client

Adaptee: Class/interface to be adapted

Adapter: A class that implements the target and adaptee adaptation


Third, applicability

    • Want to use a class that already exists, but its interface doesn't fit your needs


Iv. Examples of

Finally let's take a look at an example, assuming we have a MediaPlayer interface that contains the play () abstract method, Audioplayer implements the MediaPlayer method, and the default play () is the MP3 format. There is also a Advancedmediaplayer interface that contains the PlayAVI () and PLAYMP4 () two abstract methods, Aviplayer and Mp4player implement Advancedmediaplayer. Now if want to let Audioplayer also can support MP4 and AVI Two format of play, what should do? Here we can use the object adaptation model to create a Mediaadapter class that contains a Advancedmediaplayer class instance. This makes it possible to invoke its subclass method. Then add a MediaPlayer instance to the Audioplayer, judge the input music format, make different operation on the line.


Paste the following code:

MediaPlayer:

Package Designpattern.adapter;public interface MediaPlayer {public void Play (String audiotype, String fileName);}

Advancedmediaplayer:

Package Designpattern.adapter;public interface Advancedmediaplayer {public void PlayAVI (String fileName);p ublic void PLAYMP4 (String fileName);}

Aviplayer:

Package Designpattern.adapter;public class Aviplayer implements Advancedmediaplayer {@Overridepublic void PlayAVI ( String fileName) {//TODO auto-generated method StubSystem.out.println ("Playing avi, file name is" +filename);} @Overridepublic void PlayMp4 (String fileName) {//TODO auto-generated method stub}}

Mp4player:

Package Designpattern.adapter;public class Mp4player implements Advancedmediaplayer {@Overridepublic void PlayAVI (  string filename) {//Todo auto-generated method stub} @Overridepublic void PlayMp4 (string filename) {//Todo auto-generated Method StubSystem.out.println ("Playing mp4, file name is" +filename);}}

Mediaadapter:

Package Designpattern.adapter;public class Mediaadapter implements MediaPlayer {private Advancedmediaplayer Advancedmediaplayer;public Mediaadapter (String audiotype) {if (Audiotype.equalsignorecase ("mp4")) { Advancedmediaplayer = new Mp4player ();} else if (audiotype.equalsignorecase ("avi")) {Advancedmediaplayer = new Aviplayer ();} Else{throw New IllegalArgumentException ("Unrecognized advancedmediaplayer type");}} @Overridepublic void Play (String audiotype, String fileName) {//TODO auto-generated method Stubif ( Audiotype.equalsignorecase ("avi")) {Advancedmediaplayer.playavi (fileName);} else if (Audiotype.equalsignorecase ("mp4")) {Advancedmediaplayer.playmp4 (fileName);} Else{throw New IllegalArgumentException ("Unrecognized audio type for Advancedmediaplayer");}}}

Audioadapter:

Package Designpattern.adapter;public class Audioplayer implements MediaPlayer {private Mediaadapter mediaadapter;@ Overridepublic void Play (String audiotype, String fileName) {if (Audiotype.equalsignorecase ("MP3")) { System.out.println ("Playing mp3, file name is" + FileName);} else if (Audiotype.equalsignorecase ("mp4") | | | audiotype.equalsignorecase ("avi")) {mediaadapter = new Mediaadapter ( Audiotype); Mediaadapter.play (Audiotype, fileName);} Else{system.out.println ("Unrecognized audio format");}}}

Test class, Mediademo:

Package Designpattern.adapter;public class Mediademo {public static void main (string[] args) {MediaPlayer player = new Aud Ioplayer ();p layer.play ("MP3", "1.mp3");p Layer.play ("mp4", "2.mp4");p layer.play ("avi", "3.avi");p layer.play ("mpg", "4.mpg");}}

Console output:

Playing mp3, file name is 1.mp3playing mp4, file name is2.mp4playing avi, file name is 3.aviunrecognized audio format


Reference documents:

1. Wikipedia: Adapter Pattern

2.design_pattern Adapter_pattern

3.java Best Adapter mode, adapter mode



design mode (vi) 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.