Home > Java > javaTutorial > The wonderful use of the adapter pattern in Java design patterns

The wonderful use of the adapter pattern in Java design patterns

王林
Release: 2024-05-09 12:54:01
Original
906 people have browsed it

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, a client (such as MediaPlayer) can play media in advanced formats (such as VLC), although it itself only supports common media formats (such as MP3).

The wonderful use of the adapter pattern in Java design patterns

The wonderful use of the adapter pattern in Java design patterns

Introduction

Adapter pattern Is a structural design pattern that allows one object to work with another object that has an incompatible interface. In other words, it converts one interface into another, allowing two otherwise incompatible objects to interact smoothly.

Structure

The structure of the adapter pattern is as shown below:

                        +--------------------+
                        |       Target       |
                        +--------------------+
                                 ^
                                 |
                        +--------------------+
                        |    Adaptee(被适配) |
                        +--------------------+
                                 ^
                                 |
                  +----------------------------+
                  |  Object Adapter (对象适配器)  |
                  +----------------------------+
                             |                 |
                              \_______________/
Copy after login
  • Target (target): Definition The interface required by the client.
  • Adaptee (adapted): Define the interface to be adapted.
  • Adapter: Converts the Adaptee interface to the Target interface so that the client can use Adaptee.

Object Adapter

Object adapter is a simple way to implement the adapter pattern. It creates a new adapter object that contains the Adaptee object and implements the Target interface.

// Target 接口
interface Target {
    int operation();
}

// Adaptee 接口
interface Adaptee {
    int specificOperation();
}

// Adapter (对象适配器)
class Adapter implements Target {
    private final Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public int operation() {
        return adaptee.specificOperation();
    }
}
Copy after login

Practical case

Suppose we have two classes: MediaPlayer and AdvancedMediaPlayer. The MediaPlayer class is responsible for playing media in common formats (such as MP3), while the AdvancedMediaPlayer class is responsible for playing media in advanced formats (such as VLC). We are going to use the adapter pattern to enable the MediaPlayer class to play advanced format media.

class MediaPlayer {
    public void playMP3(String filename) {
        System.out.println("Playing MP3: " + filename);
    }
}

class AdvancedMediaPlayer {
    public void playVLC(String filename) {
        System.out.println("Playing VLC: " + filename);
    }
}

class MediaAdapter implements MediaPlayer {
    private final AdvancedMediaPlayer advancedMediaPlayer;

    public MediaAdapter(AdvancedMediaPlayer advancedMediaPlayer) {
        this.advancedMediaPlayer = advancedMediaPlayer;
    }

    @Override
    public void playMP3(String filename) {
        advancedMediaPlayer.playVLC(filename);
    }
}

// 客户调用
MediaPlayer audioPlayer = new MediaAdapter(new AdvancedMediaPlayer());
audioPlayer.playMP3("FileName.vlc");
Copy after login

Output:

Playing VLC: FileName.vlc
Copy after login

By using the Adapter pattern, we extend the functionality of the MediaPlayer class to enable it to play advanced format media without modifying its source code.

The above is the detailed content of The wonderful use of the adapter pattern in Java design patterns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template