适配器模式
来源:互联网 发布:手机淘宝怎么写追评 编辑:程序博客网 时间:2024/06/06 19:16
// 已存在的、具有特殊功能、但不符合我们既有的标准接口的类class Adaptee {public void specificRequest() {System.out.println("被适配类具有 特殊功能...");}}
// 目标接口,或称为标准接口interface Target {public void request();}// 具体目标类,只提供普通功能class ConcreteTarget implements Target {public void request() {System.out.println("普通类 具有 普通功能...");}}
// 适配器类,继承了被适配类,同时实现标准接口class Adapter extends Adaptee implements Target{public void request() {super.specificRequest();}}
// 测试类public class Client {public static void main(String[] args) {// 使用普通功能类Target concreteTarget = new ConcreteTarget();concreteTarget.request();// 使用特殊功能类,即适配类Target adapter = new Adapter();adapter.request();}}
普通类 具有 普通功能...被适配类具有 特殊功能...
// 适配器类,直接关联被适配类,同时实现标准接口class Adapter implements Target{// 直接关联被适配类private Adaptee adaptee;// 可以通过构造函数传入具体需要适配的被适配类对象public Adapter (Adaptee adaptee) {this.adaptee = adaptee;}public void request() {// 这里是使用委托的方式完成特殊功能this.adaptee.specificRequest();}}
// 测试类public class Client {public static void main(String[] args) {// 使用普通功能类Target concreteTarget = new ConcreteTarget();concreteTarget.request();// 使用特殊功能类,即适配类,// 需要先创建一个被适配类的对象作为参数Target adapter = new Adapter(new Adaptee());adapter.request();}}
0 0
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 可行性研究报告——机房收费系统
- Half year's footsteps
- CentOS虚拟机web服务器实现windows本地实时测试
- 面试题8:旋转数组的最小数字
- int * const p 与int const *p或者const int *p的区别
- 适配器模式
- 新的开始,新的目标-惠普天下
- 来到惠普的日子
- 看下效果
- 面试题9:斐波那契数列
- leetcode 021_merge two list
- testlink_在线考试系统
- 什么是大数据?
- Filter配置会话超时跳转页面