java適配器模式之萬(wàn)物擬人化
什么是適配器模式
以下是百科的解釋。
在計(jì)算機(jī)編程中,適配器模式(有時(shí)候也稱(chēng)包裝樣式或者包裝)將一個(gè)類(lèi)的接口適配成用戶所期待的。一個(gè)適配允許通常因?yàn)榻涌诓患嫒荻荒茉谝黄鸸ぷ鞯念?lèi)工作在一起,做法是將類(lèi)自己的接口包裹在一個(gè)已存在的類(lèi)中。
共有兩類(lèi)適配器模式:
- 類(lèi)適配器模式:
這種適配器模式下,適配器繼承自已實(shí)現(xiàn)的類(lèi)(一般多重繼承)。
- 對(duì)象適配器模式:
在這種適配器模式中,適配器容納一個(gè)它包裹的類(lèi)的實(shí)例。在這種情況下,適配器調(diào)用被包裹對(duì)象。
設(shè)計(jì)模式和編程語(yǔ)言無(wú)關(guān),但是二當(dāng)家的依然用Java語(yǔ)言去實(shí)戰(zhàn)舉例。
類(lèi)的適配器模式

- 源(Adapee)角色:現(xiàn)在需要適配的接口。
- 目標(biāo)(Target)角色:這就是所期待得到的接口。注意:由于這里討論的是類(lèi)適配器模式,因此目標(biāo)不可以是類(lèi)。
- 適配器(Adaper)角色:適配器類(lèi)是本模式的核心。適配器把源接口轉(zhuǎn)換成目標(biāo)接口。顯然,這一角色不可以是接口,而必須是具體類(lèi)。
源(Adapee)角色
二當(dāng)家喜歡狗狗,所以養(yǎng)了一只狗狗,他有時(shí)候會(huì)發(fā)出叫聲。
package com.secondgod.adapter;
/**
* 狗狗
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Dog {
/**
* 發(fā)出聲音
*/
public void makeSound() {
System.out.println("狗狗:汪汪汪。。。。。。");
}
}
目標(biāo)(Target)角色
我們會(huì)和朋友聊天說(shuō)話。
package com.secondgod.adapter;
/**
* 朋友
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public interface IFriend {
/**
* 說(shuō)話
*/
void speak();
}
適配器(Adaper)角色
過(guò)了一段時(shí)間,二當(dāng)家把狗狗當(dāng)成了朋友,覺(jué)得它不是在叫,而是在說(shuō)話。
package com.secondgod.adapter;
/**
* 狗狗朋友
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class DogFriend extends Dog implements IFriend {
/**
* 說(shuō)話了
*/
@Override
public void speak() {
super.makeSound();
}
}
我們測(cè)試一下和狗狗朋友的說(shuō)話。
package com.secondgod.adapter;
/**
* 人
*
* @author 二當(dāng)家的白帽子 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);
}
}

二當(dāng)家的說(shuō)一句,狗狗叫一聲,我們真的像是在聊天。
增加源(Adapee)角色的后果
有一天,二當(dāng)家的又養(yǎng)了一只貓貓。
package com.secondgod.adapter;
/**
* 貓貓
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Cat {
/**
* 發(fā)出聲音
*/
public void makeSound() {
System.out.println("貓貓:喵喵喵。。。。。。");
}
}
過(guò)了幾天,二當(dāng)家的和貓貓也成了朋友。這時(shí)候只好再多增加一個(gè)貓朋友類(lèi)。
package com.secondgod.adapter;
/**
* 貓貓朋友
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class CatFriend extends Cat implements IFriend {
/**
* 說(shuō)話了
*/
@Override
public void speak() {
super.makeSound();
}
}
二當(dāng)家的和狗朋友,貓朋友聊天。
package com.secondgod.adapter;
/**
* 人
*
* @author 二當(dāng)家的白帽子 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);
}
}

以后要是二當(dāng)家的再有其他動(dòng)物朋友,就需要再去增加適配器類(lèi)。有沒(méi)有辦法通用一點(diǎn)呢?
對(duì)象的適配器模式
二當(dāng)家的希望可以有一個(gè)和各種動(dòng)物做朋友的辦法,而不是每次有了新的動(dòng)物朋友都需要增加一個(gè)適配器。

增加一個(gè)動(dòng)物接口
package com.secondgod.adapter;
/**
* 動(dòng)物
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public interface IAnimal {
/**
* 發(fā)出聲音
*/
void makeSound();
}
讓源(Adapee)角色的貓貓和狗狗實(shí)現(xiàn)動(dòng)物接口
package com.secondgod.adapter;
/**
* 狗狗
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Dog implements IAnimal {
/**
* 發(fā)出聲音
*/
public void makeSound() {
System.out.println("狗狗:汪汪汪。。。。。。");
}
}
package com.secondgod.adapter;
/**
* 貓貓
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Cat implements IAnimal {
/**
* 發(fā)出聲音
*/
public void makeSound() {
System.out.println("貓貓:喵喵喵。。。。。。");
}
}
萬(wàn)物擬人適配器(Adaper)角色
package com.secondgod.adapter;
/**
* 萬(wàn)物擬人適配器
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class AnimalFriendAdaper implements IFriend {
/**
* 被擬人化的動(dòng)物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
}
測(cè)試我們的萬(wàn)物擬人適配器。
package com.secondgod.adapter;
/**
* 人
*
* @author 二當(dāng)家的白帽子 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) {
// 一個(gè)人
Person person = new Person();
// 一只狗
IAnimal dog = new Dog();
// 一只貓
IAnimal cat = new Cat();
// 萬(wàn)物擬人
person.speakTo(new AnimalFriendAdaper(dog));
person.speakTo(new AnimalFriendAdaper(cat));
}
}

太好了。和動(dòng)物做朋友輕松多了。因?yàn)橛辛巳f(wàn)物擬人的適配器。
缺省適配模式

目標(biāo)(Target)角色增加行為聲明
有一天,朋友的標(biāo)準(zhǔn)變了。必須得會(huì)碼磚才行。
package com.secondgod.adapter;
/**
* 朋友
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public interface IFriend {
/**
* 說(shuō)話
*/
void speak();
/**
* 碼起來(lái)
*/
void coding();
}
適配器(Adaper)角色必須跟著增加行為實(shí)現(xiàn)
修改后的萬(wàn)物擬人適配器
package com.secondgod.adapter;
/**
* 萬(wàn)物擬人適配器
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class AnimalFriendAdaper implements IFriend {
/**
* 被擬人化的動(dòng)物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
@Override
public void coding() {
System.out.println("動(dòng)物:笑而不語(yǔ)搖搖頭。。。。。。");
}
}
缺省適配器
二當(dāng)家的想和動(dòng)物做朋友,但是不想去考慮他們?nèi)绾未a磚,以后二當(dāng)家的要是和植物做朋友,還得為植物朋友也實(shí)現(xiàn)碼磚行為,煩哦。所以我們來(lái)個(gè)默認(rèn)空實(shí)現(xiàn)。
package com.secondgod.adapter;
/**
* 缺省適配器
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public abstract class FriendAdaper implements IFriend {
@Override
public void speak() {
}
@Override
public void coding() {
}
}
package com.secondgod.adapter;
/**
* 萬(wàn)物擬人適配器
*
* @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
*/
public class AnimalFriendAdaper extends FriendAdaper {
/**
* 被擬人化的動(dòng)物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
}
由于多了一個(gè)默認(rèn)實(shí)現(xiàn),我們就不需要為萬(wàn)物適配器實(shí)現(xiàn)碼磚行為了。
適配器模式的用意是要改變?cè)吹慕涌?,以便于目?biāo)接口相容。缺省適配的用意稍有不同,它是為了方便建立一個(gè)不平庸的適配器類(lèi)而提供的一種平庸實(shí)現(xiàn)。
在任何時(shí)候,如果不準(zhǔn)備實(shí)現(xiàn)一個(gè)接口的所有方法時(shí),就可以使用“缺省適配模式”制造一個(gè)抽象類(lèi),給出所有方法的平庸的具體實(shí)現(xiàn)。這樣,從這個(gè)抽象類(lèi)再繼承下去的子類(lèi)就不必實(shí)現(xiàn)所有的方法了。
到此這篇關(guān)于java適配器模式之萬(wàn)物擬人化的文章就介紹到這了,更多相關(guān)java適配器模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java讀取PHP接口數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇java讀取PHP接口數(shù)據(jù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
Maven多模塊之父子關(guān)系的創(chuàng)建
這篇文章主要介紹了Maven多模塊之父子關(guān)系的創(chuàng)建,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
java實(shí)現(xiàn)微信掃碼登錄第三方網(wǎng)站功能(原理和代碼)
為避免繁瑣的注冊(cè)登陸,很多平臺(tái)和網(wǎng)站都會(huì)實(shí)現(xiàn)三方登陸的功能,增強(qiáng)用戶的粘性。這篇文章主要介紹了java實(shí)現(xiàn)微信掃碼登錄第三方網(wǎng)站功能(原理和代碼),避免做微信登錄開(kāi)發(fā)的朋友們少走彎路2022-12-12
springBoot項(xiàng)目中的static和templates文件夾的使用
本文主要介紹了springBoot項(xiàng)目中的static和templates文件夾的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法
這篇文章主要介紹了Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
詳解Spring DeferredResult異步操作使用場(chǎng)景
本文主要介紹了Spring DeferredResult異步操作使用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

