溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java描述設(shè)計(jì)模式(07):適配器模式

發(fā)布時(shí)間:2020-06-30 09:42:44 來源:網(wǎng)絡(luò) 閱讀:233 作者:知了一笑 欄目:編程語(yǔ)言

本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里

一、適配器模式簡(jiǎn)介

1、基礎(chǔ)概念

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

2、生活場(chǎng)景

基于適配器模式,把220V的電壓,轉(zhuǎn)換為需要的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 {
    // 電流轉(zhuǎn)換方法
    @Override
    public int get110VCurrent() {
        int high = get220VCurrent() ;
        int low = high/2 ;
        return low ;
    }
}

二、類適配器

1、模式簡(jiǎn)介

類的適配器模式把適配的類的API轉(zhuǎn)換成為目標(biāo)類的API。
Java描述設(shè)計(jì)模式(07):適配器模式

2、核心角色

  • 目標(biāo)(Target)角色

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

  • 源(Adapee)角色:

現(xiàn)在需要適配的接口。

  • 適配器(Adaper)角色

適配器類是本模式的核心。適配器把源接口轉(zhuǎn)換成目標(biāo)接口。

3、源碼實(shí)現(xiàn)

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

    @Override
    public void sampleOperation2() {
        System.out.println("Adapter.sampleOperation2()");
    }
}

三、對(duì)象適配器

1、模式簡(jiǎn)介

與類的適配器模式一樣,對(duì)象的適配器模式把被適配的類的API轉(zhuǎn)換成為目標(biāo)類的API,與類的適配器模式不同的是,對(duì)象的適配器模式不是使用繼承關(guān)系連接到Adaptee類,而是使用委派關(guān)系連接到Adaptee類。

Java描述設(shè)計(jì)模式(07):適配器模式

2、源碼實(shí)現(xiàn)

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();
    }

    @Override
    public void sampleOperation2() {
        System.out.println("Adapter.sampleOperation2()");
    }
}

四、接口適配器

1、模式簡(jiǎn)介

缺省(接口)適配(Default Adapter)模式為一個(gè)接口提供缺省實(shí)現(xiàn),這樣子類型可以從這個(gè)缺省實(shí)現(xiàn)進(jìn)行擴(kuò)展,而不必從原有接口進(jìn)行擴(kuò)展。

Java描述設(shè)計(jì)模式(07):適配器模式

2、源代碼實(shí)現(xiàn)

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

    }
    @Override
    public int serviceOperation2() {
        return 0;
    }
    @Override
    public String serviceOperation3() {
        return null;
    }
}

五、Spring框架應(yīng)用

1、應(yīng)用場(chǎng)景描述

在SpringMvc執(zhí)行控制執(zhí)行請(qǐng)求的時(shí)候,有這樣一個(gè)流程

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

2、流程分析

  • 核心接口和實(shí)現(xiàn)

Controller和HandlerAdapter兩核心接口。
Java描述設(shè)計(jì)模式(07):適配器模式
Java描述設(shè)計(jì)模式(07):適配器模式

  • HandlerAdapter

適配器接口,使Handler有對(duì)應(yīng)的適配器實(shí)現(xiàn)類,適配器代替Handler(控制層Controller)執(zhí)行相應(yīng)的方法。

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

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

  • DispatchServlert

抽取源碼中體現(xiàn)流程的幾個(gè)步驟。

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兩個(gè)方法的具體實(shí)現(xiàn)。

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);
    }
}

六、適配器優(yōu)缺點(diǎn)

1、優(yōu)點(diǎn)分析

更好的復(fù)用性,系統(tǒng)需要使用現(xiàn)有的類,而此類的接口不符合系統(tǒng)的需要。那么通過適配器模式就可以讓這些功能得到更好的復(fù)用。更好的擴(kuò)展性。

2、缺點(diǎn)分析

過多的使用適配器,會(huì)讓系統(tǒng)非常零亂,不易整體進(jìn)行把控。

七、源代碼地址

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Java描述設(shè)計(jì)模式(07):適配器模式

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI