1.6、适配器模式
来源:互联网 发布:照片视频制作软件下载 编辑:程序博客网 时间:2024/06/06 02:47
Java与模式:适配器模式
目的:
将一个类的接口转换成客户希望的另外一个接口。A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
意图:
(目的和意图是不变的,摘自《设计模式迷你手册》电子版)
下面是一个Java版的示例
一、原理图:
二、示例代码
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 14:43:46
* 源角色
*/
public class Adaptee {
public int get220v(){
return 220;
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 14:43:46
* 源角色
*/
public class Adaptee {
public int get220v(){
return 220;
}
}
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 14:43:23
* 目标角色
*/
public interface Target {
int get110v();
int get220v();
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 14:43:23
* 目标角色
*/
public interface Target {
int get110v();
int get220v();
}
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 14:43:07
* 适配器角色:扩展源角色,实现目标角色,从而使得目标角色改动时候,不用改动源角色,只要改动适配器
*/
public class Adapterextends Adaptee implements Target{
public int get110v(){
return 110;
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 14:43:07
* 适配器角色:扩展源角色,实现目标角色,从而使得目标角色改动时候,不用改动源角色,只要改动适配器
*/
public class Adapterextends Adaptee implements Target{
public int get110v(){
return 110;
}
}
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 15:00:31
* 客户端
*/
public class Client {
public staticvoid main(String rags[]) {
new Client().test();
}
public void test() {
Target target = new Adapter();
int v1 = target.get110v();
int v2 = target.get220v();
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-2 15:00:31
* 客户端
*/
public class Client {
public staticvoid main(String rags[]) {
new Client().test();
}
public void test() {
Target target = new Adapter();
int v1 = target.get110v();
int v2 = target.get220v();
}
}
适配器通过扩展源角色,同时实现目标接口,从而满足了同时提供220v,110v电压的要求。
- 1.6、适配器模式
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- Lotus domino/design的公式秘籍
- 关于实现(大)系统的一些小体会
- 关于内联函数
- 页面设计中a标签内嵌套div标签的高度问题
- 12.3.1 用迭代器转换序列
- 1.6、适配器模式
- boost_1.47在VS2010下的安装
- VC 2010 发布
- 1.7、缺省适配器模式
- android入门
- Linux 下定时器的实现方式分析
- JSP中MVC模式的Demo
- 比较全的Domino常用服务器命令(包括邮件替换)
- 使用QtSdk为symbian操作系统编写程序