MK
摩柯社区 - 一个极简的技术知识社区
AI 面试

Java适配器模式的应用示例

2022-04-243.2k 阅读

适配器模式的概念

在软件开发中,我们常常会遇到这样的情况:现有的类提供了一些功能,但接口与我们所需的接口不兼容。这时候,适配器模式就派上用场了。适配器模式(Adapter Pattern)是一种结构型设计模式,它允许将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

适配器模式包含三个主要角色:

  1. 目标(Target)接口:客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。
  2. 适配者(Adaptee)类:需要适配的类,它有一个已经存在的接口,但这个接口与目标接口不兼容。
  3. 适配器(Adapter)类:连接目标和适配者的桥梁,它包装了一个适配者对象,实现了目标接口,并将请求转发给适配者对象。

Java 中适配器模式的应用场景

  1. 旧系统与新系统集成:在企业开发中,经常会遇到需要将旧的遗留系统与新开发的系统集成的情况。旧系统可能使用的是过时的接口,而新系统期望使用现代的接口。通过适配器模式,可以将旧系统的接口适配成新系统能够理解的接口,从而实现无缝集成。
  2. 第三方库使用:当引入第三方库时,第三方库提供的接口可能与我们项目中的接口规范不一致。适配器模式可以帮助我们将第三方库的接口适配成符合我们项目需求的接口,使我们能够更方便地使用第三方库的功能。
  3. 复用已有类:如果项目中存在一些功能强大但接口不符合当前需求的类,通过适配器模式可以复用这些类,而不需要对这些类进行大规模的修改,提高了代码的复用性。

类适配器模式示例

在类适配器模式中,适配器类继承自适配者类,同时实现目标接口。下面通过一个具体的示例来演示类适配器模式。

假设我们有一个音频播放器,它只能播放 MP3 格式的音频文件。现在我们希望这个音频播放器也能播放 WAV 格式的音频文件。我们有一个 MP3Player 类作为适配者,一个 WAVPlayer 类作为我们要适配的新功能,以及一个 AudioPlayer 接口作为目标接口。

首先,定义 AudioPlayer 接口:

public interface AudioPlayer {
    void play(String audioType, String fileName);
}

然后,定义 MP3Player 类:

public class MP3Player {
    public void playMP3(String fileName) {
        System.out.println("Playing MP3 file. Name: " + fileName);
    }
}

接着,定义 WAVPlayer 类:

public class WAVPlayer {
    public void playWAV(String fileName) {
        System.out.println("Playing WAV file. Name: " + fileName);
    }
}

现在,创建类适配器 AudioPlayerAdapter

public class AudioPlayerAdapter extends WAVPlayer implements AudioPlayer {
    @Override
    public void play(String audioType, String fileName) {
        if ("wav".equals(audioType)) {
            playWAV(fileName);
        } else if ("mp3".equals(audioType)) {
            MP3Player mp3Player = new MP3Player();
            mp3Player.playMP3(fileName);
        }
    }
}

最后,在客户端使用:

public class Client {
    public static void main(String[] args) {
        AudioPlayer audioPlayer = new AudioPlayerAdapter();
        audioPlayer.play("mp3", "song.mp3");
        audioPlayer.play("wav", "sound.wav");
    }
}

在上述示例中,AudioPlayerAdapter 继承了 WAVPlayer 并实现了 AudioPlayer 接口。它在 play 方法中,根据音频类型调用相应的播放方法。当音频类型为 wav 时,调用 WAVPlayerplayWAV 方法;当音频类型为 mp3 时,创建 MP3Player 对象并调用其 playMP3 方法。

对象适配器模式示例

对象适配器模式与类适配器模式的主要区别在于,对象适配器模式中适配器类不再继承适配者类,而是持有适配者类的实例。

继续以上面音频播放器的例子来说明对象适配器模式。

同样先定义 AudioPlayer 接口和 MP3Player 类:

public interface AudioPlayer {
    void play(String audioType, String fileName);
}

public class MP3Player {
    public void playMP3(String fileName) {
        System.out.println("Playing MP3 file. Name: " + fileName);
    }
}

定义 WAVPlayer 类:

public class WAVPlayer {
    public void playWAV(String fileName) {
        System.out.println("Playing WAV file. Name: " + fileName);
    }
}

创建对象适配器 AudioPlayerObjectAdapter

public class AudioPlayerObjectAdapter implements AudioPlayer {
    private WAVPlayer wavPlayer;

    public AudioPlayerObjectAdapter() {
        wavPlayer = new WAVPlayer();
    }

    @Override
    public void play(String audioType, String fileName) {
        if ("wav".equals(audioType)) {
            wavPlayer.playWAV(fileName);
        } else if ("mp3".equals(audioType)) {
            MP3Player mp3Player = new MP3Player();
            mp3Player.playMP3(fileName);
        }
    }
}

在客户端使用:

public class ObjectAdapterClient {
    public static void main(String[] args) {
        AudioPlayer audioPlayer = new AudioPlayerObjectAdapter();
        audioPlayer.play("mp3", "song.mp3");
        audioPlayer.play("wav", "sound.wav");
    }
}

在对象适配器模式中,AudioPlayerObjectAdapter 持有 WAVPlayer 的实例,并在 play 方法中根据音频类型调用相应的播放方法。这种方式相比类适配器模式更加灵活,因为它不需要继承适配者类,避免了 Java 单继承的限制。

