Adapter mode


The Adapter Pattern is a bridge between two incompatible interfaces. This type of design pattern is a structured model that combines the functionality 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 acts as an adapter between the memory card and the notebook. You insert the memory card into the card reader and insert the card reader into the notebook so that the memory card can be read from the notebook.

We use the following example to demonstrate the use of adapter mode. Among them, the audio player device can only play mp3 files and play vlc and mp4 files by using a more advanced audio player.

Introduction

Intent: Converts the interface of one class to another interface that the client wants. The adapter mode allows those classes that would otherwise not work together due to incompatible interfaces to work together.

Main solution: Mainly solved in the software system, it is often necessary to put some "existing objects" into the new environment, and the interface required by the new environment is not satisfied by the current object.

When to use: 1. The system needs to use existing classes, and such interfaces do not meet the needs of the system. 2. You want to create a reusable class for working with classes that don't have much to do with each other, including some that might be introduced in the future. These source classes don't necessarily have a consistent interface. 3. Insert a class into another class through interface conversion. (For example, tigers and birds, there is now a flying tiger. Adding an adapter without increasing the physical requirements, and having a tiger object in it, realizes the flying interface.)

How to fix: Inherit or rely on (recommended).

Key Code: The adapter inherits or relies on existing objects to implement the desired target interface.

Application example: 1. American Electric 110V, China 220V, there must be an adapter to convert 110V to 220V. 2, JAVA JDK 1.1 provides the Enumeration interface, and the Iterator interface is provided in 1.2. If you want to use the 1.2 JDK, you need to convert the previous system's Enumeration interface to the Iterator interface. In this case, the adapter mode is required. 3. Run the WINDOWS program on LINUX. 4. jdbc in JAVA.

Advantages: 1. You can run any two unrelated classes together. 2. Improve the reuse of classes. 3. Increase the transparency of the class. 4. Good flexibility.

Disadvantages: 1. Excessive use of the adapter will make the system very messy and difficult to grasp overall. For example, it is obvious that the call to the A interface is actually adapted to the implementation of the B interface. If a system has too many such situations, it is tantamount to a disaster. So if it's not necessary, you can refactor the system without using an adapter. 2. Since JAVA inherits at most one class, at most one adaptor class can be adapted, and the target class must be an abstract class.

Usage scenarios: motivated to modify the interface of a functioning system, then you should consider using the adapter mode.

Note: Adapters are not added at the time of detailed design, but rather to solve problems in the project being served.

Implementation

We have an 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 AudioPlayer to play audio files in other formats. To do 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 knowing the actual class that can play the audio of the desired format. AdapterPatternDemo, our demo class uses the AudioPlayer class to play various formats.






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition