定義:
將兩個不相容的類別糾合在一起使用,屬於結構型模式,需要有Adaptee(被適配者)和Adaptor(適配器)兩個身份。
爲何使用?
我們經常碰到要將兩個沒有關係的類別組合在一起使用,第一解決方案是:修改各自類別的介面,但是如果我們沒有源代碼,或者,我們不願意爲了一個應用而修改各自的介面。 怎麽辦?
使用Adapter,在這兩種介面之間創建一個混合介面(混血兒)。
如何使用?
實現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){ roundPeg.insertIntoHole(str);}
}
在上面代碼中,RoundPeg屬於Adaptee,是被適配者。PegAdapter是Adapter,將Adaptee(被適配者RoundPeg)和Target(目標SquarePeg)進行適配。實際上這是將組合方法(composition)和繼承(inheritance)方法綜合運用。
PegAdapter首先繼承SquarePeg,然後使用new的組合生成物件方式,生成RoundPeg的物件roundPeg,再重載父類別insert()方法。從這裏,你也瞭解使用new生成物件和使用extends繼承生成物件的不同,前者無需對原來的類別修改,甚至無需要知道其內部結構和源代碼。
如果你有些Java使用的經驗,已經發現,這種模式經常使用。
進一步使用
上面的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 IRoundPeg{
public void insert(String str){
System.out.println("SquarePeg insert():"+str);
}
}
public class RoundPeg implements ISquarePeg{
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方法。
分享到:
相关推荐
设计模式之 适配器 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++实现