宁波网站seo诊断工具,中国建工社微课程官网,孟津网站开发,小程序开发平台哪个品牌好设计模式-适配器模式#xff08;Adapter#xff09; 一、适配器模式概述1.1 什么是适配器模式1.2 简单实现适配器模式1.3 使用适配器模式注意事项 二、适配器模式的用途三、实现适配器模式的方式3.1 继承适配器模式#xff08;Inheritance Adapter#xff09;3.2 组合适配器… 设计模式-适配器模式Adapter 一、适配器模式概述1.1 什么是适配器模式1.2 简单实现适配器模式1.3 使用适配器模式注意事项 二、适配器模式的用途三、实现适配器模式的方式3.1 继承适配器模式Inheritance Adapter3.2 组合适配器模式Composition Adapter3.3 装饰器适配器模式Decorator Adapter3.4 代理适配器模式Proxy Adapter 一、适配器模式概述
1.1 什么是适配器模式
适配器模式Adapter Pattern是一种结构型设计模式它主要用于解决两个不兼容的接口之间的问题。这种模式通过结合两个独立接口的功能使原本不能一起工作的那些类可以一起工作。
适配器模式涉及到一个单一的类这个类负责将独立的或不兼容的接口功能整合到一起。举个例子读卡器就是作为内存卡和笔记本之间的适配器。我们先把内存卡插入读卡器再把读卡器插入笔记本然后就可以通过笔记本来读取内存卡的内容了。
适配器模式也被称为Wrapper包装器因为它主要是将目标类用一个新类包装一下即在客户端与目标类之间加了一层。这解决了现存的类提供的接口与我们系统的接口不兼容而我们还不能修改现存类的问题。
1.2 简单实现适配器模式
首先我们定义一个目标接口Target
public interface Target {void request();
}然后我们创建一个实现了Target接口的具体类ConcreteTarget
public class ConcreteTarget implements Target {Overridepublic void request() {System.out.println(ConcreteTarget 的 request 方法被调用);}
}接下来我们创建一个需要适配的接口Adaptee
public interface Adaptee {void specificRequest();
}然后我们创建一个实现了Adaptee接口的具体类ConcreteAdaptee
public class ConcreteAdaptee implements Adaptee {Overridepublic void specificRequest() {System.out.println(ConcreteAdaptee 的 specificRequest 方法被调用);}
}现在我们需要创建一个适配器类Adapter它也实现了Target接口并包含一个Adaptee类型的成员变量。在Adapter类的request方法中我们将调用Adaptee的specificRequest方法
public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee adaptee;}Overridepublic void request() {System.out.println(Adapter 的 request 方法被调用);adaptee.specificRequest();}
}最后我们在客户端代码中使用适配器模式
public class Client {public static void main(String[] args) {Adaptee adaptee new ConcreteAdaptee();Target target new Adapter(adaptee);target.request();}
}运行客户端代码输出结果如下
Adapter 的 request 方法被调用
ConcreteAdaptee 的 specificRequest 方法被调用1.3 使用适配器模式注意事项
在使用适配器模式时需要注意以下几点 1、适配器模式主要解决现有接口无法改变的问题。也就是说适配器不是在详细设计阶段添加的而是在解决正在运行的项目问题时使用的。 2、由于Java是单继承机制类适配器需要继承src类这一点算是一个缺点。因为这意味着目标类必须是接口这就给设计带来了一定的局限性。 3、src类的方法在Adapter中都会暴露出来这可能会增加使用的成本。 4、尽管适配器模式可以解决一些兼容性问题但如果没有合理使用可能会导致系统复杂性增加。因此在使用时需要权衡利弊尽量避免过度使用。 5、当适配器的任何方法被调用时它会将参数转换为合适的格式然后将调用定向到其封装对象中的一个或多个方法。因此正确实现适配器模式对于保证代码的稳定运行至关重要。
二、适配器模式的用途
适配器模式主要应用于解决系统需要使用现有类但这些类的接口不符合系统的需要即接口不兼容的问题。以下是一些具体的应用场景 1、系统需要使用现有的类而此类的接口不符合系统的需要即接口不兼容。例如你可能需要将一个现有的类如某个第三方库中的类与你自己的代码一起使用但这个现有类的接口与你所需要的接口不匹配。这时你可以创建一个适配器类将现有的类适配到你所需要的接口。 2、如果你想建立一个可以重复使用的类用于与一些彼此之间没有太大关联的一些类包括一些可能在将来引进的类一起工作。例如你可能需要开发一个通用的数据处理模块该模块应该能够处理多种不同类型的数据源如数据库、文件、网络等。这时你可以创建一个适配器类将不同的数据源适配到你的数据处理模块所期望的接口。 3、需要一个统一的输出接口而输入端的类型不可预知。例如你可能需要编写一个函数该函数接受一个对象作为参数并调用该对象的某个方法。但是该方法的参数类型和返回值类型可能有很多种可能性。这时你可以创建一个适配器类将不同的参数类型和返回值类型适配到你所需要的接口。 4、当需增加客户端与目标类之间的抽象层以控制对目标类的访问。这种情况下适配器模式可以为客户端提供一个与目标类更为友好的接口。
三、实现适配器模式的方式
3.1 继承适配器模式Inheritance Adapter
通过继承原有的类实现适配器功能。这种方式适用于接口不兼容的情况但需要修改原有类的代码。
public class Adaptee {public void specificRequest() {System.out.println(Adaptee specific request);}
}public class Adapter extends Adaptee {Overridepublic void request() {super.specificRequest();}
}3.2 组合适配器模式Composition Adapter
通过组合的方式实现适配器功能。这种方式适用于接口不兼容的情况且不需要修改原有类的代码。
public interface Target {void request();
}public class Adaptee {public void specificRequest() {System.out.println(Adaptee specific request);}
}public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee adaptee;}Overridepublic void request() {adaptee.specificRequest();}
}3.3 装饰器适配器模式Decorator Adapter
通过装饰器模式实现适配器功能。这种方式适用于接口不兼容的情况且不需要修改原有类的代码。
public interface Target {void request();
}public class Adaptee {public void specificRequest() {System.out.println(Adaptee specific request);}
}public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee adaptee;}Overridepublic void request() {adaptee.specificRequest();}
}3.4 代理适配器模式Proxy Adapter
通过代理模式实现适配器功能。这种方式适用于接口不兼容的情况且不需要修改原有类的代码。
public interface Target {void request();
}public class Adaptee {public void specificRequest() {System.out.println(Adaptee specific request);}
}public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee adaptee;}Overridepublic void request() {adaptee.specificRequest();}
}