适配器设计模式(Adapter Pattern)
来源:互联网 发布:如何玩转linux 编辑:程序博客网 时间:2024/04/30 20:37
GOF设计模式
Adapter Pattern
适配器设计模式
适配器模式比喻
• 在朋友聚会上碰到了一个美女Sarah,从香港来的,可我不会说粤语,她不会说普通话,只好求助于我的朋友kent了,他作为我和Sarah之间的Adapter,让我和Sarah可以相互交谈了(也不知道他会不会耍我)• 适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。
1.类的适配器
public interface Telephone { public void tel();}
public class Socket { public static void charge(){ System.out.println("我能充电!"); }}
public class MyAdapter extends Socket implements Telephone { @Override public void tel() { super.charge(); }}
public class Test { public static void main(String[] args) { MyAdapter a=new MyAdapter(); a.tel(); }}
2.对象的适配器
public interface Telephone2 { public void tel2();}
public class Socket2 { public static void charge2(){ System.out.println("我能充电!"); }}
public class MyAdapter2 implements Telephone2{ private Socket2 socket2=new Socket2(); @Override public void tel2() { socket2.charge2(); }}
public class Test2 { public static void main(String[] args) { MyAdapter2 a=new MyAdapter2(); a.tel2(); }}
0 0
- 适配器设计模式(Adapter Pattern)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式之适配器模式(Adapter Pattern)
- c++设计模式:适配器模式(Adapter Pattern)
- 设计模式 - 适配器模式(adapter pattern) 详解
- 设计模式-适配器模式(adapter pattern)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式--适配器模式【 Adapter Pattern】
- 设计模式之适配器模式--- Pattern Adapter
- Java设计模式--适配器模式【Adapter Pattern】
- 设计模式【适配器模式Adapter Pattern】
- 设计模式--适配器模式(Adapter Pattern)
- 【设计模式】适配器模式(Adapter Pattern)
- 适配器模式(Adapter Pattern)
- 适配器模式(Adapter Pattern)
- FastDFS搭建
- OSI模型;TCP/IP
- noi-7904-陶陶摘苹果
- Android实战——jsoup实现网络爬虫,糗事百科项目的起步
- eclipse 安装反编译工具
- 适配器设计模式(Adapter Pattern)
- Oracle数据库
- Ajax三级联动下拉选择省市区
- 【SocketIO】笔记
- maven+springmvc+spring+mybatis+velocity整合
- Aspect-level Sentiment Classification using attention mechanism
- 把幻灯片PPT转换为安卓APK
- 使用密钥认证机制远程登录linux
- JVM性能调优监控工具总结