JAVA适配器特点_java设计模式-适配器模式

适配器模式允许不同接口的类协同工作,通过创建适配器类将适配者类的接口转换为客户期望的目标接口。文章详细介绍了类适配器、对象适配器和接口适配器的实现,并通过播放电影的案例进行示例代码演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.适配器模式(Adapter)的定义

将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。

适配器模式分为:类适配器模式、对象适配器模式、接口适配器模式。

2.适配器模式的主要优缺点

优点:

客户端通过适配器可以透明地调用目标接口。

复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。

将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。

缺点:

对类适配器来说,更换适配器的实现过程比较复杂。

3.适配器模式主要角色

目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。

适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。

适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

4.适配器模式结构

类适配器模式结构图

b91cd0176d809ae340aac09f525310ec.png

对象适配器模式结构图

0b31038778caf28b9a7ce8e1a5593493.png

5.适配器模式的实现

5.1 类适配器模式实现,已播放电影及切换电影为例

创建美剧电影目标接口

packagecom.lw.designpattern.adapter.classadapter;/*** @Classname AmericanMovieTarget

* @Description 美剧电影目标接口

* @Author lw

* @Date 2019-12-26 12:58*/

public interfaceAmericanMovieTarget {/*** 播放美剧

**/

public voidplayAmericanMovie();

}

创建韩剧适配者接口

packagecom.lw.designpattern.adapter.classadapter;/*** @Classname KoreanDramasAdaptee

* @Description 韩剧适配者接口

* @Author lw

* @Date 2019-12-26 13:08*/

public interfaceKoreanDramasAdaptee {/*** 播放韩剧*/

public voidplayKoreanDramas();

}

创建韩剧适配者接口实现

packagecom.lw.designpattern.adapter.classadapter;/*** @Classname KoreanDramasAdapteeImpl

* @Description 韩剧适配者接口实现

* @Author lw

* @Date 2019-12-26 13:03*/

public class KoreanDramasAdapteeImpl implementsKoreanDramasAdaptee {

@Overridepublic voidplayKoreanDramas(){

System.out.println("正在播放韩剧。。。。。。");

}

}

创建电影适配器类

packagecom.lw.designpattern.adapter.classadapter;/*** @Classname ClassMovieAdapter

* @Description 电影适配器

* @Author lw

* @Date 2019-12-26 13:01*/

public class ClassMovieAdapter extends KoreanDramasAdapteeImpl implementsAmericanMovieTarget {

@Overridepublic voidplayAmericanMovie() {

System.out.println("正在播放美剧。。。。。。");

System.out.println("开始切换。。。。。。");

playKoreanDramas();

}

}

单元测试

/*** 适配器模式-类适配器*/@Testpublic voidtestClassAdapter(){

AmericanMovieTarget americanMovie= newClassMovieAdapter();

americanMovie.playAmericanMovie();

}

打印结果

792a5aad0955a5b47ca9b2a80b2960de.png

5.2 对象适配器模式实现,已播放电影及切换电影为例

创建电影适配器类,相对于类适配器模式实现,稍作改动

packagecom.lw.designpattern.adapter.objectadapter;importcom.lw.designpattern.adapter.classadapter.AmericanMovieTarget;importcom.lw.designpattern.adapter.classadapter.KoreanDramasAdaptee;/*** @Classname ObjectMovieAdapter

* @Description 电影适配器

* @Author lw

* @Date 2019-12-26 13:01*/

public class ObjectMovieAdapter implementsAmericanMovieTarget {privateKoreanDramasAdaptee koreanDramas;publicObjectMovieAdapter(KoreanDramasAdaptee koreanDramas){this.koreanDramas =koreanDramas;

}

@Overridepublic voidplayAmericanMovie() {

System.out.println("正在播放美剧。。。。。。");

System.out.println("开始切换。。。。。。");

koreanDramas.playKoreanDramas();

}

}

单元测试

/*** 适配器模式-对象适配器*/@Testpublic voidtestObjectAdapter(){

AmericanMovieTarget americanMovie= new ObjectMovieAdapter(newKoreanDramasAdapteeImpl());

americanMovie.playAmericanMovie();

}

打印结果

aa035fe1554c08117678254cd7e947a0.png

5.3接口适配器模式实现,已播放电影及切换电影为例

创建电影目标接口类

packagecom.lw.designpattern.adapter.interfaceadpter;/*** @Classname MovieTarget

* @Description 电影目标接口

* @Author lw

* @Date 2019-12-26 13:18*/

public interfaceMovieTarget {/*** 播放美剧

**/

public voidplayAmericanMovie();/*** 播放韩剧*/

public voidplayKoreanDramas();

}

创建电影适配者抽象类,实现MovieTarget接口

packagecom.lw.designpattern.adapter.interfaceadpter;/*** @Classname AbstractMovieAdaptee

* @Description 电影适配者抽象类

* @Author lw

* @Date 2019-12-26 13:19*/

public abstract class AbstractMovieAdaptee implementsMovieTarget {

@Overridepublic voidplayAmericanMovie() { }

@Overridepublic voidplayKoreanDramas() { }

}

创建电影适配器类

packagecom.lw.designpattern.adapter.interfaceadpter;/*** @Classname InterfaceMovieAdapter

* @Description 电影适配器

* @Author lw

* @Date 2019-12-26 13:21*/

public class InterfaceMovieAdapter extendsAbstractMovieAdaptee {

@Overridepublic voidplayAmericanMovie() {

System.out.println("正在播放美剧。。。。。。");

}

@Overridepublic voidplayKoreanDramas() {

System.out.println("开始切换。。。。。。");

System.out.println("正在播放韩剧。。。。。。");

}

}

单元测试

/*** 适配器模式-接口适配器*/@Testpublic voidtestInterfacaAdapter(){

InterfaceMovieAdapter koreanDramasAdapter= newInterfaceMovieAdapter();

koreanDramasAdapter.playAmericanMovie();;

koreanDramasAdapter.playKoreanDramas();

}

打印结果

1a52fc853efe6f6ae62828c285803617.png

6.适配器模式的应用场景

以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致。

使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值