一、

1.

2.The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces

3.

4.The Adapter Pattern is full of good OO design principles: check out the use of object composition to wrap the adaptee with an altered interface. This approach has the added advantage that we can use an adapter with any subclass of the adaptee.

Also check out how the pattern binds the client to an interface, not an implementation; we could use several adapters, each converting a different backend set of classes. Or, we could add new implementations after the fact, as long as they
adhere to the Target interface.

5.

6.

二、duck假装是turkey,turkey假装是duck

1.

 package headfirst.designpatterns.adapter.ducks;

 public interface Duck {
public void quack();
public void fly();
}

2.

 package headfirst.designpatterns.adapter.ducks;

 public interface Turkey {
public void gobble();
public void fly();
}

3.

 package headfirst.designpatterns.adapter.ducks;

 public class MallardDuck implements Duck {
public void quack() {
System.out.println("Quack");
} public void fly() {
System.out.println("I'm flying");
}
}

4.

 package headfirst.designpatterns.adapter.ducks;

 public class WildTurkey implements Turkey {
public void gobble() {
System.out.println("Gobble gobble");
} public void fly() {
System.out.println("I'm flying a short distance");
}
}

5.

 package headfirst.designpatterns.adapter.ducks;
import java.util.Random; public class DuckAdapter implements Turkey {
Duck duck;
Random rand; public DuckAdapter(Duck duck) {
this.duck = duck;
rand = new Random();
} public void gobble() {
duck.quack();
} public void fly() {
if (rand.nextInt(5) == 0) {
duck.fly();
}
}
}

6.

 package headfirst.designpatterns.adapter.ducks;

 public class DuckTestDrive {
public static void main(String[] args) {
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();
}
}

7.

 package headfirst.designpatterns.adapter.ducks;

 public class TurkeyAdapter implements Duck {
Turkey turkey; public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
} public void quack() {
turkey.gobble();
} public void fly() {
for(int i=0; i < 5; i++) {
turkey.fly();
}
}
}

8.

 package headfirst.designpatterns.adapter.ducks;

 public class TurkeyTestDrive {
public static void main(String[] args) {
MallardDuck duck = new MallardDuck();
Turkey duckAdapter = new DuckAdapter(duck); for(int i=0;i<10;i++) {
System.out.println("The DuckAdapter says...");
duckAdapter.gobble();
duckAdapter.fly();
}
}
}

三、Java用Iterator取代Enumeration,处理旧代码可以用适配器模式

While Java has gone in the direction of the Iterator, there is nevertheless a lot of legacy client code that depends on the Enumeration interface, so an Adapter that converts an Iterator to an Enumeration is also quite useful.

1.

2.

 package headfirst.designpatterns.adapter.iterenum;

 import java.util.*;

 public class EnumerationIterator implements Iterator<Object> {
Enumeration<?> enumeration; public EnumerationIterator(Enumeration<?> enumeration) {
this.enumeration = enumeration;
} public boolean hasNext() {
return enumeration.hasMoreElements();
} public Object next() {
return enumeration.nextElement();
} public void remove() {
throw new UnsupportedOperationException();
}
}

3.

 package headfirst.designpatterns.adapter.iterenum;

 import java.util.*;

 public class IteratorEnumeration implements Enumeration<Object> {
Iterator<?> iterator; public IteratorEnumeration(Iterator<?> iterator) {
this.iterator = iterator;
} public boolean hasMoreElements() {
return iterator.hasNext();
} public Object nextElement() {
return iterator.next();
}
}

