目录
在现实生活中,经常出现两个对象因接口不兼容而不能在一起工作的实例,这时需要第三者进行适配。例如,讲中文的人同讲英文的人对话时需要一个翻译,用直流电的笔记本电脑接交流电源时需要一个电源适配器,用计算机访问照相机的 SD 内存卡时需要一个读卡器等。
在软件设计中也可能出现:需要开发的具有某种业务功能的组件在现有的组件库中已经存在,但它们与当前系统的接口规范不兼容,如果重新开发这些组件成本又很高,这时用适配器模式能很好地解决这些问题。
1. 概念
适配器模式(Adapter)定义:将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的哪些类能一起工作。适配器模式分为类适配器模式和对象适配器模式,前者由继承实现,后者由组合实现。
优点:
- 能提高类的透明性和复用性,现有类复用但不需要改变;
- 目标类和适配器类解耦,提高程序扩展性;
- 符合开闭原则。
缺点:
- 适配器编写过程需要全面考虑,可能会增加系统的复杂性;
- 增加系统代码可读的难度。
使用场景
- 已经存在的类,他的方法和需求不匹配;
- 不是软件设计阶段考虑的设计模式,是随着软件维护,由于不同产品,不同厂家造成功能类似而接口不相同情况下的解决方案。
2.demo
类的适配器模式可以采用多重继承的方式实现,但是由于 Java 不支持多继承,因此可以定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中以ing存在的组件。
对象的适配器模式可以采用将已有组件库中已实现的组件引入适配器中,该类同时实现当前系统的业务接口。
模式的结构和主要角色如下:
- 目标接口(Target):当前系统业务所期待的接口,可以是抽象类或是接口;
- 适配者类(Adaptee):是被访问和等待适配的现存组件库中的组件接口;
- 适配器类(Adapter):是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。
package com.example.myalgorithm.designPattern;
import java.util.*;
/**
* 适配器模式
* */
public class A10Adapter {
public static class ESSearchClient{
private void search(Integer userId){
System.out.println("通过ES检索用户的搜索行为");
}
}
public static class MustEatService{
private void loadShopInfo(List shopIds){
System.out.println("通过必吃榜服务查询想要的东西");
}
}
public static class UAService{
private void query(Integer userId, Integer actionId){
System.out.println("通过行为中心查询用户30的行为");
}
}
public static class UserContext{
private int userId;
private int actionId;
public UserContext(int userId, int actionId) {
this.userId = userId;
this.actionId = actionId;
}
public int getUserId() {
return userId;
}
public int getActionId() {
return actionId;
}
}
public interface UserActionCount{
void execute(UserContext userContext);
}
public static class SearchAction implements UserActionCount{
private ESSearchClient esSearchClient;
public SearchAction() {
this.esSearchClient = new ESSearchClient();
}
@Override
public void execute(UserContext userContext) {
this.esSearchClient.search(userContext.getUserId());
System.out.println("通过ES和浏览商户行为统计30天内的用户"+userContext.getUserId()+"的检索行为");
}
}
public static class MustEatAction implements UserActionCount{
private MustEatService mustEatService;
public MustEatAction() {
this.mustEatService = new MustEatService();
}
@Override
public void execute(UserContext userContext) {
this.mustEatService.loadShopInfo(Collections.emptyList());
System.out.println("通过必吃榜服务统计30天内用户"+userContext.getUserId()+"的必吃榜签到行为");
}
}
public static class UserAction implements UserActionCount{
private UAService uaService;
public UserAction() {
this.uaService = new UAService();
}
@Override
public void execute(UserContext userContext) {
this.uaService.query((userContext.getUserId()),userContext.getActionId());
System.out.println("通过行为中心统计30天内的用户"+userContext.getUserId()+"的"+userContext.getActionId()+"行为");
}
}
public static class ActionCountDispatch{
Map<Integer,UserActionCount> actionCountMap;
public ActionCountDispatch() {
this.actionCountMap = new HashMap<>();
this.actionCountMap.put(31,new SearchAction());
this.actionCountMap.put(32,new MustEatAction());
this.actionCountMap.put(1,new UserAction());
this.actionCountMap.put(2,new UserAction());
this.actionCountMap.put(3,new UserAction());
this.actionCountMap.put(4,new UserAction());
}
public void getActionCount(UserContext userContext){
if (actionCountMap.keySet().contains(userContext.getActionId())) {
actionCountMap.get(userContext.getActionId()).execute(userContext);
}else{
System.out.println("行为不在统计范围内:" + userContext.getActionId());
}
}
}
public static class Adapter{
public static void main(String[] args) {
ActionCountDispatch actionCountDispatch = new ActionCountDispatch();
UserContext user1 = new UserContext(10000,1);
actionCountDispatch.getActionCount(user1);
UserContext user2 = new UserContext(10000,3);
actionCountDispatch.getActionCount(user2);
UserContext user3 = new UserContext(10000,5);
actionCountDispatch.getActionCount(user3);
UserContext user4 = new UserContext(10000,32);
actionCountDispatch.getActionCount(user4);
}
}
}
3. 模式在开源软件中的应用
3.1 org.springframework.web.*.HandlerAdapter 类
public interface HandlerAdapter {
boolean supports(Object handler);
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
long getLastModified(HttpServletRequest request, Object handler);
}
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return ((Controller) handler).handleRequest(request, response);
}
@Override
public long getLastModified(HttpServletRequest request, Object handler) {
if (handler instanceof LastModified) {
return ((LastModified) handler).getLastModified(request);
}
return -1L;
}
}
public class HttpRequestHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof HttpRequestHandler);
}
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
((HttpRequestHandler) handler).handleRequest(request, response);
return null;
}
@Override
public long getLastModified(HttpServletRequest request, Object handler) {
if (handler instanceof LastModified) {
return ((LastModified) handler).getLastModified(request);
}
return -1L;
}
}