Java結(jié)構(gòu)型設(shè)計(jì)模式之適配器模式詳解
適配器模式
適配器模式(Adapter Pattern)又叫做變壓器模式,屬于結(jié)構(gòu)型設(shè)計(jì)模式。
它的功能是將一個(gè)類的接口變成客戶端所期望的另一種接口,從而使原本因接口不匹配而導(dǎo)致無法在一起工作的兩個(gè)類能夠一起工作。
適配器就像一個(gè)中間層,起著轉(zhuǎn)化委托的作用,將一種接口轉(zhuǎn)化為另一種符合需求的接口。

分類
適配器模式有3種形式:類適配器、對(duì)象適配器、接口適配器
類適配器:
類適配器的原理就是通過繼承來實(shí)現(xiàn)適配器功能。
對(duì)象適配器:
對(duì)象適配器的原理就是通過組合來實(shí)現(xiàn)適配器功能。
接口適配器:
接口適配器的主要原理就是原理利用抽象類實(shí)現(xiàn)接口,并且空實(shí)現(xiàn)接口眾多方法。
應(yīng)用場(chǎng)景
1.已經(jīng)存在的類,它的方法和需求不匹配(方法結(jié)果相同或相以)的情況。
2.適配器模式不是軟件設(shè)計(jì)階段考慮的設(shè)計(jì)模式,是隨著軟件維護(hù),由于不同產(chǎn)品、不同廠家造成功能類似以而接口不相同情況下的解決方案。
說明:
當(dāng)系統(tǒng)存在兩種接口A和B,客戶端只支持訪問A接口,但是當(dāng)前系統(tǒng)沒有A接口對(duì)象,但是有B接口對(duì)象,但客戶無法識(shí)別B接口,因此需要通過一個(gè)適配器C,將B接口內(nèi)容轉(zhuǎn)換成A接口,從而使得客戶能夠從A接口獲取得到B接口內(nèi)容。
舉例:在不同的國(guó)家電源電壓不一致,但是筆記本充電器通常有一個(gè)電壓范圍,其相當(dāng)于使用了適配器,讓其適應(yīng)了不通過國(guó)家的電壓。
優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
1.能提高類的透明性和復(fù)用,現(xiàn)有的類復(fù)用但不需要改變。
2.目標(biāo)類和適配器類解耦,提高程序的擴(kuò)展性。
3.在很多業(yè)務(wù)場(chǎng)景中符合開閉原則。
缺點(diǎn):
1.適配器編寫過程需要全面考慮,可能會(huì)增加系統(tǒng)的復(fù)雜性。
2.增加代碼閱讀難度,降低代碼可讀性,過多使用適配器會(huì)使系統(tǒng)代碼變得凌亂。
主要角色
1.目標(biāo)角色(Target)
目標(biāo)角色也就是期望的接口
2.源角色(Adaptee)
存在于系統(tǒng)中,內(nèi)容滿足客戶需求(需轉(zhuǎn)換),但接口不匹配的接口實(shí)例
3.適配器(Adapter)
將源角色(Adaptee)轉(zhuǎn)化為目標(biāo)角色(Target)的類實(shí)例
適配器模式各角色之間的關(guān)系
需要的是Target接口,但Target接口設(shè)有一個(gè)實(shí)例符合需求,而Adaptee實(shí)例符合需求;但是無法直接使用Adaptee(接口不兼容);因此需要一個(gè)適配器(Adapter)來進(jìn)行中轉(zhuǎn),讓Adaptee能轉(zhuǎn)化為Target接口形式;
類適配器
類適配器的原理就是通過繼承來實(shí)現(xiàn)適配器功能。
讓Adapter實(shí)現(xiàn)Target接口,并且繼承Adaptee,這樣Adapter就具備Target和Adaptee的特性,就可以將兩者進(jìn)行轉(zhuǎn)化。
舉例:以不同設(shè)備使用不同交流電為例,通過電源適配器進(jìn)行轉(zhuǎn)換說明。
創(chuàng)建目標(biāo)角色(Target)
public interface Target {
int out();
}
創(chuàng)建源角色(Adaptee)
public class Adaptee{
public int input() {
System.out.println("輸入交流電: 220V");
return 220;
}
}
創(chuàng)建適配器(Adapter)
public class Adapter extends Adaptee implements Target {
@Override
public int out() {
int input220V = super.input();
int output = input220V / 2;
System.out.println("輸出交流電: " + output + "V");
return output;
}
}
客戶端調(diào)用
public static void main(String[] args) {
Target adapter = new Adapter();
int result = adapter.out();
System.out.println(result);
}
輸入交流電: 220V
輸出交流電: 110V
110
對(duì)象適配器
對(duì)象適配器的原理就是通過組合來實(shí)現(xiàn)適配器功能。
讓Adapter實(shí)現(xiàn)Target接口,然后內(nèi)部持有Adaptee實(shí)例,然后再Target接口規(guī)定的方法內(nèi)轉(zhuǎn)換Adaptee。
創(chuàng)建目標(biāo)角色(Target)
public interface Target {
int out();
}
創(chuàng)建源角色(Adaptee)
public class Adaptee{
public int input() {
System.out.println("輸入交流電: 220V");
return 220;
}
}
創(chuàng)建適配器(Adapter)
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public int out() {
int output = adaptee.input() / 2;
System.out.println("輸出交流電: " + output + "V");
return output;
}
}客戶端調(diào)用
public static void main(String[] args) {
Target adapter = new Adapter(new Adaptee());
int result = adapter.out();
System.out.println(result);
}輸入交流電: 220V
輸出交流電: 110V
110
接口適配器
接口適配器的關(guān)注點(diǎn)與類適配器和對(duì)象適配器的關(guān)注點(diǎn)不太一樣,類適配器和對(duì)象適配器著重于將系統(tǒng)存在的一個(gè)角色(Adaptee)轉(zhuǎn)化成目標(biāo)接口(Target)所需內(nèi)容,而接口適配器的使用場(chǎng)景是解決接口方法過多,如果直接實(shí)現(xiàn)接口,那么類會(huì)多出許多空實(shí)現(xiàn)的方法,類顯得很臃腫。此時(shí),使用接口適配器就能讓我們只實(shí)現(xiàn)我們需要的接口方法,目標(biāo)更清晰。
接口適配器的主要原理就是原理利用抽象類實(shí)現(xiàn)接口,并且空實(shí)現(xiàn)接口眾多方法。
創(chuàng)建目標(biāo)角色(Target)
public interface Target {
int out1();
int out2();
int out3();
int out4();
}
創(chuàng)建源角色(Adaptee)
public class Adaptee{
public int input() {
System.out.println("輸入交流電: 220V");
return 220;
}
}
創(chuàng)建適配器(Adapter)
public class Adapter implements Target {
protected Adaptee adaptee;
public Adapter(Adaptee adaptee){
this.adaptee = adaptee;
}
@Override
public int out1() {
int input220V = adaptee.input();
int output = input220V / 1;
System.out.println("輸出交流電: " + output + "V");
return output;
}
@Override
public int out2() {
int input220V = adaptee.input();
int output = input220V / 2;
System.out.println("輸出交流電: " + output + "V");
return output;
}
@Override
public int out3() {
return 0;
}
@Override
public int out4() {
return 0;
}
}客戶端調(diào)用
public static void main(String[] args) {
Target adapter = new Adapter(new Adaptee());
adapter.out1();
System.out.println("---------------------");
adapter.out2();
System.out.println("---------------------");
Target adapter2 = new Adapter(new Adaptee()) {
@Override
public int out3() {
int input220V = adaptee.input();
int output = input220V / 3;
System.out.println("輸出交流電: " + output + "V");
return output;
}
};
adapter2.out3();
System.out.println("---------------------");
Target adapter3 = new Adapter(new Adaptee()) {
@Override
public int out4() {
int input220V = adaptee.input();
int output = input220V / 4;
System.out.println("輸出交流電: " + output + "V");
return output;
}
};
adapter3.out4();
}輸入交流電: 220V
輸出交流電: 220V
---------------------
輸入交流電: 220V
輸出交流電: 110V
---------------------
輸入交流電: 220V
輸出交流電: 73V
---------------------
輸入交流電: 220V
輸出交流電: 55V
到此這篇關(guān)于Java結(jié)構(gòu)型設(shè)計(jì)模式之適配器模式詳解的文章就介紹到這了,更多相關(guān)Java適配器模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot啟動(dòng)時(shí)如何指定spring.profiles.active
這篇文章主要介紹了springboot啟動(dòng)時(shí)如何指定spring.profiles.active問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
EL表達(dá)式簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
EL全名為Expression Language,這篇文章主要給大家介紹EL表達(dá)式的主要作用及內(nèi)容簡(jiǎn)介,感興趣的朋友一起看看2017-07-07
IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問題
這篇文章主要介紹了IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
解決Maven的pom.xml中設(shè)置repository不起作用問題
這篇文章主要介紹了解決Maven的pom.xml中設(shè)置repository不起作用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
AsyncHttpClient IOExceptionFilter異常過濾器
這篇文章主要為大家介紹了AsyncHttpClient IOExceptionFilter異常過濾器代碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

