适配器模式,从名字就可以很好的理解该模式。 我们平时使用的电子产品的适配器,就是将我们的生活电压220v 转换为电子产品使用的5V电压。 原本220V电压的生活用电是没有办法作用在电子产品上的,可是我们增加了一个适配器后就可以完美的解决了这个问题。
适配器模式(Adapter Pattern )就是把一个类的接口变换成用户期待的另一种接口,从而使原本因接口不匹配无法在一起工作的两个类可以在一起工作。
适配器模式根据适配的类型可以分为接口适配和类与对象适配。
接口适配: Target 是一个接口,Adaptee 是以及已经存在的类 Adapter 则需要实现Target接口同时需要继续已经存在的类。
类与对象适配:通过继承Target类,利用现有的类满足所需要的方法。
/**
* Apatee ,需要被适配的接口
* 生活电压接口
* @author zhangwei
* @version $Id: LivingElectric.java, v 0.1 2014年9月23日 下午10:23:55 zhangwei Exp $
*/
public interface LivingElectric {
/**
* 返回生活用电的电压
*
* @return
*/
public int getLivingElectric();
}
/**
* Target: 这个是笔记本期待的电压接口
* 笔记本的电压接口
* @author zhangwei
* @version $Id: NoteElectric.java, v 0.1 2014年9月23日 下午10:26:44 zhangwei Exp $
*/
public interface NoteElectric {
/**
* 获取笔记本的电压
*
* @return
*/
public int getNoteElectric();
}
/**
*Adapter 将生活电压转换为笔记本需要的电压
* @author Lenovo
* @version $Id: ElectricAdapter.java, v 0.1 2014年9月23日 下午10:29:16 Lenovo Exp $
*/
public class ElectricAdapter implements NoteElectric {
private LivingElectric livingElectric;
/**
* @see com.cathy.demo.pattern.adapter.NoteElectric#getNoteElectric()
*/
public int getNoteElectric() {
return livingElectric.getLivingElectric() / 40;
}
public ElectricAdapter(LivingElectric livingElectric) {
super();
this.livingElectric = livingElectric;
}
}
/**
* 笔记本类
* @author Lenovo
* @version $Id: Note.java, v 0.1 2014年9月23日 下午10:29:34 Lenovo Exp $
*/
public class Note {
NoteElectric power;
public void setPower(NoteElectric noteElectric) {
power = noteElectric;
}
public void use() {
if (power.getNoteElectric() > 7 || power.getNoteElectric() <= 0) {
throw new IllegalArgumentException("电压不匹配,无法工作");
}
System.out.println(MessageFormat.format("当前电压{0}V-符合工作条件开始工作", power.getNoteElectric()));
}
}
/**
*
* @author zhangwei
* @version $Id: Client.java, v 0.1 2014年9月23日 下午10:33:13 Lenovo Exp $
*/
public class Client {
public static void main(String[] args) {
Note note = new Note();
// 连接电源
note.setPower(new ElectricAdapter(new LivingElectric() {
/**
* 返回生活电压220V
* @see com.cathy.demo.pattern.adapter.LivingElectric#getLivingElectric()
*/
public int getLivingElectric() {
return 220;
}
}));
// 使用笔记本
note.use();
}
}
运行的结果是:当前电压5V-符合工作条件开始工作
/**
*
* @author zhangwei
* @version $Id: ChinaLivingElectirc.java, v 0.1 2014年9月24日 上午9:03:03 Lenovo Exp $
*/
public class ChinaLivingElectirc implements LivingElectric {
/**
* @see com.cathy.demo.pattern.adapter.LivingElectric#getLivingElectric()
*/
public int getLivingElectric() {
return 220;
}
}
/**
*
* @author Lenovo
* @version $Id: Adapter.java, v 0.1 2014年9月24日 上午9:02:10 Lenovo Exp $
*/
public class Adapter extends ChinaLivingElectirc implements NoteElectric {
/**
* @see com.cathy.demo.pattern.adapter.NoteElectric#getNoteElectric()
*/
public int getNoteElectric() {
return getLivingElectric() / 40;
}
}
/**
*
* @author zhangwei
* @version $Id: Client.java, v 0.1 2014年9月23日 下午10:33:13 Lenovo Exp $
*/
public class Client {
public static void main(String[] args) {
//
Note note = new Note();
// 连接适配器
note.setPower(new Adapter());
// 开始工作
note.use();
}
}