适配器模式
什么是适配器模式?
适配器模式(Adapter)的定义如下:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。适配器模式分为类结构型模式和对象结构型模式两种,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。
该模式的主要优点如下。
- 客户端通过适配器可以透明地调用目标接口。
- 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
- 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
- 在很多业务场景中符合开闭原则。
其缺点是:
- 适配器编写过程需要结合业务场景全面考虑,可能会增加系统的复杂性。
- 增加代码阅读难度,降低代码可读性,过多使用适配器会使系统代码变得凌乱。
1. 模式的结构
适配器模式(Adapter)包含以下主要角色。
- 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
- 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
- 适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。
1.类适配器模式:
//目标接口
public interface Target {
//交流电
public void alternatingCurrent();
}
//适配者
public class Adaptee {
public void directCurrent(){
System.out.println("这个是直流电");//交流被适配成直流
}
}
//类适配器
public class ClassAdapter extends Adaptee implements Target{
@Override
public void alternatingCurrent(){
super.directCurrent();//适配成直流电
}
}
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.charge();
}
public void charge() {
Target target = new ClassAdapter();
System.out.println("直流电脑正在充电...");
target.alternatingCurrent();
}
}
2.对象适配器模式:
//目标接口
public interface Target {
//交流电
public void alternatingCurrent();
}
//适配者
public class Adaptee {
public void directCurrent(){
System.out.println("这个是直流电");//交流被适配成直流
}
}
//对象适配器
public class ObjectAdapter implements Target{
private Adaptee adaptee;
public ObjectAdapter(Adaptee adaptee){
this.adaptee = adaptee;
}
@Override
public void alternatingCurrent(){
adaptee.directCurrent();//适配成直流电
}
}
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.charge();
}
public void charge() {
Adaptee adaptee = new Adaptee();
Target target = new ObjectAdapter(adaptee);
System.out.println("直流电脑正在充电...");
target.alternatingCurrent();
}
}
两个不同点:
1.在适配器中,类适配器继承了被适配类Adaptee,然后通过调用父类Adaptee的方法来实现Target类中的方法,从而完成适配;
2.在适配器中,对象适配器私有化了被适配类对象Adaptee,然后在构造函数中将Adaptee作为参数传递进去,然后直接调用Adaptee中的方法实现Target类中的适配方法,从而完成适配.
拓展:
模式的扩展
适配器模式(Adapter)可扩展为双向适配器模式,双向适配器类既可以把适配者接口转换成目标接口,也可以把目标接口转换成适配者接口,其结构图如图所示。
package adapter;
//目标接口
interface TwoWayTarget
{
public void request();
}
//适配者接口
interface TwoWayAdaptee
{
public void specificRequest();
}
//目标实现
class TargetRealize implements TwoWayTarget
{
public void request()
{
System.out.println("目标代码被调用!");
}
}
//适配者实现
class AdapteeRealize implements TwoWayAdaptee
{
public void specificRequest()
{
System.out.println("适配者代码被调用!");
}
}
//双向适配器
class TwoWayAdapter implements TwoWayTarget,TwoWayAdaptee
{
private TwoWayTarget target;
private TwoWayAdaptee adaptee;
public TwoWayAdapter(TwoWayTarget target)
{
this.target=target;
}
public TwoWayAdapter(TwoWayAdaptee adaptee)
{
this.adaptee=adaptee;
}
public void request()
{
adaptee.specificRequest();
}
public void specificRequest()
{
target.request();
}
}
//客户端代码
public class TwoWayAdapterTest
{
public static void main(String[] args)
{
System.out.println("目标通过双向适配器访问适配者:");
TwoWayAdaptee adaptee=new AdapteeRealize();
TwoWayTarget target=new TwoWayAdapter(adaptee);
target.request();
System.out.println("-------------------");
System.out.println("适配者通过双向适配器访问目标:");
target=new TargetRealize();
adaptee=new TwoWayAdapter(target);
adaptee.specificRequest();
}
}
文章知识点概括来自