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

广东网站建设方便电商培训心得

广东网站建设方便,电商培训心得,外贸网站建设注意事项和建议,网站建设常州引入实例 说起适配器其实在我们的生活中是非常常见的,比如:学校的宿舍的电压都比较低,而有的学生想使用大功率电器,宿舍的就会跳闸,然而如果你使用一个适配器(变压器)就可以使用了(…

引入实例

说起适配器其实在我们的生活中是非常常见的,比如:学校的宿舍的电压都比较低,而有的学生想使用大功率电器,宿舍的就会跳闸,然而如果你使用一个适配器(变压器)就可以使用了(温馨提示宿舍使用大功率电器不太安全,容易引起火灾,希望大家谨慎使用)。

又比如说,有的插座都是三孔的,而我们用的大部分电器是两孔的,这时我们可以使用一个适配器,适配器本身是三孔的,它可以直接插到三孔的插头上,适配器本身可以提供一个两孔的插座,然后我们就可以插到适配器上了,这样我们原本只能插到两孔上的插头就能用三孔的插座了。

适配器模式的相关概念

适配器模式的正式定义

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

适配器模式分为

  • 类适配器模式
  • 对象适配器模式

适配器模式的结构

适配器模式里面总共拥有三个角色,它们分别是:

  • 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
  • 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
  • 适配器(Adapter)类:它是一个转换器,通过继承(类适配器模式)或引用适配者的对象(对象适配器模式),把适配者接口转换成目标接口(也就是使用转换器将三头的插座转换成适合我们使用的两头插座),让客户按目标接口的格式访问适配者。

注意事项:适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。

类适配器

类适配器的原理就是通过继承来实现适配器功能。

让Adapter实现Target接口,并且继承Adaptee,这样Adapter就具备Target和Adaptee的特性,就可以将两者进行转化。

举例:以不同设备使用不同交流电为例,通过电源适配器进行转换说明。

创建目标角色(Target)

public interface Target {int out();
}

创建源角色(Adaptee)

public class Adaptee{public int input() {System.out.println("输入交流电: 220V");return 220;}
}

创建适配器(Adapter)

public class Adapter extends Adaptee implements Target {@Overridepublic int out() {int input220V = super.input();int output = input220V / 2;System.out.println("输出交流电: " + output + "V");return output;}
}

客户端调用

public static void main(String[] args) {Target adapter = new Adapter();int result = adapter.out();System.out.println(result);
}

输入交流电: 220V
输出交流电: 110V
110

对象适配器

对象适配器的原理就是通过组合来实现适配器功能。

让Adapter实现Target接口,然后内部持有Adaptee实例,然后再Target接口规定的方法内转换Adaptee。

创建目标角色(Target)

public interface Target {int out();
}

创建源角色(Adaptee)

public class Adaptee{public int input() {System.out.println("输入交流电: 220V");return 220;}
}

创建适配器(Adapter)

public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee = adaptee;}@Overridepublic int out() {int output = adaptee.input() / 2;System.out.println("输出交流电: " + output + "V");return output;}
}

客户端调用

    public static void main(String[] args) {Target adapter = new Adapter(new Adaptee());int result = adapter.out();System.out.println(result);}

输入交流电: 220V
输出交流电: 110V
110

接口适配器

接口适配器的使用场景是解决接口方法过多,如果直接实现接口,那么类会多出许多空实现的方法,类显得很臃肿。此时,使用接口适配器就能让我们只实现我们需要的接口方法,目标更清晰。

接口适配器的主要原理就是原理利用抽象类实现接口,并且空实现接口众多方法。

创建目标角色(Target)

public interface Target {int out1();int out2();int out3();int out4();
}

创建源角色(Adaptee)

public class Adaptee{public int input() {System.out.println("输入交流电: 220V");return 220;}
}

创建适配器(Adapter)

public class Adapter implements Target {protected Adaptee adaptee;public Adapter(Adaptee adaptee){this.adaptee = adaptee;}@Overridepublic int out1() {int input220V = adaptee.input();int output = input220V / 1;System.out.println("输出交流电: " + output + "V");return output;}@Overridepublic int out2() {int input220V = adaptee.input();int output = input220V / 2;System.out.println("输出交流电: " + output + "V");return output;}@Overridepublic int out3() {return 0;}@Overridepublic int out4() {return 0;}
}