双向适配器模式示例

双向适配器模式允许适配器在两个方向上进行适配,既可以将适配者的接口适配成目标接口,也可以将目标接口适配成适配者接口。

假设我们有一个 Rectangle 类和一个 Square 类,Rectangle 类有 getWidthgetHeight 方法,而 Square 类有 getSide 方法。我们希望创建一个双向适配器,使得 Rectangle 可以当作 Square 使用,Square 也可以当作 Rectangle 使用。

首先,定义 Rectangle 类:

public class Rectangle {
    private int width;
    private int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
}

然后,定义 Square 类:

public class Square {
    private int side;

    public Square(int side) {
        this.side = side;
    }

    public int getSide() {
        return side;
    }
}

接着,创建双向适配器 RectangleSquareAdapter

public class RectangleSquareAdapter implements Rectangle, Square {
    private Rectangle rectangle;
    private Square square;

    public RectangleSquareAdapter(Rectangle rectangle) {
        this.rectangle = rectangle;
    }

    public RectangleSquareAdapter(Square square) {
        this.square = square;
    }

    @Override
    public int getWidth() {
        if (rectangle != null) {
            return rectangle.getWidth();
        } else {
            return square.getSide();
        }
    }

    @Override
    public int getHeight() {
        if (rectangle != null) {
            return rectangle.getHeight();
        } else {
            return square.getSide();
        }
    }

    @Override
    public int getSide() {
        if (square != null) {
            return square.getSide();
        } else {
            return rectangle.getWidth();
        }
    }
}

在客户端使用:

public class BiDirectionalAdapterClient {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(5, 10);
        Square square = new Square(7);

        RectangleSquareAdapter rectangleAdapter = new RectangleSquareAdapter(rectangle);
        System.out.println("Rectangle as Square - Side: " + rectangleAdapter.getSide());

        RectangleSquareAdapter squareAdapter = new RectangleSquareAdapter(square);
        System.out.println("Square as Rectangle - Width: " + squareAdapter.getWidth());
        System.out.println("Square as Rectangle - Height: " + squareAdapter.getHeight());
    }
}

在双向适配器模式中,RectangleSquareAdapter 实现了 RectangleSquare 两个接口。它根据传入的对象类型,在不同的方法中返回相应的值,实现了双向适配。

适配器模式在 Java 标准库中的应用

  1. Reader 和 InputStream 的适配:在 Java I/O 包中,InputStreamReader 类就是一个适配器。InputStream 用于读取字节流,而 Reader 用于读取字符流。InputStreamReader 类将 InputStream 适配成 Reader,使得我们可以将字节流当作字符流来处理。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamReaderExample {
    public static void main(String[] args) {
        try (InputStream inputStream = System.in;
             InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            System.out.println("Enter some text: ");
            String line = bufferedReader.readLine();
            System.out.println("You entered: " + line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在上述代码中,System.in 是一个 InputStreamInputStreamReader 将其适配成 ReaderBufferedReader 又对 Reader 进行了包装,使得我们可以方便地从控制台读取字符数据。

  1. Swing 事件处理的适配器类:在 Swing 编程中,为了简化事件处理,Java 提供了一些适配器类。例如,MouseAdapter 类实现了 MouseListener 接口。如果我们只对鼠标点击事件感兴趣,而不需要实现 MouseListener 接口的所有方法,就可以继承 MouseAdapter 类并只重写 mouseClicked 方法。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseAdapterExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Mouse Adapter Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        JPanel panel = new JPanel();
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse clicked at (" + e.getX() + ", " + e.getY() + ")");
            }
        });

        frame.add(panel);
        frame.setVisible(true);
    }
}

在这个例子中,MouseAdapter 类作为适配器,使得我们可以选择性地实现 MouseListener 接口中的部分方法,而不是全部方法。

适配器模式的优缺点

  1. 优点
    • 提高复用性:通过适配器模式,可以复用那些接口不符合需求的类,而不需要对这些类进行修改,提高了代码的复用性。
    • 增强扩展性:当需要适配新的类时,只需要创建新的适配器类,而不需要修改现有代码,增强了系统的扩展性。
    • 解耦接口与实现:适配器模式将目标接口与适配者的实现解耦,使得两者可以独立变化,互不影响。
  2. 缺点
    • 增加代码复杂性:适配器模式引入了新的类(适配器类),增加了系统的代码复杂性,特别是在双向适配器模式中,代码可能会变得更加复杂。
    • 性能开销:在适配器模式中,可能会涉及到对象的创建和方法的转发,这可能会带来一定的性能开销,尤其是在频繁调用的情况下。

总结

适配器模式是一种非常实用的结构型设计模式,它在软件开发中有着广泛的应用。无论是在系统集成、第三方库使用还是代码复用方面,适配器模式都能帮助我们解决接口不兼容的问题。通过合理使用适配器模式,可以提高代码的复用性、扩展性和解耦度。在实际开发中,我们需要根据具体的需求和场景,选择合适的适配器模式(类适配器、对象适配器或双向适配器),同时也要注意适配器模式带来的代码复杂性和性能开销等问题。希望通过本文的介绍和示例,你对 Java 中适配器模式的应用有了更深入的理解和掌握,能够在实际项目中灵活运用适配器模式解决各种接口适配问题。