Liar design Mode-Adapter mode

Source: Internet
Author: User
Tags inheritance

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. This pattern involves a single class that is responsible for adding standalone or incompatible interface functions. As a real example, the card reader is the adapter between the memory card and the notebook. You insert the memory card into the card reader and then insert the card reader into the notebook so that the memory card can be read from the notebook. The main solution in the software system, often to put some "existing objects" into the new environment, and the new environment requires the interface is not satisfied with the present object.

In the big talk design mode, the geoscience teacher gives the definition of adapter mode: The interface of a class is transformed 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 .

System data and behavior are correct, but the interface does not meet, we should consider the adapter, the purpose is to make an existing object outside the control range to match an interface.

In Gof design mode, two types of adapters are spoken, class Adapter mode and Object Adapter mode , because the class adapter pattern matches one interface to another through multiple inheritance, while languages such as C #, vb.net, and Java do not support multiple inheritance (C + + support). That is, a class has only one parent class, so here we only deal with object adapters.

Adapter Mode structure diagram:

Instance parsing:

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. Adapterpatterndemo, our Demo class uses the Audioplayer class to play various formats.

Package com.exercise.adapter;
/**
 * Create interface for Media Player
 * @author LMB
 * *
 /Public
interface MediaPlayer {public

    void play (String Audiotype,string fileName);
}
Package com.exercise.adapter;
/**
 * Create interface for Advanced Media Player
 * @author LMB * */public
interface Advancedmediaplayer {

    public void PlayMp4 (String fileName);
    public void PLAYVLC (String fileName);
Package com.exercise.adapter;
/**
 * Advanced Media Player Interface implementation Class Mp4player
 * @author LMB * * */Public
class Mp4player implements Advancedmediaplayer {

    @Override public
    void PlayMp4 (String fileName) {
        //plays media
        in MP4 format System.out.println ("playing mp4 filename:" + filename);
    }

    @Override public
    void PLAYVLC (String fileName) {
        //Does nothing
    }

}
Package com.exercise.adapter;
/**
 * Advanced Media Player Interface implementation Class Vlcplayer
 * @author LMB * * */Public
class Vlcplayer implements Advancedmediaplayer {

    @Override public
    void PlayMp4 (String fileName) {
        //do nothing
    }

    @ Override public
    void PLAYVLC (String fileName) {
        //play the VLC format for media
        System.out.println ("Playing VLC fileName : "+ fileName);
    }

}
package com.exercise.adapter;/** * Create adapter class to implement MediaPlayer interface Mediaadapter * @author LMB * */Pub

    Lic class Mediaadapter implements MediaPlayer {Advancedmediaplayer advancedmediaplayer; Constructor public Mediaadapter (String audiotype) {if (Audiotype.equalsignorecase ("VLC")) {Adva
        Ncedmediaplayer = new Vlcplayer ();
        }else if (Audiotype.equalsignorecase ("mp4")) {Advancedmediaplayer = new Mp4player (); }} @Override public void Play (String audiotype, String fileName) {if (Audiotype.equalsignorecase ("V
        LC ")) {ADVANCEDMEDIAPLAYER.PLAYVLC (fileName);
        }else if (Audiotype.equalsignorecase ("mp4")) {Advancedmediaplayer.playmp4 (fileName); }
    }

}
package com.exercise.adapter;/** * Entity class for creating MediaPlayer interfaces * @author LMB * */public class Audio

    Player implements MediaPlayer {Mediaadapter mediaadapter;
            @Override public void Play (String audiotype, String fileName) {if (Audiotype.equalsignorecase ("MP3")) {
        Built-in support for MP3 format media System.out.println ("Play MP3 filename:" + filename); }else if (Audiotype.equalsignorecase ("mp4") | | audiotype.equalsignorecase ("VLC")) {mediaadapter = new Mediaadap
            ter (Audiotype);
        Mediaadapter.play (Audiotype, fileName); }else{System.out.println ("Invalid media."
        + audiotype + "format not supported"); }
    }

}
Package com.exercise.adapter;

public class Adaptertestdemo {public

    static void Main (string[] args) {

        Audioplayer audioplayer = new Audioplayer ( );

        Audioplayer.play ("MP3", "Love.mp3");
        Audioplayer.play ("mp4", "Love.mp4");
        Audioplayer.play ("VLC", "LOVE.VLC");
        Audioplayer.play ("Bhd", "Love.bhd");

    }

}

Operation Result:

Usage Scenarios:

1, when you want to use an existing class, but if its interface is also its method and your requirements are not the same, you should consider the adapter mode. That is, when you are motivated to modify the interface of a normal operating system.

2, at the beginning of the design to consider the adapter mode: For example, the company designed a system to consider the use of third-party development components, and the interface of this component is not the same as our own system interface, and we do not have to adapt to it to adapt its interface, at this time, despite the development of the design phase, It is also possible to consider using adapter mode to solve different interface problems.

the adapter mode is in. NET in the application

There is a very important adapter in. NET that has been implemented by a class library, and that is DataAdapter. DataAdapter is used as an adapter between a dataset and a data source to retrieve and save data. DataAdapter provides this adapter by mapping fill, which changes the data in the dataset to make it easier for data items in the data source to match, and update, which changes the data in the data source to match the data items in the dataset. Because the data source may be from SQL Server or Oracle or access, DB2, the data may be different in the organization, but we want to get a unified dataset (essentially XML data), at which time using DataAdapter is a very good way to We can use data flexibly without having to focus on the data details of different databases.

Advantages:

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.

Disadvantages:

1, too much to use 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.

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

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.