- 非常感謝你閱讀本文,歡迎【👍點贊】【?收藏】【📝評論】~
- 放棄不難,但堅持一定很酷!希望我們大家都能每天進步一點點!🎉
- 本文由 二當家的白帽子 https://le-yi.blog.csdn.net/ 博客原創,轉載請注明來源,謝謝~
文章目錄
- 什么是配接器模式
- 類的配接器模式
- 源(Adapee)角色
- 目標(Target)角色
- 配接器(Adaper)角色
- 增加源(Adapee)角色的后果
- 物件的配接器模式
- 增加一個動物介面
- 讓源(Adapee)角色的貓貓和狗狗實作動物介面
- 萬物擬人配接器(Adaper)角色
- 預設適配模式
- 目標(Target)角色增加行為宣告
- 配接器(Adaper)角色必須跟著增加行為實作
- 預設配接器
什么是配接器模式
以下是百科的解釋,
在計算機編程中,配接器模式(有時候也稱包裝樣式或者包裝)將一個類的介面適配成用戶所期待的,一個適配允許通常因為介面不兼容而不能在一起作業的類作業在一起,做法是將類自己的介面包裹在一個已存在的類中,
共有兩類配接器模式:
- 類配接器模式:
這種配接器模式下,配接器繼承自已實作的類(一般多重繼承),- 物件配接器模式:
在這種配接器模式中,配接器容納一個它包裹的類的實體,在這種情況下,配接器呼叫被包裹物件,
設計模式和編程語言無關,但是二當家的依然用Java語言去實戰舉例,
類的配接器模式

- 源(Adapee)角色:現在需要適配的介面,
- 目標(Target)角色:這就是所期待得到的介面,注意:由于這里討論的是類配接器模式,因此目標不可以是類,
- 配接器(Adaper)角色:配接器類是本模式的核心,配接器把源介面轉換成目標介面,顯然,這一角色不可以是介面,而必須是具體類,
源(Adapee)角色
二當家喜歡狗狗,所以養了一只狗狗,他有時候會發出叫聲,
package com.secondgod.adapter;
/**
* 狗狗
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Dog {
/**
* 發出聲音
*/
public void makeSound() {
System.out.println("狗狗:汪汪汪,,,,,,");
}
}
目標(Target)角色
我們會和朋友聊天說話,
package com.secondgod.adapter;
/**
* 朋友
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public interface IFriend {
/**
* 說話
*/
void speak();
}
配接器(Adaper)角色
過了一段時間,二當家把狗狗當成了朋友,覺得它不是在叫,而是在說話,
package com.secondgod.adapter;
/**
* 狗狗朋友
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class DogFriend extends Dog implements IFriend {
/**
* 說話了
*/
@Override
public void speak() {
super.makeSound();
}
}
我們測驗一下和狗狗朋友的說話,
package com.secondgod.adapter;
/**
* 人
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Person {
/**
* 和朋友聊天
*
* @param friend
*/
public void speakTo(IFriend friend) {
System.out.println("人:朋友,你干什么呢?");
friend.speak();
}
public static void main(String[] args) {
Person person = new Person();
IFriend friend = new DogFriend();
person.speakTo(friend);
}
}

二當家的說一句,狗狗叫一聲,我們真的像是在聊天,
增加源(Adapee)角色的后果
有一天,二當家的又養了一只貓貓,
package com.secondgod.adapter;
/**
* 貓貓
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Cat {
/**
* 發出聲音
*/
public void makeSound() {
System.out.println("貓貓:喵喵喵,,,,,,");
}
}
過了幾天,二當家的和貓貓也成了朋友,這時候只好再多增加一個貓朋友類,
package com.secondgod.adapter;
/**
* 貓貓朋友
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class CatFriend extends Cat implements IFriend {
/**
* 說話了
*/
@Override
public void speak() {
super.makeSound();
}
}
二當家的和狗朋友,貓朋友聊天,
package com.secondgod.adapter;
/**
* 人
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Person {
/**
* 和朋友聊天
*
* @param friend
*/
public void speakTo(IFriend friend) {
System.out.println("人:朋友,你干什么呢?");
friend.speak();
}
public static void main(String[] args) {
Person person = new Person();
IFriend dogFriend = new DogFriend();
IFriend catFriend = new CatFriend();
person.speakTo(dogFriend);
person.speakTo(catFriend);
}
}

