介面卡模式

2021-09-27 03:36:46 字數 1280 閱讀 1989

adapter(介面卡)模式主要用於解決介面間不相容問題,避免了新舊版本更替時需要大量修改客戶**的問題。

介面卡模式共有兩種實現方式:

1.類的介面卡模式

類的介面卡模式主要通過繼承實現

比如有乙個執行資料訪問物件的父介面,和針對具體表的子介面

public inte***ce idao

public inte***ce ideptdao extends idao

若實現子介面時並不想實現父介面中定義的所有方法,可以使用介面卡實現

定義乙個daoadapter類對父介面進行空實現

public class daoadapter implements idao

@override

public int insert(t e)

@override

public int update(t e)

@override

public int delete(int id)

}實現類同時繼承介面卡和實現ideptdao介面,就可以只實現需要的方法了

public class deptdaoimpl extends daoadapter implements ideptdao catch (exception e)

return emplist;

}@override

public int insert(dept dept) catch (exception e)

return 0;

}/**

* 根據物件的id更新物件屬性

*/@override

public int update(dept dept) catch (exception e)

return 0;}}

2.物件介面卡模式

物件介面卡中存放需要適配的物件的成員

inte***ce player

inte***ce audioplayer

inte***ce videoplayer

class myaudioplayer implements audioplayer

}class myvideoplayer implements videoplayer

}class myplayer implements player

@override

public void play(string audiotype, string filename) else if(audiotype.equalsignorecase("***"))}}

介面卡模式(類介面卡 物件介面卡)

做個筆記 引用 public inte ce usb public inte ce psp public class usber implements usb 類介面卡 psp適用usb介面 public class usbadapter extends usber implements psp 物...

介面卡模式 預設介面卡,類介面卡,物件介面卡

模式思想 改變乙個類的對外介面 增加或減少 以滿足不同外部呼叫者的需求 角色成員 目標介面 target 客戶所期待的介面。目標可以是具體的或抽象的類,也可以是介面。需要適配的類 adaptee 需要適配的類或適配者類。介面卡 adapter 通過包裝乙個需要適配的物件,把原介面轉換成目標介面。適配...

設計模式 介面卡模式 類介面卡 物件介面卡

乙個小例子,便於理解,上 這是我們造的。現在想用這個方法。public class adaptee 類介面卡。對我們想要的方法封裝一下,target就能像之前一樣,呼叫request方法即可。public class adapter1 extends adaptee implements targe...