摘要:本文主要向大家介绍了JAVA语言设计模式原则之适配器模式实例讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言设计模式原则之适配器模式实例讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
适配器模式定义
Adapter Pattern:Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interface.
适配器模式:将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
适配器通用类图
适配器模式就是把一个接口或类转换成其他的接口或类,适配器模式就是一个包装模式。
Target目标角色:
该角色定义把其他类转换为何种接口,也就是我们期望的接口 Adaptee源角色
你想把谁转换为目标角色,这个谁就是源角色,它是已经存在的,运行良好的类或对象,经过适配角色的包装,它会成为一个新的角色 Adapter适配器角色
适配器模式的核心角色,其他两个角色都存在,而适配器角色是需要新建立的,它的职责:把源角色转换为目标角色,怎么转换?通过继承或是关联的方式。
类适配器通用代码
目标角色:
public interface Target {
//目标角色有自己的方法
public void request();
}
目标角色的实现类:
public class ConcreteTarget implements Target {
@Override
public void request() {
System.out.println("if you need any help, please call me!");
}
}
源角色:
public class Adaptee {
public void doSomeThing(){
System.out.println("I'm kind of busy, leave me alone please!");
}
}
适配器角色:
public class Adapter extends Adaptee implements Target {
@Override
public void request() {
super.doSomeThing();
}
}
场景类:
public class Client {
public static void main(String[] args) {
Target target = new ConcreteTarget();
target.request();
System.out.println("--------------------------");
Target target2 = new Adapter();
target2.request();
}
}
对象适配器
对象适配器是通过对对象关联的方法进行的适配。
对象适配器uml图:
//arget 类:
public interface Target {
public void request();
}
//ConcreteTarget 类:
public class ConcreteTarget implements Target {
@Override
public void request() {
System.out.println("ConcreteTarget-----request()");
}
}
//Adaptee01 类:
public class Adaptee01 {
public void SpecificRequest1(){
System.out.println("Adaptee01--------------SpecificRequest1()");
}
}
//Adaptee02 类:
public class Adaptee02 {
public void SpecificRequest2(){
System.out.println("Adaptee02--------------SpecificRequest2()");
}
}
//Adaptee03 类:
public class Adaptee03 {
public void SpecificRequest3(){
System.out.println("Adaptee03--------------SpecificRequest3()");
}
}
//Adapter 类:
public class Adapter implements Target {
private Adaptee01 adaptee01;
private Adaptee02 adaptee02;
private Adaptee03 adaptee03;
public Adapter(Adaptee01 adaptee01, Adaptee02 adaptee02, Adaptee03 adaptee03) {
super();
this.adaptee01 = adaptee01;
this.adaptee02 = adaptee02;
this.adaptee03 = adaptee03;
}
@Override
public void request() {
// TODO Auto-generated method stub
System.out.println("Adapter-----request()");
adaptee01.SpecificRequest1();
adaptee02.SpecificRequest2();
adaptee03.SpecificRequest3();
}
}
//Client 类:
public class Client {
public static void main(String[] args) {
Adaptee01 adaptee01 = new Adaptee01();
Adaptee02 adaptee02 = new Adaptee02();
Adaptee03 adaptee03 = new Adaptee03();
Adapter adapter = new Adapter(adaptee01, adaptee02, adaptee03);
adapter.request();
}
}
优点
适配器模式可以让两个没有任何关系的类在一起运行,只要一个适配器就能够搞定 增加类的透明性
我们访问的Target目标角色,但是具体的实现都委托给了源角色,而这些对高层次模块是透明的 提高了类的复用度 灵活性非常好
如果不想用适配器,删除这个适配器就可以了,其他的代码都不要动,基本上就类似一个灵活的构件,想用就用,不想就删除。
注意事项
适配器模式使用的主要场景是应用扩展。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号