`
iamxi
  • 浏览: 188695 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

学习:适配器(Adapter)

阅读更多

如果你知道电源适配器的作用,就应该很容易理解这个模式。

 

意图:

将一个类的接口转换成客户希望的另外一个接口。 Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

 

别名:

包装器 Wrapper

 

结构:

类适配器

public interface Target {

	public void request();
}

public class Adaptee {

	public void specificRequest() {
		//do something
	}
}

//类适配器
public class Adapter extends Adaptee implements Target {

	@Override
	public void request() {
		super.specificRequest();
	}

}

 

对象适配器


//对象适配器
public class Adapter implements Target {

	private Adaptee adaptee = new Adaptee();
	
	@Override
	public void request() {
		adaptee.specificRequest();
	}

}
 

 

适用:
以下情况使用Adapter模式
• 你想使用一个已经存在的类,而它的接口不符合你的需求。
• 你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
• (仅适用于对象Adapter)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。

 

双向适配器:

一般适配器对使用target接口的透明,无法给使用adaptee接口的使用。双向适配器可以解决这个问题,使适配器更透明。

 

相关模式:

     模式Bridge的结构与对象适配器类似,但是Bridge模式的出发点不同:Bridge目的是将接口部分和实现部分分离,从而对它们可以较为容易也相对独立的加以改变。而Adapter则意味着改变一个已有对象的接口。
     Decorator模式增强了其他对象的功能而同时又不改变它的接口。因此Decorator对应用程序的透明性比适配器要好。结果是Decorator支持递归组合,而纯粹使用适配器是不可能实现这一点的。
     模式Proxy在不改变它的接口的条件下,为另一个对象定义了一个代理。

  • 大小: 12.7 KB
  • 大小: 13.2 KB
分享到:
评论

相关推荐

    设计模式C++学习之适配器模式(Adapter)

    设计模式C++学习之适配器模式(Adapter)

    设计模式学习之适配器模式

    和RecyclerView的Adapter就是典型的适配器模式,当我们在开发时,碰到要在两个完全没有关系的类之间进行交互,第一个解决方案是修改各自类的接口,但是如果无法修改源代码或者其他原因导致无法更改接口,此时怎么办...

    react-adapter:React适配器

    这将使您可以访问组件以及与我们的适配器相同的基本功能。 FlatfileButton的用法 import { FlatfileButton } from '@flatfile/react' FlatfileButton道具 信息 例子 settings在这里您将传递。 **必需的。 **目的 ...

    Android 适配器——BaseAdapter

    这里面存放的是我博客中BaseAapter适配器的学习代码

    traveladapter:旅行适配器和插头数据库:electric_plug:

    开源旅行适配器数据库。 入门 TravelAdapter.info是一个简单的旅行适配器数据库,可提供有关世界各地特定区域的电源插座,插头类型和电压的详细信息。 该网站使用HTML和Bootstrap构建,因此任何人都可以贡献力量!...

    07-使用代理快速接入第三方库(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    08-责任链和管道的协作(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    05-容易被忽略的迭代器(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    06-让观察者监听事件(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    01-制造工具的工厂模式(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    09-通过容器实现的外观模式(2).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    04-通过策略选择驱动(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    02-控制反转和服务容器(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    11-回顾和总结(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    03-查询语句建造器(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    00-初探 Laravel 和其中的设计模式(3).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    12-附录 1 设计模式的七大原则(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    10-基于装饰器的日志写入器(1).html

    适配器模式( Adapter ) 装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ...

    acc的matlab代码-residual_adapters:剩余适配器

    2017:“使用残余适配器学习多个视觉域”, CVPR 2018:“多域深度神经网络的有效参数化”, 有关多域分类的关联的Visual Domain十项全能挑战的页面: 抽象的 深度神经网络的实际限制是它们对单个任务和视觉领域的...

Global site tag (gtag.js) - Google Analytics