设计模式-23-1-类适配器
来源:互联网 发布:解救吾先生知乎 编辑:程序博客网 时间:2024/06/03 13:53
类适配器:
服务端中的类ExistingClass中有方法usefulMethod(),他能满足客户端的需求a
而客户端端中有接口RequiredInterface定义了方法requiredMethod()满足需求a
那么我们可以利用”适配器模式”
创建一个类NewClass继承类ExistingClass,那么就有usefulMethod()方法
并实现接口RequiredInterface,并在满足需求a的方法requiredMethod()调用
usefulMethod()实现完美的对接
这种通过子类进行适配,叫做“类适配”,通过继承类来获取功能
代码:
package com.structuralPattern.adapter.edition1;public class ExistingClass { public void usefulMethod(){ System.out.println("ExistingClass类中的实际执行的代码"); }}
package com.structuralPattern.adapter.edition1;public class NewClass extends ExistingClass implements RequiredInterface{ public static void main(String[] args) { NewClass nc = new NewClass(); nc.requiredMethod(); } @Override public void requiredMethod() { usefulMethod(); }}
package com.structuralPattern.adapter.edition1;public interface RequiredInterface { void requiredMethod();}
阅读全文
0 0
- 设计模式-23-1-类适配器
- 设计模式-适配器模式(1)
- 设计模式之适配器(1)
- [设计模式](五):适配器模式(Adapter)[类适配器、对象适配器、接口适配器]
- 23种设计模式之适配器模式
- 23种设计模式--适配器模式
- 设计模式(23):适配器模式Adapter
- 23种设计模式05---适配器模式
- 23种设计模式之适配器模式
- 23种设计模式(6)-适配器模式
- c++设计模式--类适配器模式
- JAVA设计模式-类的适配器模式
- 设计模式1-适配器模式(Adapter)
- 设计模式:适配器模式
- 设计模式------适配器模式
- 设计模式:适配器模式
- 设计模式---适配器模式
- 设计模式 适配器模式
- 华为机试——句子逆序
- AJAX简介
- 第一章人工智能之机器学习算法体系汇总
- Timer类jdk文档翻译及源码分析
- 虚拟机安装步骤
- 设计模式-23-1-类适配器
- hdu1162
- 协调作用域不同的Bean
- HDU
- 暑期学习记录08
- CentOS 6.6下安装Jenkins和Maven以及私服nexus的步骤
- 【Javascript笔记】1.3基本语法
- hdu 2072 单词数 字典树
- Java 8新特性学习