日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java描述设计模式(07):适配器模式

發布時間:2025/3/17 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java描述设计模式(07):适配器模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、適配器模式簡介

1、基礎概念

適配器模式把一個類的接口變換成客戶端所期待的另一種接口,從而使原本因接口不匹配而無法在一起工作的兩個類能夠在一起工作。適配器模式有類適配器模式和對象適配器模式,以及缺省(接口)適配器,三種不同的形式。

2、生活場景

基于適配器模式,把220V的電壓,轉換為需要的110V電壓。

public class C01_InScene {public static void main(String[] args) {CurrentAdapter adapter = new CurrentAdapter() ;System.out.println(adapter.get110VCurrent()) ;} } // 220V電流 class Current220V {public int get220VCurrent (){return 220 ;} } // 110V電流接口 interface Current110V {int get110VCurrent () ; } // 電流適配器 class CurrentAdapter extends Current220V implements Current110V {// 電流轉換方法@Overridepublic int get110VCurrent() {int high = get220VCurrent() ;int low = high/2 ;return low ;} }

二、類適配器

1、模式簡介

類的適配器模式把適配的類的API轉換成為目標類的API。

2、核心角色

  • 目標(Target)角色

這就是所期待得到的接口。

  • 源(Adapee)角色:

現在需要適配的接口。

  • 適配器(Adaper)角色

適配器類是本模式的核心。適配器把源接口轉換成目標接口。

3、源碼實現

interface Target {void sampleOperation1();void sampleOperation2(); } class Adaptee {public void sampleOperation1(){System.out.println("Adaptee.sampleOperation1()");} } class Adapter extends Adaptee implements Target{@Overridepublic void sampleOperation2() {System.out.println("Adapter.sampleOperation2()");} }

三、對象適配器

1、模式簡介

與類的適配器模式一樣,對象的適配器模式把被適配的類的API轉換成為目標類的API,與類的適配器模式不同的是,對象的適配器模式不是使用繼承關系連接到Adaptee類,而是使用委派關系連接到Adaptee類。

2、源碼實現

interface Target1 {void sampleOperation1();void sampleOperation2(); } class Adaptee1 {public void sampleOperation1(){System.out.println("Adaptee.sampleOperation1()");} } class Adapter1 implements Target1 {private Adaptee1 adaptee ;public Adapter1 (Adaptee1 adaptee){this.adaptee = adaptee;}public void sampleOperation1() {this.adaptee.sampleOperation1();}@Overridepublic void sampleOperation2() {System.out.println("Adapter.sampleOperation2()");} }

四、接口適配器

1、模式簡介

缺省(接口)適配(Default Adapter)模式為一個接口提供缺省實現,這樣子類型可以從這個缺省實現進行擴展,而不必從原有接口進行擴展。

2、源代碼實現

public class C04_AdapterInte {public static void main(String[] args) {ServiceAdapter adapter = new ServiceAdapter(){@Overridepublic int serviceOperation2() {return 22 ;}};System.out.println(adapter.serviceOperation2());} } interface AbstractService {void serviceOperation1();int serviceOperation2();String serviceOperation3(); } class ServiceAdapter implements AbstractService{@Overridepublic void serviceOperation1() {}@Overridepublic int serviceOperation2() {return 0;}@Overridepublic String serviceOperation3() {return null;} }

五、Spring框架應用

1、應用場景描述

在SpringMvc執行控制執行請求的時候,有這樣一個流程

1)前段控制器DispatcherServlet調用處理器適配器去執行Handler(也就是Controller);
2)處理器適配器去執行Handler,給適配器返回ModelAndView ;
3)處理器適配器向前端控制器返回ModelAndView ;

2、流程分析

  • 核心接口和實現

Controller和HandlerAdapter兩核心接口。

  • HandlerAdapter

適配器接口,使Handler有對應的適配器實現類,適配器代替Handler(控制層Controller)執行相應的方法。

public interface HandlerAdapter {// 判斷類型是否匹配boolean supports(Object var1);// 執行方法,返回ModelAndViewModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception; }

supports()方法傳入處理器,判斷適配器是否支持,如果支持則返回支持的適配器實現類。

  • DispatchServlert

抽取源碼中體現流程的幾個步驟。

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HandlerExecutionChain mappedHandler = null;mappedHandler = this.getHandler(processedRequest);HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());mv = ha.handle(processedRequest, response, mappedHandler.getHandler());mappedHandler.applyPostHandle(processedRequest, response, mv); }
  • SimpleControllerHandlerAdapter

最后看下supports和handle兩個方法的具體實現。

public class SimpleControllerHandlerAdapter implements HandlerAdapter {public SimpleControllerHandlerAdapter() {}public boolean supports(Object handler) {return handler instanceof Controller;}public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {return ((Controller)handler).handleRequest(request, response);} }

六、適配器優缺點

1、優點分析

更好的復用性,系統需要使用現有的類,而此類的接口不符合系統的需要。那么通過適配器模式就可以讓這些功能得到更好的復用。更好的擴展性。

2、缺點分析

過多的使用適配器,會讓系統非常零亂,不易整體進行把控。

七、源代碼地址

GitHub地址:知了一笑 https://github.com/cicadasmile 碼云地址:知了一笑 https://gitee.com/cicadasmile


總結

以上是生活随笔為你收集整理的Java描述设计模式(07):适配器模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。