一、

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. 1.6建造者模式(生成器模式) Builder

    1.概念:将一个复杂对象的构建和他的表示分离,使得同样的构件可以创建不同的表示. 2.实例:肯德基和中餐,肯德基抽象了整个做菜的复杂过程(相同的构建),然后在不同的店铺进行实现(不同的表示).中餐往往 ...

  2. 2016年11月ACM/ICPC亚洲区北京赛赛后总结

    2016年11月12到11月13为期两天的比赛,这是我们这个对第一次去打亚洲区域赛,经过这次比赛,我认识到了自己与别人的差距,也许我们与别人的起点不同,但这不是理由. 这次的比赛12号的热身赛两点开始 ...

  3. Java使用泛型类来提高方法的可重用性

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3832268.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  4. L009-oldboy-mysql-dba-lesson09

    L009-oldboy-mysql-dba-lesson09 mysql> grant replication salve,replication client on *.* to ‘repl_ ...

  5. NPOI Excel导入 导出

    添加引用 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Gene ...

  6. xml直接读取节点

    <root> <books> <book id="one"></book> <book id="two"& ...

  7. laravel扩展Debugbar

    github地址:https://github.com/barryvdh/laravel-debugbar

  8. maven项目转eclipse工程的命令:eclipse.bat

    call mvn clean:clean call mvn eclipse:eclipse -DdownloadSources=true @pause 复制以上内容,保存为eclipse.bat 以后 ...

  9. (转载)SQL语句,纵列转横列

    SQL语句,纵列转横列 Feed: 大富翁笔记 Title: SQL语句,纵列转横列 Author: wzmbox Comments sTable.db库位 货物编号 库存数1 0101 501 01 ...

  10. android.support.v4.widget.DrawerLayout使用

    activity_main.xml布局如下: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas ...