适配器模式(Adapter)
来源:互联网 发布:网易云音乐网络电视版 编辑:程序博客网 时间:2024/06/10 18:31
Java适配器模式是为了解决合作开发中,当接口方法与实现方法不太匹配时(譬如方法名字不相同),提供的中间件来匹配接口与实现。
接口IStudy中提供了一个write方法,但是在StudyImpl类中有和write相同的功能的一个方法,但是名字是xie,所以我们需要一个适配器来将两者关联起来
package com.vincent.designpatterns.adapter;/** * Created by vincent on 14-10-21. */public interface IStudy { void write();}
package com.vincent.designpatterns.adapter;/** * Created by vincent on 14-10-21. */public class StudyImpl { public void xie(){ System.out.println("学习写字"); }}
package com.vincent.designpatterns.adapter;/** * Created by vincent on 14-10-21. */public class StudyAdapter extends StudyImpl implements IStudy{ @Override public void write() { this.xie(); }}
package com.vincent.designpatterns.adapter;/** * Created by vincent on 14-10-21. */public class Main { public static void main(String[] args) { IStudy study=new StudyAdapter(); study.write(); }}
最后输出结果是:学习写字
通过适配器,接口和方法关联起来了
0 0
- Adapter模式 适配器模式
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器(Adapter)模式
- 适配器模式(Adapter Pattern)
- ADAPTER(适配器)模式
- 适配器模式(adapter)
- Adapter适配器模式
- 适配器(Adapter)模式
- 适配器模式(Adapter Pattern)
- Adapter(适配器)模式
- 适配器模式(adapter)
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器模式 Adapter Pattern
- Adapter 适配器模式
- 适配器模式(Adapter)
- ACM-容斥原理
- 《数据结构》实验一:VC编程环境灵活应用
- 入职三个月总结
- Java.Sql.Sqlexception: Io Exception: The Network Adapter Could Not Establish The Connection
- ACM-抽屉原理
- 适配器模式(Adapter)
- django-groundwork在django1.6中正确的用法
- 秋意浪漫风景如画 诗情画意 谁能读懂一个浪子的心
- 详解Linux内核之双向循环链表
- 【内核周报-2014年10月9日】 批量网络包传输
- Mac OS X 中快速访问系统根目录的四种方法
- 从 Qt 的 delete 说开来
- 网络编程-了解概念
- STM32经验