概要
interface Player {void action();}interface Mp4 {void play();}class ExpensiveMp4 implements Mp4 {@Overridepublic void play() {System.out.println("play mp4");}}public class PlayerAdapter extends ExpensiveMp4 implements Player {@Overridepublic void action() {play();}}
接口适配器
interface Player {void action();}interface Mp4 {void play();}class ExpensiveMp4 implements Mp4 {@Overridepublic void play() {System.out.println("play mp4");}}public class PlayerAdapter implements Player {private Mp4 mp4;public PlayerAdapter(Mp4 mp4) {this.mp4 = mp4;}@Overridepublic void action() {mp4.play();}}
总结
- 提高代码复用
- 灵活性非常好
- 适配器其实是对目标类/接口的包装,实际工作只有目标类/接口
