适配器模式
字数: 0

基本概念

Adapter Pattern: Convert the interface of a class into another interface that clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces.
notion image

实现代码

假设我们有一个旧的 MediaPlayer 接口和一个实现了这个接口的 AudioPlayer 类 (mp3)。但现在我们想要支持更多的媒体格式 (mp4),但不想改变 AudioPlayer 类。

目标

Target

适配类

Adaptee

适配器

Adapter

测试类

具体应用

java.util.Arrays#asList()

notion image
 
2023 - 2026