-
盡量少修改代碼庭瑰,通過擴(kuò)展的方式解決了問題,這就是適配模式。
Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.(將一個(gè)類的接口變換成客戶端所期待的另一種接口,從而使原本因接口不匹配而無法在一起工作的兩個(gè)類能夠在一起工作栓拜。)
適配器模式又叫做變壓器模式,也叫做包裝模式(Wrapper)惠昔,但是包裝模式可不止一個(gè)幕与,還包括了裝飾模式。
適配器通用類圖
簡(jiǎn)單地說镇防,適配器模式就是把一個(gè)接口或類轉(zhuǎn)換成其他的接口或類啦鸣,從另一方面來說,適配器模式也就是一個(gè)包裝模式来氧,為什么呢诫给?它把Adaptee包裝成一個(gè)Target接口的類,加了一層衣服啦扬。大家知道中狂,設(shè)計(jì)模式原是為建筑設(shè)計(jì)而服務(wù)的,軟件設(shè)計(jì)模式只是借用了人家的原理而已扑毡,那我們來看看最原始的適配器是如何設(shè)計(jì)的胃榕。
-
適配器簡(jiǎn)單例子
//目標(biāo)類
public interface Target {
public void doSomeing();
}
//目標(biāo)角色實(shí)現(xiàn)類
public class ConcreteTarget implements Target {
@Override
public void doSomeing() {
System.out.println("我是一個(gè)小小鳥...");
}
}
//原角色
public class Adaptee {
public void doAdd() {
System.out.println("我想飛的更高...");
}
}
//轉(zhuǎn)換類
public class Adapter extends Adaptee implements Target {
@Override
public void doSomeing() {
super.doAdd();
}
}
//客戶端
public class Client {
public static void main(String[] args) {
Target target = new ConcreteTarget();
target.doSomeing();
Target target1 = new Adapter();
target1.doSomeing();
}
}
-
適配器模式的優(yōu)點(diǎn)
1.類的透明性
2.類的復(fù)用度
3.靈活性
-
使用場(chǎng)景
你有要修改一個(gè)已經(jīng)投產(chǎn)中的接口時(shí),適配器模式可能是最適合你的模式
-
適配器注意事項(xiàng)
設(shè)計(jì)階段不要考慮瞄摊,不是為了解決開發(fā)階段問題勤晚,而是為了解決已經(jīng)上生產(chǎn)項(xiàng)目問題
再次提醒一點(diǎn)枉层,項(xiàng)目一定要遵守依賴倒置原則和里氏替換原則泉褐,否則即使在適合使用適配器的場(chǎng)合下赐写,也會(huì)帶來非常大的改造。
-
舉個(gè)比較特殊的例子
原目標(biāo)類有三個(gè)膜赃,目標(biāo)類有一個(gè)
//源目標(biāo)
public interface IBMWFactoryInfo {
//基本信息挺邀,比如什么車產(chǎn)地等
public Map getBMWCar();
}
//源目標(biāo)
public interface IBenzFactoryInfo {
//基本信息,比如什么車類型等
public Map getBenzCar();
}
//源目標(biāo)
public interface IAudiFactoryInfo {
//基本信息跳座,比如什么車端铛,輪胎、大小等
public Map getAudiCar();
}
//源目標(biāo)實(shí)現(xiàn)類
public class BMWFactory implements IBMWFactoryInfo {
@Override
public Map getBMWCar() {
Map<String,String> map = new HashMap<String,String>();
map.put("place","Germany");
map.put("engine","v12");
return map;
}
}
//源目標(biāo)實(shí)現(xiàn)類
public class BenzFactory implements IBenzFactoryInfo {
@Override
public Map getBenzCar() {
Map<String,String> map = new HashMap<String,String>();
map.put("model","S600");
map.put("engine","w12");
return map;
}
}
//源目標(biāo)實(shí)現(xiàn)類
public class AudiFactory implements IAudiFactoryInfo {
@Override
public Map getAudiCar() {
Map<String,String> map = new HashMap<String,String>();
map.put("carname","Audi");
map.put("engine","v12");
return map;
}
}
//目標(biāo)類
public interface CarInfo {
public String getCarName();
public String getCarEngien();
}
//轉(zhuǎn)換類
public class AllCarFactory implements CarInfo {
//源目標(biāo)對(duì)象
private IBMWFactoryInfo ibmwFactoryInfo = null;
private IBenzFactoryInfo iBenzFactoryInfo = null;
private IAudiFactoryInfo iAudiFactoryInfo = null;
//數(shù)據(jù)處理
private Map<String,String> bmwMap = null;
private Map<String,String> benzMap = null;
private Map<String,String> audiMap = null;
public AllCarFactory(IBMWFactoryInfo ibmwFactoryInfo, IBenzFactoryInfo iBenzFactoryInfo,
IAudiFactoryInfo iAudiFactoryInfo) {
this.ibmwFactoryInfo = ibmwFactoryInfo;
this.iBenzFactoryInfo = iBenzFactoryInfo;
this.iAudiFactoryInfo = iAudiFactoryInfo;
this.audiMap = iAudiFactoryInfo.getAudiCar();
this.benzMap = iBenzFactoryInfo.getBenzCar();
this.bmwMap = ibmwFactoryInfo.getBMWCar();
}
@Override
public String getCarName() {
return audiMap.get("carname");
}
@Override
public String getCarEngien() {
return audiMap.get("engine");
}
}
//客戶端
public class Client {
public static void main(String[] args) {
IAudiFactoryInfo iAudiFactoryInfo = new AudiFactory();
IBenzFactoryInfo iBenzFactoryInfo = new BenzFactory();
IBMWFactoryInfo ibmwFactoryInfo = new BMWFactory();
AllCarFactory allCarFactory = new AllCarFactory(ibmwFactoryInfo,iBenzFactoryInfo,iAudiFactoryInfo);
System.out.println(allCarFactory.getCarEngien());
System.out.println(allCarFactory.getCarName());
}
}
運(yùn)行結(jié)果
以上就是適配器的簡(jiǎn)單用法疲眷,歡迎大家指教禾蚕,萬分感謝