以后要是二當家的再有其他動物朋友,就需要再去增加配接器類,有沒有辦法通用一點呢?
物件的配接器模式
二當家的希望可以有一個和各種動物做朋友的辦法,而不是每次有了新的動物朋友都需要增加一個配接器,

增加一個動物介面
package com.secondgod.adapter;
/**
* 動物
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public interface IAnimal {
/**
* 發出聲音
*/
void makeSound();
}
讓源(Adapee)角色的貓貓和狗狗實作動物介面
package com.secondgod.adapter;
/**
* 狗狗
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Dog implements IAnimal {
/**
* 發出聲音
*/
public void makeSound() {
System.out.println("狗狗:汪汪汪,,,,,,");
}
}
package com.secondgod.adapter;
/**
* 貓貓
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Cat implements IAnimal {
/**
* 發出聲音
*/
public void makeSound() {
System.out.println("貓貓:喵喵喵,,,,,,");
}
}
萬物擬人配接器(Adaper)角色
package com.secondgod.adapter;
/**
* 萬物擬人配接器
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class AnimalFriendAdaper implements IFriend {
/**
* 被擬人化的動物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
}
測驗我們的萬物擬人配接器,
package com.secondgod.adapter;
/**
* 人
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Person {
/**
* 和朋友聊天
*
* @param friend
*/
public void speakTo(IFriend friend) {
System.out.println("人:朋友,你干什么呢?");
friend.speak();
}
public static void main(String[] args) {
// 一個人
Person person = new Person();
// 一只狗
IAnimal dog = new Dog();
// 一只貓
IAnimal cat = new Cat();
// 萬物擬人
person.speakTo(new AnimalFriendAdaper(dog));
person.speakTo(new AnimalFriendAdaper(cat));
}
}

太好了,和動物做朋友輕松多了,因為有了萬物擬人的配接器,
預設適配模式

目標(Target)角色增加行為宣告
有一天,朋友的標準變了,必須得會碼磚才行,
package com.secondgod.adapter;
/**
* 朋友
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public interface IFriend {
/**
* 說話
*/
void speak();
/**
* 碼起來
*/
void coding();
}
配接器(Adaper)角色必須跟著增加行為實作
修改后的萬物擬人配接器
package com.secondgod.adapter;
/**
* 萬物擬人配接器
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class AnimalFriendAdaper implements IFriend {
/**
* 被擬人化的動物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
@Override
public void coding() {
System.out.println("動物:笑而不語搖搖頭,,,,,,");
}
}
預設配接器
二當家的想和動物做朋友,但是不想去考慮他們如何碼磚,以后二當家的要是和植物做朋友,還得為植物朋友也實作碼磚行為,煩哦,所以我們來個默認空實作,
package com.secondgod.adapter;
/**
* 預設配接器
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public abstract class FriendAdaper implements IFriend {
@Override
public void speak() {
}
@Override
public void coding() {
}
}
package com.secondgod.adapter;
/**
* 萬物擬人配接器
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class AnimalFriendAdaper extends FriendAdaper implements IFriend {
/**
* 被擬人化的動物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
}
由于多了一個默認實作,我們就不需要為萬物配接器實作碼磚行為了,
配接器模式的用意是要改變源的介面,以便于目標介面相容,預設適配的用意稍有不同,它是為了方便建立一個不平庸的配接器類而提供的一種平庸實作,
在任何時候,如果不準備實作一個介面的所有方法時,就可以使用“預設適配模式”制造一個抽象類,給出所有方法的平庸的具體實作,這樣,從這個抽象類再繼承下去的子類就不必實作所有的方法了,

推薦繼續閱讀:java 設計模式實戰,原始模型模式之寫作業,克隆以后就是新的
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/294750.html
標籤:其他
