当前位置:网站首页>适配器模式
适配器模式
2022-06-26 05:36:00 【绿毛水怪12138】
什么时适配器模式
适配器模式(Adapter):将一个类的接口转换成客户端希望的另外一个接口。Adapter模式使得原本由于接口不兼容不能一起工作的那些类可以一起工作。
UML
适配器模式分为对象适配器和类适配器,对象适配器通过组成聚合实现适配功能,类适配器通过继承目标类和工作类实现功能。
示例性代码
package adapter;
public class test {
public static void main(String[] args) {
Target a=new Adapter();
a.request();
}
}
package adapter;
public abstract class Target {
public abstract void request();
}
package adapter;
public class Adapter extends Target{
private Adaptee adaptee;
public Adapter() {
// TODO Auto-generated constructor stub
adaptee=new Adaptee();
}
@Override
public void request() {
// TODO Auto-generated method stub
adaptee.request();
}
}
package adapter;
public class Adaptee {
public void request(){
System.out.println("真正的请求!");
}
}
使用场景
1、当你希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用适配器类。
适配器模式允许你创建一个中间层类, 其可作为代码与遗留类、 第三方类或提供怪异接口的类之间的转换器。
2、如果您需要复用这样一些类, 他们处于同一个继承体系, 并且他们又有了额外的一些共同的方法, 但是这些共同的方法不是所有在这一继承体系中的子类所具有的共性。
你可以扩展每个子类, 将缺少的功能添加到新的子类中。 但是, 你必须在所有新子类中重复添加这些代码, 这样会使得代码有坏味道。
将缺失功能添加到一个适配器类中是一种优雅得多的解决方案。 然后你可以将缺少功能的对象和功能封装在适配器中,为这个继承体系添加适配器对象, 从而动态地获取所需功能。如要这一点正常运作, 目标类必须要有通用接口, 适配器的成员变量应当遵循该通用接口。 这种方式同装饰模式非常相似。
与其他设计模式的关系
桥接模式通常会于开发前期进行设计, 使你能够将程序的各个部分独立开来以便开发。 另一方面, 适配器模式通常在已有程序中使用, 让相互不兼容的类能很好地合作。
适配器可以对已有对象的接口进行修改, 装饰模式则能在不改变对象接口的前提下强化对象功能。 此外, 装饰还支持递归组合, 适配器则无法实现。
适配器能为被封装对象提供不同的接口, 代理模式能为对象提供相同的接口, 装饰则能为对象提供加强的接口。
外观模式为现有对象定义了一个新接口, 适配器则会试图运用已有的接口。 适配器通常只封装一个对象, 外观通常会作用于整个对象子系统上。
桥接、 状态模式和策略模式 (在某种程度上包括适配器) 模式的接口非常相似。 实际上, 它们都基于组合模式——即将工作委派给其他对象, 不过也各自解决了不同的问题。 模式并不只是以特定方式组织代码的配方, 你还可以使用它们来和其他开发者讨论模式所解决的问题。
边栏推荐
- vscode config
- 二次bootloader关于boot28.asm应用的注意事项,28035的
- Lesson 4 serial port and clock
- MySQL数据库-01数据库概述
- A new journey
- Feelings of virtual project failure
- Ribbon负载均衡服务调用
- Setting pseudo static under fastadmin Apache
- [activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
- cross entropy loss = log softmax + nll loss
猜你喜欢
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
MySQL数据库-01数据库概述
【C语言】深度剖析数据在内存中的存储
小小面试题之GET和POST的区别
cartographer_ local_ trajectory_ builder_ 2d
When was the autowiredannotationbeanpostprocessor instantiated?
redis探索之布隆过滤器
[arm] add desktop application for buildreoot of rk3568 development board
基于SDN的DDoS攻击缓解
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
随机推荐
PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
Leetcode114. 二叉树展开为链表
The difference between get and post in small interview questions
RIA ideas
[arm] build boa based embedded web server on nuc977
data = self._ data_ queue. get(timeout=timeout)
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
Source code of findcontrol
Command line interface of alluxio
Written before father's Day
Henkel database custom operator '~~‘
AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
Could not get unknown property ‘*‘ for SigningConfig container of type org. gradle. api. internal
使用Jenkins执行TestNg+Selenium+Jsoup自动化测试和生成ExtentReport测试报告
Talk 5 wireless communication
Win socket programming (Mengxin initial battle)
【PHP】PHP二维数组按照多个字段进行排序
Fedora alicloud source
Install the tp6.0 framework under windows, picture and text. Thinkphp6.0 installation tutorial
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week