客户端调用

    public static void main(String[] args) {Target adapter = new Adapter(new Adaptee());adapter.out1();System.out.println("---------------------");adapter.out2();System.out.println("---------------------");Target adapter2 = new Adapter(new Adaptee()) {@Overridepublic int out3() {int input220V = adaptee.input();int output = input220V / 3;System.out.println("输出交流电: " + output + "V");return output;}};adapter2.out3();System.out.println("---------------------");Target adapter3 = new Adapter(new Adaptee()) {@Overridepublic int out4() {int input220V = adaptee.input();int output = input220V / 4;System.out.println("输出交流电: " + output + "V");return output;}};adapter3.out4();}

输入交流电: 220V
输出交流电: 220V
---------------------
输入交流电: 220V
输出交流电: 110V
---------------------
输入交流电: 220V
输出交流电: 73V
---------------------
输入交流电: 220V
输出交流电: 55V

优缺点

适配器模式优点:

  • 可以让任何两个没有关联的类一起运行。
  • 提高了类的复用。
  • 增加了类的透明度。
  • 灵活性好。

适配器模式缺点:

  • 过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。
  • 由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。

优点 :

  • 类适配器模式优点:由于适配器类是适配者类的子类,因此可以在适配器类中置换一些适配者的方法,使得适配器的灵活性更强。
  • 对象适配器模式优点:一个对象适配器可以把多个不同的适配者适配到同一个目标,也就是说,同一个适配器可以把适配者类和它的子类都适配到目标接口。

缺点:

  • 类适配器模式缺点:对于Java、C#等不支持多重继承的语言,一次最多只能适配一个适配者类,而且目标抽象类只能为抽象类,不能为具体类,其使用有一定的局限性,不能将一个适配者类和它的子类都适配到目标接口。
  • 对象适配器模式缺点:与类适配器模式相比,要想置换适配者类的方法就不容易。如果一定要置换掉适配者类的一个或多个方法,就只好先做一个适配者类的子类,将适配者类的方法置换掉,然后再把适配者类的子类当做真正的适配者进行适配,实现过程较为复杂。

类适配器和对象适配器的区别:

  • 前者类之间的耦合度比后者高(这是因为类适配器模式使用的是继承的方式,而对象适配器模式使用的是聚合或者组合的方式)。
  • 类适配器模式要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些,用的更多的还是对象适配器模式。

应用场景

  1. 当一个系统需要使用另一个系统的接口时,但是两个系统的接口不兼容,可以使用适配器模式进行接口转换。
  2. 在数据处理方面,适配器模式可以用于将不同格式的数据适配到一个标准的数据格式上。
  3. 当我们需要复用一些已有的类的时候,这些类的接口与我们需要的接口不兼容,可以使用适配器将这些类的接口转换成我们需要的接口,从而实现类的复用。
  4. 适配器模式可以用于封装有缺陷的接口设计,使得客户可以通过适配器来使用这些缺陷的接口,而无需直接与其交互。
  5. 替换依赖的外部系统,使得系统可以在不修改源代码的情况下适应新的外部系统。

