---
layout: pattern
title: Adapter
folder: adapter
permalink: /patterns/adapter/
pumlid: DSR14S8m30J0Lg20M7-wEMnDOiPMFDA9j0yyUEtUkzMHJTF7xI1NF4GSLzaxZtncgDVJgCPIpobzv0N2vOKtjgRHTziMI7KBcOXl10thfxB-Nz9dMJd71m00
categories: Structural
tags:
- Java
- Gang Of Four
- Difficulty-Beginner
---
## Also known as
Wrapper
## Intent
Convert the interface of a class into another interface the clients
expect. Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
## Explanation
Real world example
> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
> Another example would be the famous power adapter; a three legged plug can't be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet.
> Yet another example would be a translator translating words spoken by one person to another
In plain words
> Adapter pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.
Wikipedia says
> In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
**Programmatic Example**
Consider a captain that can only use rowing boats and cannot sail at all.
First we have interfaces `RowingBoat` and `FishingBoat`
```
public interface RowingBoat {
void row();
}
public class FishingBoat {
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
public void sail() {
LOGGER.info("The fishing boat is sailing");
}
}
```
And captain expects an implementation of `RowingBoat` interface to be able to move
```
public class Captain implements RowingBoat {
private RowingBoat rowingBoat;
public Captain(RowingBoat rowingBoat) {
this.rowingBoat = rowingBoat;
}
@Override
public void row() {
rowingBoat.row();
}
}
```
Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
```
public class FishingBoatAdapter implements RowingBoat {
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoatAdapter.class);
private FishingBoat boat;
public FishingBoatAdapter() {
boat = new FishingBoat();
}
@Override
public void row() {
boat.sail();
}
}
```
And now the `Captain` can use the `FishingBoat` to escape the pirates.
```
Captain captain = new Captain(new FishingBoatAdapter());
captain.row();
```
## Applicability
Use the Adapter pattern when
* you want to use an existing class, and its interface does not match the one you need
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
## Consequences:
Class and object adapters have different trade-offs. A class adapter
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
* let’s Adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee.
* introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
An object adapter
* let’s a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
## Real world examples
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
* [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)
* [java.util.Collections#enumeration()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#enumeration-java.util.Collection-)
* [javax.xml.bind.annotation.adapters.XMLAdapter](http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#marshal-BoundType-)
## Credits
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
没有合适的资源?快使用搜索试试~ 我知道了~
设计模式实现(Java、C++、Golang).zip
共1663个文件
java:971个
xml:132个
png:111个
0 下载量 49 浏览量
2024-08-28
13:06:55
上传
评论
收藏 5.68MB ZIP 举报
温馨提示
项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全栈开发),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助 【资源内容】:项目具体内容可查看/点击本页面下方的*资源详情*,包含完整源码+工程文件+说明(若有)等。【若无VIP,此资源可私信获取】 【本人专注IT领域】:有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为您提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【适合场景】:相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可基于此项目来扩展开发出更多功能 #注 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担 2. 部分字体及插图等来自网络,若是侵权请联系删除,本人不对所涉及的版权问题或内容负法律责任。收取的费用仅用于整理和收集资料耗费时间的酬劳 3. 积分资源不提供使用问题指导/解答
资源推荐
资源详情
资源评论
收起资源包目录
设计模式实现(Java、C++、Golang).zip (1663个子文件)
Bass-Drum-1.aif 216KB
Closed-Hi-Hat-1.aif 13KB
Mediator.cpp 2KB
Observer.cpp 2KB
AbstractFactory.cpp 2KB
State.cpp 1KB
Flyweight.cpp 1KB
Builder.cpp 1KB
Visitor.cpp 1KB
Composite.cpp 1KB
handle.cpp 1KB
Iterator.cpp 1KB
Bridge.cpp 997B
main.cpp 988B
interpret.cpp 931B
Strategy.cpp 880B
memento.cpp 874B
Decorator.cpp 866B
Template.cpp 862B
ex02.cpp 811B
Prototype.cpp 702B
ex01.cpp 687B
Command.cpp 679B
Facade.cpp 621B
Proxy.cpp 601B
Source.cpp 529B
Source.cpp 505B
Adapter.cpp 471B
main.cpp 396B
AbstractFactoryMain.cpp 393B
main.cpp 348B
Source.cpp 336B
Source.cpp 329B
Source.cpp 317B
Singleton.cpp 316B
Source.cpp 309B
Source.cpp 283B
Source.cpp 263B
Source.cpp 261B
main.cpp 256B
Source.cpp 251B
Source.cpp 246B
Source.cpp 244B
Source.cpp 209B
Source.cpp 201B
main.cpp 200B
SingletonMain.cpp 181B
Source.cpp 172B
BuilderMain.cpp 172B
context.cpp 79B
album-list.css 3KB
style.css 2KB
application.css 827B
SimpleObjectSpec_listAllAndCreate.feature 1KB
queue-load-leveling.gif 15KB
spinning-icon.gif 5KB
.gitattributes 764B
.gitignore 392B
.gitignore 308B
.gitignore 190B
.gitignore 94B
.gitignore 18B
.gitignore 9B
.gitignore 9B
.gitignore 9B
.gitignore 9B
.gitignore 9B
.gitignore 9B
.gitignore 9B
.gitignore 9B
.gitignore 8B
main.go 1KB
main.go 906B
main.go 887B
main.go 756B
main.go 500B
sigslot.h 20KB
Mediator.h 1KB
AbstractFactory.h 1KB
Observer.h 1KB
Visitor.h 1KB
Builder.h 1KB
Iterator.h 970B
State.h 896B
Bridge.h 860B
memento.h 807B
interpret.h 781B
Composite.h 702B
Flyweight.h 702B
Decorator.h 628B
Template.h 624B
handle.h 606B
Strategy.h 585B
Command.h 514B
Facade.h 424B
Proxy.h 405B
Prototype.h 366B
Adapter.h 365B
Singleton.h 195B
context.h 125B
共 1663 条
- 1
- 2
- 3
- 4
- 5
- 6
- 17
资源评论
热爱技术。
- 粉丝: 2631
- 资源: 7860
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机视觉大作业Python基于tensorflow与CNN的花卉图像识别源码+实验报告
- 基于Vue实现的移动端手机商城项目 电商购物网站 成品源码 共20+页.zip
- Goc Chess资源文件(Goc Chess)
- CLShanYanSDKDataList.sqlite
- mmexport1732965153341.mp4
- 音效文件(Goc Chess)
- SPot-the-Difference Self-Supervised Pre-training for Anomaly Detection and Segmentation
- 计算机视觉大作业-卫星云层图像的理解与识别python源码+实验报告(高分项目)
- 英雄联盟云顶之弈双城之战2
- 8266 MSYS2 压缩包文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功