23种经典设计模式的java实现_2_适配器模式
来源:互联网 发布:石青软件被骗 编辑:程序博客网 时间:2024/04/30 07:01
适配器模式的适用:
你想使用一个已经存在的类,而它的接口不符合你的需求。
你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口,那么对象适配器可以适配它的父类接口(仅适用于对象Adapter)。
示例说明:
这个例子模拟了对一个特定组织系统——OrganizationAdaptee的适配。适配器OrganizationAdapter实现了适配接口IOrganization,从而使得客户端系统可以通过自己的接口IOrganization使用此组织系统——OrganizationAdaptee,而不需要耦合它的API。
package com.alex.designpattern.adapter;
import java.util.HashMap;
import java.util.Map;
/** *//**
* 适配器模式
* <p>
* 将一个类的接口转换成客户希望的另外一个接口。 <br>
* Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
*
* @author <a href="mailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public class Test ...{
public static void main(String[] args) ...{
Test test = new Test();
IOrganization org = new OrganizationAdapter();
String result = test.genericClientCode(org);
System.out.println("Using client, get result: " + result);
}
private String genericClientCode(IOrganization org) ...{
// We assume this function contains client-side code that only
// knows about IOrganization.
return org.getRole(4);
// other calls to IOrganization go here
// ...
}
}
interface IOrganization ...{
public String getRole(int id);
}
class OrganizationAdaptee ...{
public Map<String, String> queryRole(String id) ...{
System.out.println("Query a Role in the Adaptee Organization, id: "
+ id);
// query using id ... get result
Map<String, String> map = new HashMap<String, String>();
map.put("id", id);
map.put("name", "huqi");
map.put("type", "human");
return map;
}
}
class OrganizationAdapter implements IOrganization ...{
private OrganizationAdaptee adaptee = new OrganizationAdaptee();
public String getRole(int id) ...{
Map result = adaptee.queryRole(String.valueOf(id));
return result.get("id") + ", " + result.get("type") + ", "
+ result.get("name");
}
}
import java.util.HashMap;
import java.util.Map;
/** *//**
* 适配器模式
* <p>
* 将一个类的接口转换成客户希望的另外一个接口。 <br>
* Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
*
* @author <a href="mailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public class Test ...{
public static void main(String[] args) ...{
Test test = new Test();
IOrganization org = new OrganizationAdapter();
String result = test.genericClientCode(org);
System.out.println("Using client, get result: " + result);
}
private String genericClientCode(IOrganization org) ...{
// We assume this function contains client-side code that only
// knows about IOrganization.
return org.getRole(4);
// other calls to IOrganization go here
// ...
}
}
interface IOrganization ...{
public String getRole(int id);
}
class OrganizationAdaptee ...{
public Map<String, String> queryRole(String id) ...{
System.out.println("Query a Role in the Adaptee Organization, id: "
+ id);
// query using id ... get result
Map<String, String> map = new HashMap<String, String>();
map.put("id", id);
map.put("name", "huqi");
map.put("type", "human");
return map;
}
}
class OrganizationAdapter implements IOrganization ...{
private OrganizationAdaptee adaptee = new OrganizationAdaptee();
public String getRole(int id) ...{
Map result = adaptee.queryRole(String.valueOf(id));
return result.get("id") + ", " + result.get("type") + ", "
+ result.get("name");
}
}
- 23种经典设计模式的java实现_2_适配器模式
- JAVA的23种设计模式---适配器模式
- Java经典23种设计模式之结构型模式(三)------附代理模式、适配器模式、外观模式区别
- 设计模式之适配器模式Java实现
- Java实现设计模式之适配器模式
- 设计模式--适配器模式--Java实现
- 设计模式之适配器模式(三种实现) JAVA
- 设计模式_2_策略模式
- JAVA设计模式-类的适配器模式
- 设计模式C++实现_2_简单工厂模式
- 23种经典设计模式的java实现_1_抽象工厂模式
- 23种经典设计模式的java实现_3_桥接模式
- 23种经典设计模式的java实现_4_生成器模式
- 23种经典设计模式的java实现_5_职责链模式
- 23种经典设计模式的java实现之生成器模式
- 23种经典设计模式的java实现_5_职责链模式
- 设计模式之桥接模式+适配器模式Java实现
- Java 适配器设计模式
- 《OpenSSL与网络信息安全——密码算法实践》新书预告
- 通过GRUB指令来引导Windows
- 破解EXCEL工具区保护的宏【转】
- DataGrid中如何使用带参数Link
- 一个相机类的源代码
- 23种经典设计模式的java实现_2_适配器模式
- fork函數詳解
- 利用case在select语句里写条件判断
- struts标签logic:iterate学习
- select 函数 使用
- 35岁前成功的12条黄金法则
- 常用的正则表达式,你要的都有!
- 新闻点评:即时通讯不开放源码?
- wxPython的安装