1.概念:
把一个类的接口变换成客户端所希望的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
(就是将两个类的方法组合起来,使其可以在一个类中使用!)
2.两种形式
a.类的适配器模式 b.对象的适配器模式
http://www.iteye.com/topic/339198
http://www.iteye.com/topic/74417
1.概念:
适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
2.两种形式
a.类的适配器模式 b.对象的适配器模式
3.模拟问题:
现在假设我们的程序已经设计了接口Request接口,但是现在有一个特殊的接口SpecificRequst能更好的完成我们的功能,但是它和我们现有的Request接口不匹配。那我们如何将它们一起工作呢?看下面的实例:
3.实例1:a.类的适配器模式 (继承类、实现接口)
目标角色:
- public interface Target {
- public void request();
- }
源角色:
- public class Adaptee {
- public void specificRequest(){
- System.out.println("实现所需功能");
- }
- }
适配器角色:
- public class ClassAdapter extends Adaptee implements Target {
- public void request() {
- this.specificRequest();
- }
- }
用户角色:
- public class TestClassAdapter {
- public static void main(String args[]){
- ClassAdapter adapter = new ClassAdapter();
- adapter.request();
- }
- }
3.图示实例2:b.对象的适配器模式
实例代码:
目标角色,源角色代码不变。
适配器角色:
- public class ObjectAdapter implements Target {
- private Adaptee adaptee;
- public ObjectAdapter(Adaptee adaptee){
- this.adaptee = adaptee;
- }
- public void request() {
- adaptee.specificRequest();
- }
- }
用户角色:
- public class TestOjbectAdapter {
- public static void main(String arg[]){
- Adaptee adaptee = new Adaptee();
- ObjectAdapter adapter = new ObjectAdapter(adaptee);
- adapter.request();
- }
- }
相关推荐
设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式...
java 汽车适配器(Adapter适配器模式) java 汽车适配器(Adapter适配器模式)
设计模式C++学习之适配器模式(Adapter)
基于类的Adapter模式的一般结构如下:Adaptee类为Adapter的父类,Adaptee类为适配源,适配目标(接口)也是Adapter的父类;基于类的Adapter模式比较适合应用于Adapter想修改Adaptee的部分方法的情况。 基于对象的...
走进设计模式之——适配器模式 (Adapter) 对应代码
介绍24设计模式中的Adapter模式,如果使用,实例讲解,等等
适配器模式(Adapter) demo
C#设计模式之适配器模式应用实例
适配器Adapter模式——日志记录程序,这里写的是类适配器和对象适配器,这里用的是c#的语言写的。
适配器模式(Adapter Pattern) 是作为两个不兼容接口之间的桥梁, 这种类型的设计模式属于结构型模式。 一些书籍也称适配器模式为缺省适配器模式(Default Adapter Pattern) 。 适配器模式主要分 为三类: 类...
java中的适配器模式,Adapter设计模式(包装器)23种设计模式之一
设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适配器模式 设计模式 Adapter 还请各位大S指教
javascript设计模式之Adapter模式【适配器模式】实现方法示例.docx
NULL 博文链接:https://toyota2006.iteye.com/blog/553012
NULL 博文链接:https://wy649898543.iteye.com/blog/1432031
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第7节:结构型模式Adapter 适配器模式
java设计模式之适配器模式,希望对大家有所帮助。
Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现