Gof23-Adapter模式
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口(API)功能。它位于实际情况与需求之间,填补两者之间的差异。
在程序世界中,经常会存在现有的程序无法直接使用,需要做适当的变换之后才能使用的情况。这种用于填补“现有的程序”和“所需的程序”之间差异的设计模式就是 Adapter 模式。Adapter 模式也被称为 Wrapper 模式。Wrapper 有“包装器”的意思,就像用精美的包装纸将普通商品包装成礼物那样,替我们把某样东西包起来,使其能够用于其他用途的东西就被称为“包装器”或是“适配器”。
Adapter模式有以下两种。
- 类适配器模式(使用继承的适配器)
- 对象适配器模式(使用委托的适配器)【推荐】
意图:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。
何时使用:
1、系统需要使用现有的类,而此类的接口不符合系统的需要。
2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。
3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)
如何解决:继承或依赖(推荐)。
关键代码:适配器继承或依赖已有的对象,实现想要的目标接口。
应用实例:
1、美国电器 110V,中国 220V,就要有一个适配器将 110V 转化为 220V。
2、JAVA JDK 1.1 提供了 Enumeration 接口,而在 1.2 中提供了 Iterator 接口,想要使用 1.2 的 JDK,则要将以前系统的 Enumeration 接口转化为 Iterator 接口,这时就需要适配器模式。
3、在 LINUX 上运行 WINDOWS 程序。
4、JAVA 中的 jdbc。
优点:1、可以让任何两个没有关联的类一起运行。 2、提高了类的复用。 3、增加了类的透明度。 4、灵活性好。
缺点:1、过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。
使用场景:有动机地修改一个正常运行的系统的接口,这时应该考虑使用适配器模式。
注意事项:适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。
角色:
(1)Target(对象):负责定义所需的方法,需求
(2)Client(请求者):
(3)Adaptee(被适配):
(4)Adapter(适配器):
实现
场景分析:如果想让额定工作电压是直流12伏特的笔记本电脑在交流220V伏特的AC电源下工作应该怎么做呢?通常,我们会使用 AC 适配器,将家庭用的交流220V伏特电压转换成我们所需要的直流12伏特电压。这就是适配器的工作。
继承适配器:
/**
* @program: interview
* @description: 实际情况家庭电流220V(被适配)
* @author:
* @create: 2023-03-26 22:57
**/
public class HouseholdCurrent {
private String current;
public HouseholdCurrent(String current) {
this.current = current;
}
public void output220VCurrent() {
System.out.println(current);
}
}
/**
* 电脑需要的电流12V(Target,目标需求)
*/
public interface ComputerCurrent {
public abstract void output12VCurrent();
}
/**
* @program: interview
* @description: Ac电源适配器(适配器)
* @author:
* @create: 2023-03-26 23:01
**/
public class AcAdapter extends HouseholdCurrent implements ComputerCurrent{
public AcAdapter(String current) {
super(current);
}
@Override
public void output12VCurrent() {
output220VCurrent();
System.out.println("适配电源至12V");
}
}
/**
* @program: interview
* @description: 电脑(请求者)
* @author:
* @create: 2023-03-26 23:05
**/
public class Computer {
public static void main(String[] args) {
ComputerCurrent acAdapter = new AcAdapter("220V");
acAdapter.output12VCurrent();
}
}
UML类图:
委托适配器:
/**
* @program: interview
* @description: 家庭电流(Adaptee被适配对象)
* @author:
* @create: 2023-03-27 10:48
**/
public class HouseholdCurrent {
private String current;
public HouseholdCurrent(String current) {
this.current = current;
}
public void output220VCurrent() {
System.out.println(this.current);
}
}
/**
* @program: interview
* @description: 电脑电流(Target需求)
* @author:
* @create: 2023-03-27 10:51
**/
public abstract class ComputerCurrent {
public abstract void output12VCurrent();
}
/**
* @program: interview
* @description: AC适配器(Adapter适配器)
* @author:
* @create: 2023-03-27 10:52
**/
public class AcAdapter extends ComputerCurrent{
private HouseholdCurrent householdCurrent;
public AcAdapter(String current) {
this.householdCurrent = new HouseholdCurrent(current);
}
@Override
public void output12VCurrent() {
householdCurrent.output220VCurrent();
System.out.println("适配至12V");
}
}
/**
* @program: interview
* @description: 电脑(Client请求者)
* @author:
* @create: 2023-03-27 10:53
**/
public class Computer {
public static void main(String[] args) {
ComputerCurrent acAdapter = new AcAdapter("220V");
acAdapter.output12VCurrent();
}
}
UML类图:
JDBC实例:
UML类图:
笔者学习设计模式的记录与心得。
搜狐畅游公司福利 1337人发布

查看11道真题和解析