国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

設(shè)計(jì)模式之適配器模式

2019-11-10 23:09:29
字體:
供稿:網(wǎng)友

適配器模式(Adapter):將一個類的接口轉(zhuǎn)換成客戶端(client)希望的另外一個接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作。 Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

適配器 這個有類adaptee方法method1()和method2()繼承自ownInterface接口,現(xiàn)在有一個接口target有三個方法,需要用到method1()或者method2(),為了不造成耦合,需要一個適配器adapter將adaptee進(jìn)行適配(java中有直接繼承adaptee和 委托 方法),這樣client就可以通過target調(diào)用adaptee的方法或者是自己實(shí)現(xiàn)的another

適配器模式分為類適配器和對象適配器. 類適配器的實(shí)現(xiàn)就是通過繼承adaaptee實(shí)現(xiàn)適配,對象適配器就是用委托的方式實(shí)現(xiàn)適配

原始類

// 原始類public class Adaptee implements OwnInterface { public void method1() { System.out.類適配器

public class Adapter extends Adaptee implements Target{ public void method1() { super.method1(); } public void method2() { super.method2(); } public void method3() { System.out.println("自己實(shí)現(xiàn)的功能1"); }}

對象適配器

public class Adapter implements Target{ //委托或者代理 private Adaptee adaptee = new Adaptee(); public void method1() { this.adaptee.method1(); } public void method2() { this.adaptee.method2(); } public void method3() { System.out.println("自己實(shí)現(xiàn)的功能1"); }}

client類

public class Client { public static void main(String[] args) { // 使用普通功能類 Target another= new Another(); another.method1(); // 使用特殊功能類,即適配類, // 需要先創(chuàng)建一個被適配類的對象作為參數(shù) Target adapter = new Adapter(); adapter.method1(); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 辽中县| 湛江市| 乐都县| 阿拉尔市| 盘锦市| 揭西县| 朝阳区| 大化| 综艺| 兴宁市| 嘉善县| 和平区| 汤阴县| 石柱| 枣阳市| 梁河县| 海门市| 曲靖市| 玉屏| 峨山| 台北市| 舞钢市| 九龙城区| 龙胜| 安康市| 兴海县| 桐庐县| 崇明县| 庄浪县| 三台县| 将乐县| 扎鲁特旗| 堆龙德庆县| 满洲里市| 云阳县| 元朗区| 南通市| 娄烦县| 渭南市| 安多县| 上饶市|