注释:以下资料都是个人从网上收集起来的,会注明来源地址
一、定义与介绍
1、定义
定义为将一个类的接口变换成客户端所期待的一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
2、对象适配器
6、说明
二、使用
1、如何使用?
实现Adapter方式,其实"think in Java"的"类再生"一节中已经提到,有两种方式:组合(composition)和继承(inheritance).
假设我们要打桩,有两种类:方形桩 圆形桩.
public class SquarePeg{ public void insert(String str){ System.out.println("SquarePeg insert():"+str); } } public class RoundPeg{ public void insertIntohole(String msg){ System.out.println("RoundPeg insertIntoHole():"+msg); } }
现在有一个应用,需要既打方形桩,又打圆形桩.那么我们需要将这两个没有关系的类综合应用.假设RoundPeg我们没有源代码,或源代码我们不想修改,那么我们使用Adapter来实现这个应用:
public class PegAdapter extends SquarePeg{ private RoundPeg roundPeg; public PegAdapter(RoundPeg peg)(this.roundPeg=peg;) public void insert(String str){super.insert(str); roundPeg.insertIntoHole(str);} }
在上面代码中,RoundPeg属于Adaptee,是被适配者.PegAdapter是Adapter,将Adaptee(被适配者RoundPeg)和Target(目标SquarePeg)进行适配.实际上这是将组合方法(composition)和继承(inheritance)方法综合运用.
PegAdapter首先继承SquarePeg,然后使用new的组合生成对象方式,生成RoundPeg的对象roundPeg,再重载父类insert()方法。从这里,你也了解使用new生成对象和使用extends继承生成对象的不同,前者无需对原来的类修改,甚至无需要知道其内部结构和源代码.
如果你有些Java使用的经验,已经发现,这种模式经常使用。
2、进一步使用
上面的PegAdapter是继承了SquarePeg,如果我们需要两边继承,即继承SquarePeg 又继承RoundPeg,因为Java中不允许多继承,但是我们可以实现(implements)两个接口(interface)
public interface IRoundPeg{ public void insertIntoHole(String msg); } public interface ISquarePeg{ public void insert(String str); }
下面是新的RoundPeg 和SquarePeg, 除了实现接口这一区别,和上面的没什么区别。
public class SquarePeg implements ISquarePeg{ public void insert(String str){ System.out.println("SquarePeg insert():"+str); } } public class RoundPeg implements IRoundPeg{ public void insertIntohole(String msg){ System.out.println("RoundPeg insertIntoHole():"+msg); } }
下面是新的PegAdapter,叫做two-way adapter:
public class PegAdapter implements IRoundPeg,ISquarePeg{ private RoundPeg roundPeg; private SquarePeg squarePeg; // 构造方法 public PegAdapter(RoundPeg peg){this.roundPeg=peg;} // 构造方法 public PegAdapter(SquarePeg peg)(this.squarePeg=peg;) public void insert(String str){ roundPeg.insertIntoHole(str);} }
还有一种叫Pluggable Adapters,可以动态的获取几个adapters中一个。使用Reflection技术,可以动态的发现类中的Public方法。
例子:
MapAdapter.java package com.hysec.utils.jaxb; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.xml.bind.annotation.adapters.XmlAdapter; public class MapAdapter extends XmlAdapter<MapEntity[], Map> { @Override public MapEntity[] marshal(Map map) throws Exception { // TODO Auto-generated method stub MapEntity[] list = new MapEntity[map.size()]; Set keyset = map.keySet(); int index =0; for(Iterator itor=keyset.iterator();itor.hasNext();){ MapEntity item = new MapEntity(); item.key = itor.next(); item.value = map.get(item.key); list[index++] = item; } return list; } @Override public Map unmarshal(MapEntity[] list) throws Exception { // TODO Auto-generated method stub Map map = new HashMap(); for(int i=0;i<list.length;i++){ MapEntity item = list[i]; map.put(item.key, item.value); } return map; } }
来源:《Adapter_百度百科》、《设计模式之Adapter(适配器)》、《CXF+JAXB处理复杂的数据之XMLAdapter》
相关推荐
设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式...
主要为大家详细介绍了Java设计模式之Adapter适配器模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
设计模式C++学习之适配器模式(Adapter)
设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
javascript设计模式之Adapter模式【适配器模式】实现方法示例.docx
java设计模式之适配器模式,希望对大家有所帮助。
java中的适配器模式,Adapter设计模式(包装器)23种设计模式之一
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第7节:结构型模式Adapter 适配器模式
走进设计模式之——适配器模式 (Adapter) 对应代码
C#面向对象设计模式纵横谈(7):Adapter 适配器模式(结构型模式) 体验课程
介绍24设计模式中的Adapter模式,如果使用,实例讲解,等等
C#面向对象设计模式 Adapter 适配器模式 视频讲座下载
Adapter适配器模式在JavaScript设计模式编程中的运用分析_.docx
适配器模式 设计模式 Adapter 还请各位大S指教
C#面向对象设计模式纵横谈(7):Adapter 适配器模式(结构型模式) (Level 300)
NULL 博文链接:https://lym6520.iteye.com/blog/713094
C#设计模式之适配器模式应用实例
适配器模式(Adapter Pattern) 是作为两个不兼容接口之间的桥梁, 这种类型的设计模式属于结构型模式。 一些书籍也称适配器模式为缺省适配器模式(Default Adapter Pattern) 。 适配器模式主要分 为三类: 类...
Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现