设计模式 - 适配器模式(adapter pattern) 详解
来源:互联网 发布:线性代数与矩阵论 编辑:程序博客网 时间:2024/04/30 22:59
适配器模式(adapter pattern) 详解
本文地址: http://blog.csdn.net/caroline_wendy
适配器模式(adapter pattern): 将一个类的接口, 转换成客户期望的另一个接口. 适配器让原本不兼容的类可以合作无间.
适配器模式(adapter pattern)主要包括:
1. 被适配者接口(adaptee interface): 需要被适配的接口.
2. 适配器(adapter): 转换的类;
3. 目标接口(target interface): 需要转换成为的接口.
4. 客户(client): 具体使用的类.
具体方法:
1. 被适配者(adaptee interface)接口, 和实现接口具体的类.
/** * @time 2014年6月17日 */package adapter;/** * @author C.L.Wang * */public interface Turkey {public void gobble();public void fly();}/** * @time 2014年6月17日 */package adapter;/** * @author C.L.Wang * */public class WildTurkey implements Turkey {/* (non-Javadoc) * @see adapter.Turkey#gobble() */@Overridepublic void gobble() {// TODO Auto-generated method stubSystem.out.println("Gobble gobble! ");}/* (non-Javadoc) * @see adapter.Turkey#fly() */@Overridepublic void fly() {// TODO Auto-generated method stubSystem.out.println("I'm flying a short distance! ");}}
2. 目标接口(target interface), 和实现接口具体的类.
/** * @time 2014年6月17日 */package adapter;/** * @author C.L.Wang * */public interface Duck {public void quack();public void fly();}/** * @time 2014年6月17日 */package adapter;/** * @author C.L.Wang * */public class MallardDuck implements Duck {/* (non-Javadoc) * @see adapter.Duck#quack() */@Overridepublic void quack() {// TODO Auto-generated method stubSystem.out.println("Quack! ");}/* (non-Javadoc) * @see adapter.Duck#fly() */@Overridepublic void fly() {// TODO Auto-generated method stubSystem.out.println("I'm flying! ");}}
3. 适配器(adapter): 被适配者接口(adaptee interface) 转换 目标接口(target interface).
组合被适配者类接口, 实现目标类接口.
/** * @time 2014年6月17日 */package adapter;/** * @author C.L.Wang * */public class TurkeyAdapter implements Duck {Turkey turkey;public TurkeyAdapter (Turkey turkey) {this.turkey = turkey;}/* (non-Javadoc) * @see adapter.Duck#quack() */@Overridepublic void quack() {// TODO Auto-generated method stubturkey.gobble();}/* (non-Javadoc) * @see adapter.Duck#fly() */@Overridepublic void fly() {// TODO Auto-generated method stubfor (int i=0; i<5; ++i) { //多次飞行turkey.fly(); }}}
4. 客户(client)方法需要使用目标类,
把被适配者(adaptee)的具体类, 通过适配器(adapter), 转换为目标(target)类, 进行调用:
/** * @time 2014年6月17日 */package adapter;/** * @author C.L.Wang * */public class DuckTestDrive {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMallardDuck duck = new MallardDuck();WildTurkey turkey = new WildTurkey();Duck turkeyAdapter = new TurkeyAdapter(turkey);System.out.println("The Turkey says ...");turkey.gobble();turkey.fly();System.out.println("\nThe Duck says ...");testDuck(duck);System.out.println("\nThe TurkeyAdapter says ...");testDuck(turkeyAdapter);}static void testDuck (Duck duck) {duck.quack();duck.fly();}}
5. 输出.
The Turkey says ...Gobble gobble! I'm flying a short distance! The Duck says ...Quack! I'm flying! The TurkeyAdapter says ...Gobble gobble! I'm flying a short distance! I'm flying a short distance! I'm flying a short distance! I'm flying a short distance! I'm flying a short distance!
4 0
- 设计模式 - 适配器模式(adapter pattern) 详解
- 适配器设计模式(Adapter Pattern)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式之适配器模式(Adapter Pattern)
- c++设计模式:适配器模式(Adapter Pattern)
- 设计模式-适配器模式(adapter pattern)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式--适配器模式【 Adapter Pattern】
- 设计模式之适配器模式--- Pattern Adapter
- Java设计模式--适配器模式【Adapter Pattern】
- 设计模式【适配器模式Adapter Pattern】
- 设计模式--适配器模式(Adapter Pattern)
- 【设计模式】适配器模式(Adapter Pattern)
- 适配器模式(Adapter Pattern)
- 适配器模式(Adapter Pattern)
- JavaScript实现简单图片滚动 --9张图告诉你,C罗欲哭无泪
- QT获取本机IP
- dmesg
- 黑马程序员_反射
- 动态规划解决0-1背包问题
- 设计模式 - 适配器模式(adapter pattern) 详解
- 程序员画画-小狐仙
- 软硬兼施的网站优化方式
- Java线程(一):线程安全与不安全
- aix系统使用定时任务每天备份清理nohup日志操作步骤
- Tomcat --> Cannot create a server using the selected type
- Oracle:列出当前用户下可见的所有表名及权限…
- Light OJ 1005 - Rooks 数学题解
- Linux编程---进程通信