本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里
適配器模式把一個(gè)類的接口變換成客戶端所期待的另一種接口,從而使原本因接口不匹配而無法在一起工作的兩個(gè)類能夠在一起工作。適配器模式有類適配器模式和對(duì)象適配器模式,以及缺省(接口)適配器,三種不同的形式。
基于適配器模式,把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 ;
}
}
類的適配器模式把適配的類的API轉(zhuǎn)換成為目標(biāo)類的API。
這就是所期待得到的接口。
現(xiàn)在需要適配的接口。
適配器類是本模式的核心。適配器把源接口轉(zhuǎn)換成目標(biāo)接口。
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ì)象的適配器模式把被適配的類的API轉(zhuǎn)換成為目標(biāo)類的API,與類的適配器模式不同的是,對(duì)象的適配器模式不是使用繼承關(guān)系連接到Adaptee類,而是使用委派關(guān)系連接到Adaptee類。
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()");
}
}
缺省(接口)適配(Default Adapter)模式為一個(gè)接口提供缺省實(shí)現(xiàn),這樣子類型可以從這個(gè)缺省實(shí)現(xiàn)進(jìn)行擴(kuò)展,而不必從原有接口進(jìn)行擴(kuò)展。
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;
}
}
在SpringMvc執(zhí)行控制執(zhí)行請(qǐng)求的時(shí)候,有這樣一個(gè)流程
1)前段控制器DispatcherServlet調(diào)用處理器適配器去執(zhí)行Handler(也就是Controller);
2)處理器適配器去執(zhí)行Handler,給適配器返回ModelAndView ;
3)處理器適配器向前端控制器返回ModelAndView ;
Controller和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)類。
抽取源碼中體現(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);
}
最后看下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);
}
}
更好的復(fù)用性,系統(tǒng)需要使用現(xiàn)有的類,而此類的接口不符合系統(tǒng)的需要。那么通過適配器模式就可以讓這些功能得到更好的復(fù)用。更好的擴(kuò)展性。
過多的使用適配器,會(huì)讓系統(tǒng)非常零亂,不易整體進(jìn)行把控。
GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
文章題目:Java描述設(shè)計(jì)模式(07):適配器模式-創(chuàng)新互聯(lián)
瀏覽路徑:http://sd-ha.com/article8/dghsip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、用戶體驗(yàn)、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、服務(wù)器托管、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容