一、

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. SpringMVC接收多个对象

    问题背景: 要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:将Json对象序列化成Json字符串提 ...

  2. c#跨窗体调用操作

    我知道的常用的有三种,以前记录的笔记: 1.通过构造函数实现 在form1的load事件中new form2时  在构造函数里添加一个参数 此参数就是form1类型的参数,同时记得在form2里重写构 ...

  3. mysql安装启动教程(两种方法)

    mysql安装启动: 方法一(简单版): cmd进入mysql安装的bin目录:mysqld.exe –install net start mysql  服务启动(或者选择计算机->(右键)管理 ...

  4. 【Winform】 Enum逆向解析

    将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象.   名称 说明   Parse(Type, String) 将一个或多个枚举常数 ...

  5. NGINX+UWSGI 莫名发生Nginx 502 Bad Gateway错误的排查过程

    自己有个阿里云UBUNTU运行的Django站,使用NGINX+UWSGI驱动,今天登陆系统后台更新内容出现了几个大字:Nginx 502 Bad Gateway,一看情况不好,这是要糟糕啊. 啊西八 ...

  6. php错误消息捕获

    <?php header('Content-type:text/html;charset=UTF-8'); //function_exists('ini_set') && ini ...

  7. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  8. PHP 对数组数值进行排序,使用另一个容器

    <?php /* 排序方式::事实上只需要将要循环的数组进行N次循环,然后每次取最大的一个值*/ $array = array(100,25,10,258,33,48,10,5,13,58,33 ...

  9. Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异

    我还是不发表到博客园首页吧,要不然还是要被取消,>_< 还是言归正传吧,关于Ext.Ajax.request()方法和FormPanel.getForm().submit()方法返回suc ...

  10. 过长文字自动换行的技巧 Word-Break Word-Wrap

    在很多时候,为了防止内容过长把表格或容器撑破, 我们都需要为容器加上自动换行的功能. 实现自动换行,用CSS来实现,通常有两种方式: word-break: 取值为 normal, break-all ...