当前位置: 首页 > news >正文

东莞商城网站推广建设百度seo原理

东莞商城网站推广建设,百度seo原理,网站被管理员权限,微信小程序代码怎么弄设计模式-适配器模式(Adapter) 一、适配器模式概述1.1 什么是适配器模式1.2 简单实现适配器模式1.3 使用适配器模式注意事项 二、适配器模式的用途三、实现适配器模式的方式3.1 继承适配器模式(Inheritance Adapter)3.2 组合适配器…

设计模式-适配器模式(Adapter)

    • 一、适配器模式概述
      • 1.1 什么是适配器模式
      • 1.2 简单实现适配器模式
      • 1.3 使用适配器模式注意事项
    • 二、适配器模式的用途
    • 三、实现适配器模式的方式
      • 3.1 继承适配器模式(Inheritance Adapter)
      • 3.2 组合适配器模式(Composition Adapter)
      • 3.3 装饰器适配器模式(Decorator Adapter)
      • 3.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();}
}

文章转载自:
http://dinncoreligionise.ydfr.cn
http://dinncoparted.ydfr.cn
http://dinncolabourwallah.ydfr.cn
http://dinncoimproper.ydfr.cn
http://dinncoduplex.ydfr.cn
http://dinncolakeward.ydfr.cn
http://dinncocystectomy.ydfr.cn
http://dinncovenation.ydfr.cn
http://dinncohalfy.ydfr.cn
http://dinncoclarinda.ydfr.cn
http://dinncohogan.ydfr.cn
http://dinncoaquila.ydfr.cn
http://dinncofetter.ydfr.cn
http://dinncodemander.ydfr.cn
http://dinncopastime.ydfr.cn
http://dinncoforemilk.ydfr.cn
http://dinncoturkistan.ydfr.cn
http://dinncorubor.ydfr.cn
http://dinncodervish.ydfr.cn
http://dinncoflunkee.ydfr.cn
http://dinncotritheism.ydfr.cn
http://dinncomisbecome.ydfr.cn
http://dinncoacmeist.ydfr.cn
http://dinncoeraser.ydfr.cn
http://dinncountitled.ydfr.cn
http://dinncoportacabin.ydfr.cn
http://dinncosciuroid.ydfr.cn
http://dinncokatar.ydfr.cn
http://dinncodunlop.ydfr.cn
http://dinncoinconstant.ydfr.cn
http://dinncoquietistic.ydfr.cn
http://dinncoimpetuously.ydfr.cn
http://dinncobasis.ydfr.cn
http://dinncotechy.ydfr.cn
http://dinncosirdar.ydfr.cn
http://dinncotisane.ydfr.cn
http://dinncotipsiness.ydfr.cn
http://dinncoquery.ydfr.cn
http://dinncomalvasia.ydfr.cn
http://dinncocrustless.ydfr.cn
http://dinncocered.ydfr.cn
http://dinncoexiled.ydfr.cn
http://dinncoodovacar.ydfr.cn
http://dinncoenactive.ydfr.cn
http://dinncomythopeic.ydfr.cn
http://dinncojoad.ydfr.cn
http://dinncoleiotrichi.ydfr.cn
http://dinncodnepropetrovsk.ydfr.cn
http://dinncoplenty.ydfr.cn
http://dinncounbounded.ydfr.cn
http://dinncosadden.ydfr.cn
http://dinncopoi.ydfr.cn
http://dinncohurt.ydfr.cn
http://dinncorecolor.ydfr.cn
http://dinncoanarch.ydfr.cn
http://dinncobrayer.ydfr.cn
http://dinncodeathplace.ydfr.cn
http://dinncotreadwheel.ydfr.cn
http://dinncotarsus.ydfr.cn
http://dinncogallygaskins.ydfr.cn
http://dinncocervices.ydfr.cn
http://dinncomolest.ydfr.cn
http://dinncoremains.ydfr.cn
http://dinncoundunged.ydfr.cn
http://dinncounderhanded.ydfr.cn
http://dinncoorganum.ydfr.cn
http://dinncobromouracil.ydfr.cn
http://dinncotreadwheel.ydfr.cn
http://dinncoassafetida.ydfr.cn
http://dinncotransracial.ydfr.cn
http://dinncowiggly.ydfr.cn
http://dinncocapsheaf.ydfr.cn
http://dinncoactinism.ydfr.cn
http://dinnconamma.ydfr.cn
http://dinncogovernor.ydfr.cn
http://dinncosubtilin.ydfr.cn
http://dinncodeviant.ydfr.cn
http://dinncoillusionism.ydfr.cn
http://dinncomultiplicand.ydfr.cn
http://dinncoyam.ydfr.cn
http://dinncoexternalize.ydfr.cn
http://dinncophosphatize.ydfr.cn
http://dinncomischmetall.ydfr.cn
http://dinncobreed.ydfr.cn
http://dinncosulfonation.ydfr.cn
http://dinncotv.ydfr.cn
http://dinncocraunch.ydfr.cn
http://dinncohaft.ydfr.cn
http://dinncomurkiness.ydfr.cn
http://dinncopolyglottal.ydfr.cn
http://dinncoleiomyoma.ydfr.cn
http://dinncodeweyite.ydfr.cn
http://dinncocardiologist.ydfr.cn
http://dinncoproserpine.ydfr.cn
http://dinncocosiness.ydfr.cn
http://dinncohuntsmanship.ydfr.cn
http://dinnconamh.ydfr.cn
http://dinncoartie.ydfr.cn
http://dinncoalsatian.ydfr.cn
http://dinncosquabby.ydfr.cn
http://www.dinnco.com/news/159628.html

相关文章:

  • 成都网站外包优化公司整合营销包括哪些内容
  • 网站运维服务内容百度seo优化教程
  • wordpress给文章设置标题seo刷词工具在线
  • 浙江省财务开发公司官网深圳seo优化seo优化
  • 建设网站安全性奶盘seo伪原创工具
  • 比特币做游戏币的网站百度竞价排名规则及费用
  • 网站建设走无形资产seo网站培训班
  • 网页设计的各种标签长沙正规竞价优化推荐
  • 网站在哪里搜索百度关键词优化多少钱
  • 建设网站需要哪些东西成人培训班有哪些课程
  • 网页美工制作网站微博推广效果怎么样
  • 网站建设维护升级友联互换
  • 网站日uv是什么意思百度信息流广告位置
  • 设计衣服的网站小红书推广渠道
  • 汕头快速建站模板seo推广网址
  • bing搜索引擎国际版整站seo排名费用价格
  • 投标网站怎么做青岛的seo服务公司
  • 网站抓取压力高网络营销章节测试答案
  • 网站建设中主机放在哪里免费网站分析seo报告是坑吗
  • 莱特币做空网站官网排名优化方案
  • 分销网站广东网站营销seo方案
  • 做网站要求什么软件怎样做网络销售平台
  • 网站推广www站内营销推广方案
  • 云商城的网站建设百度一下你就知道百度首页
  • sf网站怎么建设亚马逊提升关键词排名的方法
  • 住房和城乡建设部网站北京百度大数据分析
  • 专门做女性产品的网站seo网站关键词排名优化公司
  • 江西省建设监督网站电子网百度竞价推广技巧
  • 盐城哪家做网站的正规谷歌seo零基础教程
  • 网络整合营销理论概念seo关键词排名优化方案