Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of adapter patterns in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about the usefulness of adapter patterns in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Introduction

Intention: convert the interface of one class to another interface that the customer wants. The adapter pattern allows classes that cannot work together because the interfaces are not compatible.

The main solution: the main solution is in the software system, it is often necessary to put some "existing objects" into the new environment, but the interface required by the new environment can not be satisfied by the current object.

When to use: 1, the system needs to use the existing class, and this kind of interface does not meet the needs of the system. 2. Want to create a reusable class to work with some classes that are not much related to each other, including some classes that may be introduced in the future, these source classes do not necessarily have a consistent interface. 3. Insert one class into another through interface transformation. (for example, tigers and birds, now there is a flying tiger, without the need to increase the entity, add an adapter, in which a tiger object is included to implement the flying interface. )

How to solve: inherit or depend on (recommended).

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

Application examples: 1, American electrical 110V, China 220V, there is an adapter to convert 110V to 220V. 2. JAVA JDK 1.1 provides Enumeration interface, while Iterator interface is provided in 1.2. if you want to use JDK of 1.2, you need to convert the Enumeration interface of the previous system into Iterator interface, which requires the adapter pattern. 3. Run WINDOWS program on LINUX. 4. Jdbc in JAVA.

Advantages: 1, you can let any two unrelated classes run together. 2. The reuse of classes is improved. 3. Increase the transparency of the class. 4. Good flexibility.

Disadvantages: 1. Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that interface An is called, but in fact, it is internally adapted to the implementation of interface B. if this happens in too many systems, it is tantamount to a disaster. So if it is not necessary, the system can be refactored directly without using an adapter. two。 Because JAVA inherits at most one class, at most one adaptor class can be adapted, and the target class must be an abstract class.

Usage scenario: when you are motivated to modify the interface of a functioning system, you should consider using the adapter pattern.

Note: adapters are not added at detailed design time, but rather resolve problems with projects in service.

Realize

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 have another interface, AdvancedMediaPlayer, and an entity class that implements the AdvancedMediaPlayer interface. This class can play files in vlc and mp4 formats.

We want AudioPlayer to 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 required audio type without knowing the actual class that can play the desired format audio. AdapterPatternDemo, our demo class uses the AudioPlayer class to play various formats.

Step 1

Create interfaces for media players and more advanced media players.

MediaPlayer.javapublic interface MediaPlayer {public void play (String audioType, String fileName);} AdvancedMediaPlayer.javapublic interface AdvancedMediaPlayer {public void playVlc (String fileName); public void playMp4 (String fileName);}

Step 2

Create an entity class that implements the AdvancedMediaPlayer interface.

VlcPlayer.javapublic class VlcPlayer implements AdvancedMediaPlayer {@ Override public void playVlc (String fileName) {System.out.println ("Playing vlc file. Name: "+ fileName);} @ Override public void playMp4 (String fileName) {/ / do nothing}} Mp4Player.javapublic class Mp4Player implements AdvancedMediaPlayer {@ Override public void playVlc (String fileName) {/ / do nothing} @ Override public void playMp4 (String fileName) {System.out.println (" Playing mp4 file. Name: "+ fileName);}}

Step 3

Create an adapter class that implements the MediaPlayer interface.

MediaAdapter.javapublic class MediaAdapter implements MediaPlayer {AdvancedMediaPlayer advancedMusicPlayer; public MediaAdapter (String audioType) {if (audioType.equalsIgnoreCase ("vlc")) {advancedMusicPlayer = new VlcPlayer ();} else if (audioType.equalsIgnoreCase ("mp4")) {advancedMusicPlayer = new Mp4Player ();}} @ Override public void play (String audioType, String fileName) {if (audioType.equalsIgnoreCase ("vlc")) {advancedMusicPlayer.playVlc (fileName) } else if (audioType.equalsIgnoreCase ("mp4")) {advancedMusicPlayer.playMp4 (fileName);}

Step 4

Create an entity class that implements the MediaPlayer interface.

AudioPlayer.javapublic class AudioPlayer implements MediaPlayer {MediaAdapter mediaAdapter; @ Override public void play (String audioType, String fileName) {/ / built-in support for playing mp3 music files if (audioType.equalsIgnoreCase ("mp3")) {System.out.println ("Playing mp3 file. Name: "+ fileName);} / / mediaAdapter provides support for else if (audioType.equalsIgnoreCase (" vlc ") | | audioType.equalsIgnoreCase (" mp4 ")) {mediaAdapter = new MediaAdapter (audioType); mediaAdapter.play (audioType, fileName);} else {System.out.println (" Invalid media. "+ audioType +" format not supported ");}

Step 5

Use AudioPlayer to play different types of audio formats.

AdapterPatternDemo.javapublic class AdapterPatternDemo {public static void main (String [] args) {AudioPlayer audioPlayer = new AudioPlayer (); audioPlayer.play ("mp3", "beyond the horizon.mp3"); audioPlayer.play ("mp4", "alone.mp4"); audioPlayer.play ("vlc", "far far away.vlc"); audioPlayer.play ("avi", "mind me.avi");}}

Step 6

Execute the program and output the result:

Playing mp3 file. Name: beyond the horizon.mp3Playing mp4 file. Name: alone.mp4Playing vlc file. Name: far far away.vlcInvalid media. Avi format not supported, thank you for your reading! This is the end of this article on "what is the use of adapter patterns in Java?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 226

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report