适配器模式是一种常见的设计模式,用于将一个类的接口转换成客户端所期望的另一个接口。在前端开发中,适配器模式可以帮助我们解决不同框架或库之间的兼容性问题,提高代码的复用性和可维护性。
- // 目标接口
- class Target {
- request() {
- throw new Error('This method should be overridden!');
- }
- }
-
- // 被适配对象
- class Adaptee {
- specificRequest() {
- return 'Specific request';
- }
- }
-
- // 适配器类
- class Adapter extends Target {
- constructor(adaptee) {
- super();
- this.adaptee = adaptee;
- }
-
- request() {
- const specificRequest = this.adaptee.specificRequest();
- // 对数据进行格式转换
- return specificRequest.toUpperCase();
- }
- }
-
- // 使用适配器
- const adaptee = new Adaptee();
- const adapter = new Adapter(adaptee);
- console.log(adapter.request()); // 输出:SPECIFIC REQUEST
request的方法,但这个方法没有具体实现,只是一个抛出错误的抽象方法。specificRequest的特定方法,这个方法实现了具体的功能。request方法。然后在request方法中,适配器调用了被适配对象的specificRequest方法,并对其返回的数据进行了格式转换(转换为大写)。在适配器模式中,适配器是一个新的类,它将被适配对象和目标接口连接起来,使得目标接口可以像调用被适配对象的specificRequest方法一样调用request方法。request方法时,适配器内部调用了被适配对象的specificRequest方法并进行了格式转换,最后输出结果为"SPECIFIC REQUEST"。- // 请求接口
- class Request {
- send() {
- throw new Error("This method should be overridden!");
- }
- }
-
- // 现代浏览器请求类
- class FetchRequest extends Request {
- send() {
- return fetch("/api/data").then((response) => response.json());
- }
- }
-
- // 旧版浏览器请求类
- class XHRRequest extends Request {
- send() {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.open("GET", "/api/data");
- xhr.onload = () => resolve(JSON.parse(xhr.responseText));
- xhr.onerror = () => reject(xhr.statusText);
- xhr.send();
- });
- }
- }
-
- // 适配器类
- class Adapter extends Request {
- constructor() {
- super();
- this.request = null;
- if (typeof window.fetch === "function") {
- this.request = new FetchRequest();
- } else {
- this.request = new XHRRequest();
- }
- }
- send() {
- return this.request.send();
- }
- }
-
- // 使用适配器发送请求
- const adapter = new Adapter();
- adapter.send().then((data) => console.log(data));
-
上述示例中创建了一个适配器类Adapter,它继承自Request类,并具有一个request属性。这个request属性实际上是一个现代浏览器的请求类(FetchRequest)或者一个旧版浏览器的请求类(XHRRequest)。
在适配器类的构造函数中,根据浏览器是否支持fetch函数,选择创建一个FetchRequest实例或者一个XHRRequest实例,然后赋值给request属性。
然后,在适配器类中,重写了send方法,这个方法调用了对应request实例的send方法。
最后,创建了一个适配器实例,并调用了它的send方法,这个方法会根据当前浏览器环境,使用对应的请求方式发送请求,然后打印返回的数据。
适配器模式是一种非常有用的设计模式,在前端开发中经常用于解决不同框架或库之间的兼容性问题。它可以提高代码复用性和可维护性,并且能够有效地解决兼容性问题。然而,使用适配器模式也需要注意增加了代码复杂性和可能引入的性能损耗。