适配器模式(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()
*/
@Override
public void gobble() {
// TODO Auto-generated method stub
System.out.println("Gobble gobble! ");
} /* (non-Javadoc)
* @see adapter.Turkey#fly()
*/
@Override
public void fly() {
// TODO Auto-generated method stub
System.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()
*/
@Override
public void quack() {
// TODO Auto-generated method stub
System.out.println("Quack! ");
} /* (non-Javadoc)
* @see adapter.Duck#fly()
*/
@Override
public void fly() {
// TODO Auto-generated method stub
System.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()
*/
@Override
public void quack() {
// TODO Auto-generated method stub
turkey.gobble();
} /* (non-Javadoc)
* @see adapter.Duck#fly()
*/
@Override
public void fly() {
// TODO Auto-generated method stub
for (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 stub
MallardDuck 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!

设计模式 - 适配器模式(adapter pattern) 具体解释的更多相关文章

  1. 设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释

    适配器模式(adapter pattern) 枚举器和迭代器 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考适配器模式(adapter patter ...

  2. C#设计模式——适配器模式(Adapter Pattern)

    一.概述在软件开发中,常常会想要复用一个已经存在的组件,但该组件的接口却与我们的需要不相符,这时我们可以创建一个适配器,在需复用的组件的接口和我们需要的接口间进行转换,从而能够正常的使用需复用的组件. ...

  3. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  4. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  5. 设计模式系列之适配器模式(Adapter Pattern)——不兼容结构的协调

    模式概述 模式定义 模式结构图 模式伪代码 类适配器,双向适配器,缺省适配器 类适配器 双向适配器 缺省适配器 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 主要缺点 适 ...

  6. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  7. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  8. 设计模式(七): 通过转接头来观察"适配器模式"(Adapter Pattern)

    在前面一篇博客中介绍了“命令模式”(Command Pattern),今天博客的主题是“适配器模式”(Adapter Pattern).适配器模式用处还是比较多的,如果你对“适配器模式”理解呢,那么自 ...

  9. 适配器模式(Adapter Pattern)--设计模式

    在生活中,想用苹果充电线给安卓的手机充电时,因为两者的接口不一样,会导致充电口无法进行匹配, 这时候,就需要适配器,将安卓的充电口转化为苹果的接口,这样就可以充电啦.已有的类与新的接口不兼容问题是很普 ...

随机推荐

  1. scanf函数和printf函数

    C程序实现输出和输入的 主要是printf函数 和 scanf函数,这两个函数是格式输入输出 格式声明由%和格式字符组成 如%d,%f 格式字符:  d格式符:用来输出一个有符号的十进制整数  c格式 ...

  2. java——String的那边破事

    经典的先看下面一段代码,请问最终创建几个对象,分别在哪里? String s0 = new String("luoliang.me"); String s1 = "luo ...

  3. 常用的Linux操作命令(一)

    ls 目录 mkdir 创建文件夹 vi 新建文件 :w filename 将文章存入指定的文件名filename :wq 保存并退出编辑文件 :q! 强制离开并放弃编辑的文件 cd 切换到目录/ro ...

  4. hadoop搭建杂记:Linux下不同linux主机之间文件copy的scp命令

    不同的Linux之间copy文件常用有3种方法: 不同的Linux之间copy文件常用有3种方法: ①ftp 就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的程序来进行文件 ...

  5. JavaScriptの例

    Dateのオブジェクト: <html> <head> <title>Date Object Example</title> </head> ...

  6. codeforces 589F. Gourmet and Banquet 二分+网络流

    题目链接 给你n种菜, 每一种可以开始吃的时间不一样, 结束的时间也不一样. 求每种菜吃的时间都相同的最大的时间.时间的范围是0-10000. 看到这个题明显可以想到网络流, 但是时间的范围明显不允许 ...

  7. vcredist作用

    一.vcredist作用: vcredist_x86.exe是微软公司Visual C++的32位运行时库,包含了一些Visual C++的库函数. vcredist_x64.exe是微软公司Visu ...

  8. QPointer很大程度上避免了野指针(使用if语句判断即可,类似于dynamic_cast),而且使用非常方便 good

    QPointer 如何翻译呢?我不太清楚,保留英文吧. The QPointer class is a template class that provides guarded pointers    ...

  9. PHP并发最佳实践

    直接参考大牛的: http://www.searchtb.com/2012/06/rolling-curl-best-practices.html

  10. Installing perl and writing your first perl program in Ubuntu

    Installing perl and writing your first perl program in Ubuntu     Installing perl and writing your f ...