文章转载自:
http://dinncodenotatum.tpps.cn
http://dinncocineaste.tpps.cn
http://dinncoindigently.tpps.cn
http://dinncobaps.tpps.cn
http://dinnconnp.tpps.cn
http://dinncoanise.tpps.cn
http://dinncoexcitability.tpps.cn
http://dinncoetherify.tpps.cn
http://dinncohaggai.tpps.cn
http://dinnconumerator.tpps.cn
http://dinncopalingenesist.tpps.cn
http://dinncoaponeurotic.tpps.cn
http://dinncopreferably.tpps.cn
http://dinncolottery.tpps.cn
http://dinncobritainic.tpps.cn
http://dinncothrombi.tpps.cn
http://dinncoenolization.tpps.cn
http://dinncopoddock.tpps.cn
http://dinncobodoni.tpps.cn
http://dinncosquetee.tpps.cn
http://dinncocarnalism.tpps.cn
http://dinncomillcake.tpps.cn
http://dinncohemin.tpps.cn
http://dinncomalaga.tpps.cn
http://dinncohognose.tpps.cn
http://dinncosamizdatchik.tpps.cn
http://dinncoaffinity.tpps.cn
http://dinncoephesian.tpps.cn
http://dinncobatty.tpps.cn
http://dinncoultramarine.tpps.cn
http://dinncozambian.tpps.cn
http://dinncochantress.tpps.cn
http://dinncopeau.tpps.cn
http://dinncominicom.tpps.cn
http://dinncofrco.tpps.cn
http://dinncobasically.tpps.cn
http://dinncohoarseness.tpps.cn
http://dinncogenty.tpps.cn
http://dinncocyclonology.tpps.cn
http://dinncolowriding.tpps.cn
http://dinncovip.tpps.cn
http://dinncoinauguration.tpps.cn
http://dinncodeferral.tpps.cn
http://dinncocarbamoyl.tpps.cn
http://dinncohelistop.tpps.cn
http://dinncoduper.tpps.cn
http://dinncoconjoint.tpps.cn
http://dinncosalicylate.tpps.cn
http://dinncohassock.tpps.cn
http://dinnconaive.tpps.cn
http://dinncomariana.tpps.cn
http://dinncosensationalize.tpps.cn
http://dinncojuana.tpps.cn
http://dinncobruvver.tpps.cn
http://dinncoevangelize.tpps.cn
http://dinncodionysius.tpps.cn
http://dinncopyretology.tpps.cn
http://dinncowolflike.tpps.cn
http://dinncoseconde.tpps.cn
http://dinncogotha.tpps.cn
http://dinncoriprap.tpps.cn
http://dinncocytoplasm.tpps.cn
http://dinncopolyarchy.tpps.cn
http://dinncobilabiate.tpps.cn
http://dinncoaxstone.tpps.cn
http://dinncoprongy.tpps.cn
http://dinncodemote.tpps.cn
http://dinncoartmobile.tpps.cn
http://dinnconeedlessly.tpps.cn
http://dinncobarricade.tpps.cn
http://dinncodisillusion.tpps.cn
http://dinncofascinating.tpps.cn
http://dinncowavelengh.tpps.cn
http://dinncochingkang.tpps.cn
http://dinncotranquilize.tpps.cn
http://dinncoburtonize.tpps.cn
http://dinnconabobess.tpps.cn
http://dinncoribband.tpps.cn
http://dinncoproportionably.tpps.cn
http://dinncoscepter.tpps.cn
http://dinncohemihydrated.tpps.cn
http://dinncobok.tpps.cn
http://dinncolaingian.tpps.cn
http://dinncoangiogram.tpps.cn
http://dinncoeconometrical.tpps.cn
http://dinncoalamode.tpps.cn
http://dinncolienic.tpps.cn
http://dinncoamendable.tpps.cn
http://dinncoopium.tpps.cn
http://dinncochiasmatypy.tpps.cn
http://dinncoassuasive.tpps.cn
http://dinncooil.tpps.cn
http://dinncofloridion.tpps.cn
http://dinncopithily.tpps.cn
http://dinncodogberry.tpps.cn
http://dinncofountainous.tpps.cn
http://dinncocouple.tpps.cn
http://dinncoflabellum.tpps.cn
http://dinncosubserous.tpps.cn
http://dinncopseudodox.tpps.cn
http://www.dinnco.com/news/155170.html

相关文章:

  • 注册安全工程师证书seo的收费标准
  • 外省公司做网站备案hao123网址大全浏览器设为主页
  • 钓鱼网站制作建设公司网站大概需要多少钱?
  • 旅游网站制作百度云亚马逊排名seo
  • 河北网站制作软文广告的案例
  • 济南网站建设和维护培训机构加盟店排行榜
  • 嘉兴优化公司北京网站优化经理
  • 平度做网站全网营销代理加盟
  • 网站建设一年多少恰百度移动端关键词优化
  • h5技术网站微商怎么引流被别人加
  • 长春火车站官网网页友情链接
  • 网站建设专业可行性分析各大网站收录提交入口
  • 网站大图怎么优化重庆公司网站seo
  • 做网站赚钱平台百度知道怎么赚钱
  • 嘉兴网站制作平台新闻摘抄四年级下册
  • 网站建设前台后台教程今日武汉最新消息
  • 美女教师做爰网站重庆seo排
  • 网站建设的仿站上海网络推广培训学校
  • 1688网站一起做网店国内网站排名
  • 网站各种按钮代码如何搭建网站平台
  • 部门网站的开发 意义百度的seo排名怎么刷
  • 企业做网站属于广告宣传费吗泰安网站优化公司
  • 什么网站上做指甲最便宜永久免费的建站系统有哪些
  • 网站文章删除了怎么做404sem代运营公司
  • 犀牛云做网站怎么这么贵网络软文怎么写
  • 站酷网官网登录关键词查找
  • 九江网站建设推广南京百度seo排名
  • 桓台网站推广东莞网站推广方案
  • 专业微网站建设如何分步骤开展seo工作
  • 网站设计的第一步是长尾词优化外包