当前位置:网站首页>适配器模式
适配器模式
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、如果您需要复用这样一些类, 他们处于同一个继承体系, 并且他们又有了额外的一些共同的方法, 但是这些共同的方法不是所有在这一继承体系中的子类所具有的共性。
你可以扩展每个子类, 将缺少的功能添加到新的子类中。 但是, 你必须在所有新子类中重复添加这些代码, 这样会使得代码有坏味道。
将缺失功能添加到一个适配器类中是一种优雅得多的解决方案。 然后你可以将缺少功能的对象和功能封装在适配器中,为这个继承体系添加适配器对象, 从而动态地获取所需功能。如要这一点正常运作, 目标类必须要有通用接口, 适配器的成员变量应当遵循该通用接口。 这种方式同装饰模式非常相似。
与其他设计模式的关系
桥接模式通常会于开发前期进行设计, 使你能够将程序的各个部分独立开来以便开发。 另一方面, 适配器模式通常在已有程序中使用, 让相互不兼容的类能很好地合作。
适配器可以对已有对象的接口进行修改, 装饰模式则能在不改变对象接口的前提下强化对象功能。 此外, 装饰还支持递归组合, 适配器则无法实现。
适配器能为被封装对象提供不同的接口, 代理模式能为对象提供相同的接口, 装饰则能为对象提供加强的接口。
外观模式为现有对象定义了一个新接口, 适配器则会试图运用已有的接口。 适配器通常只封装一个对象, 外观通常会作用于整个对象子系统上。
桥接、 状态模式和策略模式 (在某种程度上包括适配器) 模式的接口非常相似。 实际上, 它们都基于组合模式——即将工作委派给其他对象, 不过也各自解决了不同的问题。 模式并不只是以特定方式组织代码的配方, 你还可以使用它们来和其他开发者讨论模式所解决的问题。
边栏推荐
- Serious hazard warning! Log4j execution vulnerability is exposed!
- There are applications related to web network request API in MATLAB (under update)
- The difference between get and post in small interview questions
- 基于SDN的DDoS攻击缓解
- Daily production training report (16)
- cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
- Daily production training report (15)
- Leetcode513.找出树的左下角的值
- Official image acceleration
- Ad tutorial series | 4 - creating an integration library file
猜你喜欢

Leetcode114. 二叉树展开为链表

Using Jenkins to perform testng+selenium+jsup automated tests and generate extendreport test reports

Experience of reading the road to wealth and freedom

cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
Posting - don't get lost in the ocean of Technology

The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.

Recursively traverse directory structure and tree presentation

uniCloud云开发获取小程序用户openid

There are applications related to web network request API in MATLAB (under update)

uni-app吸顶固定样式
随机推荐
循环位移
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
【ARM】讯为rk3568开发板buildroot添加桌面应用
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
Pre-Sale Analysis
Redis usage and memory optimization
Cyclic displacement
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
redis探索之布隆过滤器
Daily production training report (17)
cartographer_ local_ trajectory_ builder_ 2d
Life is so fragile
9 common classes
skimage. morphology. medial_ axis
A love that never leaves
Thoughts triggered by the fact that app applications are installed on mobile phones and do not display icons
【MYSQL】MySQL 百万级数据量分页查询方法及其优化
Installation and deployment of alluxio
Introduction to GUI programming to game practice (II)
Summary of the 10th provincial Blue Bridge Cup