将一个类的接口转换为客户希望的另外一个接口。包括类适配器和对象适配器。
//类适配器
public class ClassAdapter {
interface Target{
public abstract void targetMethod();
}
class Adaptee{
public void adapteeMethod(){
System.out.println("adapteeMethod...");
}
}
class Adapter extends Adaptee implements Target{
public void targetMethod() {
super.adapteeMethod();
}
}
public static void main(String[] args){
ClassAdapter ca = new ClassAdapter();
Target t = ca.new Adapter();
t.targetMethod();
}
}
//对象适配器
public class ObjectAdapter {
interface Target{
public abstract void targetMethod();
}
class Adaptee{
public void adapteeMethod(){
System.out.println("adapteeMethod...");
}
}
class Adapter implements Target{
private Adaptee adaptee;
public Adapter(Adaptee adaptee){
this.adaptee = adaptee;
}
public void targetMethod() {
adaptee.adapteeMethod();
}
}
public static void main(String[] args){
ObjectAdapter ca = new ObjectAdapter();
Adaptee adptee = ca.new Adaptee();
Target t = ca.new Adapter(adptee);
t.targetMethod();
}
}
分享到:
相关推荐
设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式...
NULL 博文链接:https://wy649898543.iteye.com/blog/1432031
设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
java设计模式之适配器模式,希望对大家有所帮助。
介绍24设计模式中的Adapter模式,如果使用,实例讲解,等等
设计模式C++学习之适配器模式(Adapter)
适配器模式 设计模式 Adapter 还请各位大S指教
C#设计模式之适配器模式应用实例
javascript设计模式之Adapter模式【适配器模式】实现方法示例.docx
java中的适配器模式,Adapter设计模式(包装器)23种设计模式之一
主要为大家详细介绍了C++设计模式之适配器模式Adapter,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://lizhao6210-126-com.iteye.com/blog/1753041
主要介绍了Android设计模式之适配器(Adapter)模式,以源码解析的方式分析适配器模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了java设计模式之适配器模式Adapter的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
用c++方法实现AdapterPattern模式
主要介绍了Java设计模式之适配器模式(Adapter模式)介绍,本文讲解了为何使用适配器模式、如何使用适配器模式等内容,需要的朋友可以参考下
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要资源分。敬请期待。 这是第7节:结构型...
Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现
NULL 博文链接:https://jacky-dai.iteye.com/blog/1929106