简介
在设计模式中,适配器模式有时候也称包装样式或者包装。将一个类的接口转接成用户所期待的。一个适配使得因接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。
分类
适配器分为,类适配器、对象适配、接口适配方式
类适配器方式采用继承方式,对象适配方式使用构造函数传递
适配器案例
我们就拿日本电饭煲的例子进行说明,日本电饭煲电源接口标准是110V电压,而中国标准电压接口是220V,所以要想在中国用日本电饭煲,需要一个电源转换器
日本110V电源接口
public interface JP110VInterface {
public void connect();
}
日本110V电源实现
public class JP110VInterfaceImpl implements JP110VInterface {
@Override
public void connect() {
System.out.println("日本110V,接通电源,开始工作..");
}
}
中国220V电源接口和实现
public interface CN220VInterface {
public void connect();
}
public class CN220VInterfaceImpl implements CN220VInterface {
@Override
public void connect() {
System.out.println("中国220V,接通电源,开始工作");
}
}
定义一个电压适配器
要想在中国使用日本电饭煲,需要把电饭煲110v的电源接口适配成我们220V的电源接口,这就需要一个电源适配器
public class ElectricCooker {
// 日本电饭煲
private JP110VInterface jp110VInterface;
ElectricCooker(JP110VInterface jp110VInterface){
this.jp110VInterface=jp110VInterface;
}
public void cook(){
jp110VInterface.connect();
System.out.println("开始做饭了..");
}
}
定义一个电压适配器,实现110V接口,使这个适配器可以作为参数传给电饭煲。但是在connect里去执行220V接口的逻辑
public class PowerAdaptor implements JP110VInterface {
private CN220VInterface cn220VInterface;
public PowerAdaptor(CN220VInterface cn220VInterface) {
this.cn220VInterface = cn220VInterface;
}
@Override
public void connect() {
cn220VInterface.connect();
}
}
测试运行
public class AdaptorTest {
public static void main(String[] args) {
CN220VInterface cn220VInterface = new CN220VInterfaceImpl();
PowerAdaptor powerAdaptor = new PowerAdaptor(cn220VInterface);
// 电饭煲
ElectricCooker cooker = new ElectricCooker(powerAdaptor);
cooker.cook();//使用了适配器,在220V的环境可以工作了。
}
}
适配器应用场景
我们根据上面的适配器的特点的介绍中,我们来分析下适配器模式的几类比较适用的使用场景:
1、我们在使用第三方的类库,或者说第三方的API的时候,我们通过适配器转换来满足现有系统的使用需求。
2、我们的旧系统与新系统进行集成的时候,我们发现旧系统的数据无法满足新系统的需求,那么这个时候,我们可能需要适配器,完成调用需求。
3、我们在使用不同数据库之间进行数据同步。(我这里只是分析的是通过程序来说实现的时候的情况。还有其他的很多种方式[数据库同步])。
BufferedWriter:是Writer的子类,将输出的字节流变为字符流。
BufferedReader:是Reader的子类,将输入的字节流变为字符流。
下面以BufferedReader作为案例,对适配器模式思想进行分析。
Reader类的核心代码
public abstract class Reader implements Readable, Closeable {
protected Object lock;
protected Reader() {
this.lock = this;
}
protected Reader(Object lock) {
if (lock == null) {
throw new NullPointerException();
}
this.lock = lock;
}
/**
* 读取字节流
*/
public int read() throws IOException {
char cb[] = new char[1];
if (read(cb, 0, 1) == -1)
return -1;
else
return cb[0];
}
}
BufferedReader核心代码
public class BufferedReader extends Reader {
private Reader in;
/**
* 构造中传入一个Reader,BufferedReader作为Reader的适配器
*/
public BufferedReader(Reader in) {
this(in, defaultCharBufferSize);
}
/**
* 校验适配器中是否有Reader
*/
private void ensureOpen() throws IOException {
if (in == null)
throw new IOException("Stream closed");
}
/**
* 适配器中的方法,读取字符流
*/
public int read() throws IOException {
synchronized (lock) {
ensureOpen();
for (;;) {
if (nextChar >= nChars) {
fill();
if (nextChar >= nChars)
return -1;
}
if (skipLF) {
skipLF = false;
if (cb[nextChar] == '\n') {
nextChar++;
continue;
}
}
return cb[nextChar++];
}
}
}
/**
* 按行读取
*/
String readLine(boolean ignoreLF) throws IOException {
StringBuffer s = null;
int startChar;
synchronized (lock) {
ensureOpen();
boolean omitLF = ignoreLF || skipLF;
bufferLoop:
for (;;) {
if (nextChar >= nChars)
fill();
if (nextChar >= nChars) {
if (s != null && s.length() > 0)
return s.toString();
else
return null;
}
boolean eol = false;
char c = 0;
int i;
if (omitLF && (cb[nextChar] == '\n'))
nextChar++;
skipLF = false;
omitLF = false;
charLoop:
for (i = nextChar; i < nChars; i++) {
c = cb[i];
if ((c == '\n') || (c == '\r')) {
eol = true;
break charLoop;
}
}
startChar = nextChar;
nextChar = i;
if (eol) {
String str;
if (s == null) {
str = new String(cb, startChar, i - startChar);
} else {
s.append(cb, startChar, i - startChar);
str = s.toString();
}
nextChar++;
if (c == '\r') {
skipLF = true;
}
return str;
}
if (s == null)
s = new StringBuffer(defaultExpectedLineLength);
s.append(cb, startChar, i - startChar);
}
}
}
/**
* 关流方法,本质上是关闭Reader的流
* 因为BufferedReader仅仅是适配器,本身并不是IO流,所以不存在关流的说法
* 因此这里的关流方法是把Reader关闭
*/
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
try {
in.close();
} finally {
in = null;
cb = null;
}
}
}
}