一、

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. Android应用资源--之属性(Attribute)资源

    原文链接: http://wujiandong.iteye.com/blog/1184921 属性(Attribute)资源:属于整个Android应用资源的一部分.其实就是网上一堆介绍怎么给自定义V ...

  2. 3月31日学习笔记(CSS基础)

    背景属性 文本属性 direction 属性影响块级元素中文本的书写方向.表中列布局的方向.内容水平填充其元素框的方向.以及两端对齐元素中最后一行的位置. 注释:对于行内元素,只有当 unicode- ...

  3. easyui扩展-日期范围选择.

    参考: http://www.5imvc.com/Rep https://github.com/dangrossman/bootstrap-daterangepicker * 特性: * (1)基本功 ...

  4. 模板与继承之艺术——奇特的递归模板模式(CRTP)

    一.什么是CRTP 奇特的模板递归模式(Curiously Recurring Template Pattern)即将派生类本身作为模板参数传递给基类. template<typename T& ...

  5. 跟着PHP100第一季学写一个CMS(1-10)

    笔记: 这次用的方法是先跟着视频做一遍,隔一天或半天后独立再做一遍,能发现真正不会的地方记录下来. CMS0.1界面布局1.问题:分两个css来实现时basic.css+index.php出现定位不正 ...

  6. Django文档——Model字段选项(Field Options)

    建立一个简易Model class Person(models.Model): GENDER_CHOICES=( (1,'Male'), (2,'Female'), ) name=models.Cha ...

  7. s3c-u-boot-1.1.6源码分析

    源码 源码结构 移植准备

  8. Linux学习系列之Linux入门(一)linux安装与入门

    第一篇:安装并配置Linux开发环境 一.安装linux: 主要安装Linux的发行版,到目前为之,主要的发行版有: 比较常用的是Ubuntu.redhat和centOS,主要的安装方法详细: Ubu ...

  9. 网络编程Socket UDP

    图表流程 linux udp测试代码 //server.c #include <stdio.h> #include <stdlib.h> #include <errno. ...

  10. Servlet一次乱码排查后的总结(转)

    原文地址:http://my.oschina.net/looly/blog/287255 由来 在写一个小小的表单提交功能的时候,出现了乱码,很奇怪request上来的参数全部是乱码,而从数据库查询出 ...