设计模式之适配器模式--- Pattern Adapter
来源:互联网 发布:linux vi编程 编辑:程序博客网 时间:2024/04/30 18:18
模式的定义
适配器模式(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目标角色,但是具体的实现都委托给了源角色,而这些对高层次模块是
透明的
- 提高了类的复用度
灵活性非常好
如果不想用适配器,删除这个适配器就可以了,其他的代码都不要动,基本上就类似一个灵活的构件,想用就用,不想就删除。
类适配器
类适配器是通过继承进行的适配
类适配器UML类图
类适配器角色介绍
Target目标角色:
该角色定义把其他类转换为何种接口,也就是我们期望的接口
Adaptee源角色
你想把谁转换为目标角色,这个谁就是源角色,它是已经存在的,运行良好的类或对象,经过适配角色的包装,它会成为一个新的角色
Adapter适配器角色
适配器模式的核心角色,其他两个角色都存在,而适配器角色是需要新建立的,它的职责:把源角色转换为目标角色,怎么转换?通过继承或是关联的方式。
类适配器模式的通用源码
Target 类:
public interface Target { public void request();}
ConcreteTarget 类:
public class ConcreteTarget implements Target { @Override public void request() { // TODO Auto-generated method stub System.out.println("ConcreteTarget-----request()"); }}
Adaptee :
public class Adaptee { public void doSomeThing(){ System.out.println("Adaptee-----doSomeThing()"); }}
Adapter :
public class Adapter extends Adaptee implements Target { @Override public void request() { // TODO Auto-generated method stub System.out.println("Adapter-----request()"); super.doSomeThing(); }}
Client :
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub Target target = new ConcreteTarget(); target.request(); System.out.println("--------------------------"); Target target2 = new Adapter(); target2.request(); }}
类适配器输出结果
ConcreteTarget-----request()--------------------------Adapter-----request()Adaptee-----doSomeThing()
对象适配器
对象适配器是通过对对象关联的方法进行的适配
对象适配器uml图
对象适配器通用源码
Target 类:
public interface Target { public void request();}
ConcreteTarget 类:
public class ConcreteTarget implements Target { @Override public void request() { // TODO Auto-generated method stub System.out.println("ConcreteTarget-----request()"); }}
Adaptee01 类:
public class Adaptee01 { public void doAct01(){ System.out.println("Adaptee01--------------doAct01()"); }}
Adaptee02 类:
public class Adaptee02 { public void doAct02(){ System.out.println("Adaptee02--------------doAct02()"); }}
Adaptee03 类:
public class Adaptee03 { public void doAct03(){ System.out.println("Adaptee03--------------doAct03()"); }}
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.doAct01(); adaptee02.doAct02(); adaptee03.doAct03(); }}
Client 类:
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub Adaptee01 adaptee01 = new Adaptee01(); Adaptee02 adaptee02 = new Adaptee02(); Adaptee03 adaptee03 = new Adaptee03(); Adapter adapter = new Adapter(adaptee01, adaptee02, adaptee03); adapter.request(); }}
对象适配器输出结果
Adapter-----request()Adaptee01--------------doAct01()Adaptee02--------------doAct02()Adaptee03--------------doAct03()
Android源码中的模式实现
杂谈
适配器模式本质就是把现有的类,对象,方法来包装成我们所需要的类和对象。
适配器模式是结构类,而从结构形式来包装现有的类一般有二种方式:
- 一是继承的方式(is-a),这也就是类适配。
- 一是关联的方式(have-a),这也就是对象适配。
参考资料
(1).设计模式之禅—第19章 适配器模式
(2)装饰模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/adapter
- 设计模式之适配器模式(Adapter Pattern)
- 设计模式之适配器模式--- Pattern Adapter
- 适配器设计模式(Adapter Pattern)
- 设计模式笔记之适配器模式(Adapter Pattern)
- Php设计模式之【适配器模式 Adapter Pattern】
- .Net设计模式之适配器模式(Adapter Pattern)
- 设计模式之六:适配器模式(Adapter Pattern)
- JAVA设计模式之 适配器模式【Adapter Pattern】
- Java设计模式之适配器模式(Adapter Pattern)
- C#设计模式之适配器模式(Adapter Pattern)
- 设计模式(结构型)之适配器模式(Adapter Pattern)
- 设计模式之适配器模式(Adapter Pattern)C++实现
- 思维盛宴之设计模式-适配器模式Adapter Pattern
- 《设计模式解析》读书笔记之Adapter Pattern(适配器模式)
- 设计模式总结之Adapter Pattern(适配器模式)
- Net设计模式实例之适配器模式(Adapter Pattern)
- 设计模式拾荒之适配器模式(Adapter Pattern)
- 23种设计模式之-----适配器模式(Adapter Pattern)
- 【LIS】HDU1257-最少拦截系统
- BeatMap
- Eclipse中导入xsd和dtd文件,使编辑XML时自动提示
- iOS退出应用代码
- ORACLE数据库测试数据插入速度
- 设计模式之适配器模式--- Pattern Adapter
- JQuery倒计时跳转
- Linux vi 中移动光标 命令
- LuCI 页面修改
- 孙子兵法与企业经营2
- 两个DIV紧靠居中
- 微信接口
- Eclipse的Servers视图中无法添加Tomcat6/Tomcat7
- DBCP数据库连接池的使用