4.

 package headfirst.designpatterns.adapter.iterenum;

 import java.util.*;

 public class EnumerationIteratorTestDrive {
public static void main (String args[]) {
Vector<String> v = new Vector<String>(Arrays.asList(args));
Iterator<?> iterator = new EnumerationIterator(v.elements());
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

5.

 package headfirst.designpatterns.adapter.iterenum;

 import java.util.*;

 public class IteratorEnumerationTestDrive {
public static void main (String args[]) {
ArrayList<String> l = new ArrayList<String>(Arrays.asList(args));
Enumeration<?> enumeration = new IteratorEnumeration(l.iterator());
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}

6.

HeadFirst设计模式之适配器模式的更多相关文章

  1. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]

    适配器模式的主要意图是对现有类的接口进行转换,以满足目标类的需求.其次,可以给目标类的接口添加新的行为(主要指方法).这一点容易与装饰模式混淆.从意图方面来看,装饰模式不改变(通常指增加)接口中的行为 ...

  2. HeadFirst设计模式<2>

    HeadFirst设计模式<2> 1 装饰者模式 星巴克咖啡 饮料 总结 如果说策略模式是通过组合实现弹性,那么装饰者模式就是通过继承来实现,在实现的同时,客户基本感觉不到使用了装饰者模式 ...

  3. 每天一个设计模式-3 适配器模式(Adapteer)

    每天一个设计模式-3 适配器模式(Adapteer) 1.现实中的情况 旧式电脑的硬盘是串口的,直接与硬盘连接,新硬盘是并口的,显然新硬盘不能直接连在电脑上,于是就有了转接线.好了,今天的学习主题出来 ...

  4. Head First 设计模式之适配器模式与外观模式

    Head First设计模式之适配器模式与外观模式 前言: 之前讲过装饰者模式,将对象包装起来并赋予新的职责,这一章我们也会将对象进行包装,只不过是让它们看起来不像自己而像是别的东西.这样就可以在设计 ...

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

    一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...

  6. 【Head-First设计模式】C#版-学习笔记-开篇及文章目录

    原文地址:[Head-First设计模式]C#版-学习笔记-开篇及文章目录 最近一年断断续续的在看技术书,但是回想看的内容,就忘了书上讲的是什么东西了,为了记住那些看过的东西,最好的办法就是敲代码验证 ...

  7. 《HeadFirst设计模式》读后感——对学习设计模式的一些想法

    最近看完了<HeadFirst设计模式>,GOF的<设计模式——可复用面向对象软件的基础>的创建型模式也读完了,经历了从一无所知到茅塞顿开再到充满迷惑的过程. 不得不说< ...

  8. Headfirst设计模式的C++实现——策略模式(Strategy)

    前言 最近在学习<Headfirst设计模式>,里面的例子都是Java的.但是我对Java并不熟悉,所以试着用C++来实现书中的例子. 先来看看Duck以及子类 Duck.h #inclu ...

  9. Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)

    1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...

随机推荐

  1. (转)深入探讨在集群环境中使用 EhCache 缓存系统

    简介: EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider.本文充分的介绍了 EhCache 缓存系统对集群环境的 ...

  2. 总是你 2008-3 (献给L之一)

    文/安然 总是你 是梦里的那份最温柔 轻轻碰触 不再敢轻易提及 总是你 是不经意的那份最感动 点点鲜活 从来就没有淡去 时间远走 我轻轻的收起 那段记忆 不再开启 但是 偶尔 偶尔 总是 总是在不经意 ...

  3. Qt实现桌面动态背景雪花飘落程序

            曾经收到过一份礼物,一个雪花飘落的程序,觉得效果很炫,通过前几篇的学习,我们已经掌握了贴图的一些技巧了,那么现在就可以自己实现了(当然你必须先拥有qt信号与槽的基础知识),这里先看效果 ...

  4. DEDECMS中,常见全局变量

    全局变量 {dede:global.cfg_cmspath/} 是dedecms 的安装目录,一般就是网站的根目录. {dede:global.cfg_cmsurl/}是当前目录 注意加一根斜线{de ...

  5. hadoop2-shell操作详解

  6. grails的插件

    今天来歪理邪说一下grails的插件. 有个问题让本人困惑了一段时间,插件是属于grails的,还是属于某个工程的?为什么会有这个问题呢,这涉及到grails插件的安装方式. grails的插件像是一 ...

  7. Asp.net Response.Redirect with post data

    string url = String.Format("{0}://{1}/{2}", Request.Url.Scheme, Request.Url.Authority, &qu ...

  8. 写了个Linux包过滤防火墙

    花几天写了个so easy的Linux包过滤防火墙,估计实际意义不是很大.防火墙包括用户态执行程序和内核模块,内核模块完全可以用iptable代替.由于在编写的过程一开始写的是内核模块所以就直接用上来 ...

  9. linux 学习笔记2

    vi  编辑命令并查看 i 插入 esc  转换模式 shift + : x  保存并退出    q  不保存  !强制保存 五个查看命令 cat / less / more / tail / hea ...

  10. C# window service的创建

    其实我也是第一次在博客园写博客,看到那些高手说自己要多动手写博客,于是乎自己也尝试尝试. 废话不多说.这几天在研究window service,通过查找各种大神写的博客,我终于成功的自己写出来了. 下 ...