GOF23 设计模式之 适配器模式
来源:互联网 发布:桌面软件开发架构 编辑:程序博客网 时间:2024/05/21 09:21
适配器模式
什么是适配器模式?
- 将一个类的接口转换为客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的两个类可以在一起工作
模式中的角色
- 目标接口(Target):客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。
- 需要适配的类(Adaptee):需要适配的类或适配者类。
- 适配器(Adapter):通过包装一个需要适配的对象,吧原接口转换成目标接口。
工作中的场景
- 经常用来做旧系统升级和改造
- 如果我们的系统开发之后再也不需要维护,那么很多模式都是没有必要的,但是不幸的是,事实却是维护一个系统的代价往往是开发一个系统的数倍。
适配器模式的例子
java.io.InputStreamReader(InputStream)
java.io.OutputStreamWriter(OutputStream)
代码示例
- Target接口
package com.coderbean.adapter;/** * Created by Chang on 15/9/24. */public interface Target { void handleReq();}
- Adapter
package com.coderbean.adapter;/** * Created by Chang on 15/9/24. * * 适配器(类适配器方式) */public class Adapter extends Adaptee implements Target { @Override public void handleReq(){ super.request(); }}
- Adaptee
package com.coderbean.adapter;/** * Created by Chang on 15/9/24. * 被适配的类,可以完成客户需要的功能 */public class Adaptee { public void request(){ System.out.println("可以完成客户请求的需要的功能!"); }}
- Client
package com.coderbean.adapter;/** * Created by Chang on 15/9/24. * * 客户端类 */public class Clinet { public void test1(Target t){ t.handleReq(); } public static void main(String[] args) { Clinet c = new Clinet(); Adaptee a = new Adaptee(); Target t = new Adapter2(a); t.handleReq(); }}
0 0
- GOF23 设计模式之 适配器模式
- GOF23设计模式之适配器模式的实现
- GOF23之适配器模式Demo
- 设计模式GOF23——适配器模式
- GoF23种设计模式之结构型模式之适配器模式
- GOF23设计模式之 单例模式
- GOF23设计模式之 工厂模式
- GOF23设计模式之 建造者模式
- GOF23 设计模式之 代理模式
- GOF23设计模式之 迭代器模式
- GOF23设计模式之 命令模式command
- java设计模式GOF23之创建者模式
- 【GOF23设计模式】_适配器模式、对象适配器、类适配器、开发中场景JAVA235
- GOF23种设计模式之Builder Pattern
- 设计模式之GOF23的Observer
- GOF23设计模式简介
- gof23设计模式图解
- GoF23种设计模式
- 第四周实训总结(实训周)
- 黑盒测试
- C# WindowsForm 员工管理系统四【发放工资】
- 第一次写博客
- 浏览器兼容、CSS3 选择器
- GOF23 设计模式之 适配器模式
- 【转载,细致】New与malloc的区别
- 软件测试
- Git学习笔记(一)
- 面试---内存偏移
- Datatables笔记
- java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
- 测试
- hdu5491 The Next(合肥网赛)