java適配器模式之萬物擬人化
什么是適配器模式
以下是百科的解釋。
在計算機編程中,適配器模式(有時候也稱包裝樣式或者包裝)將一個類的接口適配成用戶所期待的。一個適配允許通常因為接口不兼容而不能在一起工作的類工作在一起,做法是將類自己的接口包裹在一個已存在的類中。
共有兩類適配器模式:
- 類適配器模式:
這種適配器模式下,適配器繼承自已實現(xiàn)的類(一般多重繼承)。
- 對象適配器模式:
在這種適配器模式中,適配器容納一個它包裹的類的實例。在這種情況下,適配器調(diào)用被包裹對象。
設(shè)計模式和編程語言無關(guān),但是二當家的依然用Java語言去實戰(zhàn)舉例。
類的適配器模式

- 源(Adapee)角色:現(xiàn)在需要適配的接口。
- 目標(Target)角色:這就是所期待得到的接口。注意:由于這里討論的是類適配器模式,因此目標不可以是類。
- 適配器(Adaper)角色:適配器類是本模式的核心。適配器把源接口轉(zhuǎn)換成目標接口。顯然,這一角色不可以是接口,而必須是具體類。
源(Adapee)角色
二當家喜歡狗狗,所以養(yǎng)了一只狗狗,他有時候會發(fā)出叫聲。
package com.secondgod.adapter;
/**
* 狗狗
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Dog {
/**
* 發(fā)出聲音
*/
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)角色的后果
有一天,二當家的又養(yǎng)了一只貓貓。
package com.secondgod.adapter;
/**
* 貓貓
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Cat {
/**
* 發(fā)出聲音
*/
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 {
/**
* 發(fā)出聲音
*/
void makeSound();
}
讓源(Adapee)角色的貓貓和狗狗實現(xiàn)動物接口
package com.secondgod.adapter;
/**
* 狗狗
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Dog implements IAnimal {
/**
* 發(fā)出聲音
*/
public void makeSound() {
System.out.println("狗狗:汪汪汪。。。。。。");
}
}
package com.secondgod.adapter;
/**
* 貓貓
*
* @author 二當家的白帽子 https://le-yi.blog.csdn.net/
*/
public class Cat implements IAnimal {
/**
* 發(fā)出聲音
*/
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)角色必須跟著增加行為實現(xiàn)
修改后的萬物擬人適配器
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("動物:笑而不語搖搖頭。。。。。。");
}
}
缺省適配器
二當家的想和動物做朋友,但是不想去考慮他們?nèi)绾未a磚,以后二當家的要是和植物做朋友,還得為植物朋友也實現(xiàn)碼磚行為,煩哦。所以我們來個默認空實現(xiàn)。
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 {
/**
* 被擬人化的動物朋友
*/
private IAnimal animal;
public AnimalFriendAdaper(IAnimal animal) {
this.animal = animal;
}
@Override
public void speak() {
animal.makeSound();
}
}
由于多了一個默認實現(xiàn),我們就不需要為萬物適配器實現(xiàn)碼磚行為了。
適配器模式的用意是要改變源的接口,以便于目標接口相容。缺省適配的用意稍有不同,它是為了方便建立一個不平庸的適配器類而提供的一種平庸實現(xiàn)。
在任何時候,如果不準備實現(xiàn)一個接口的所有方法時,就可以使用“缺省適配模式”制造一個抽象類,給出所有方法的平庸的具體實現(xiàn)。這樣,從這個抽象類再繼承下去的子類就不必實現(xiàn)所有的方法了。
到此這篇關(guān)于java適配器模式之萬物擬人化的文章就介紹到這了,更多相關(guān)java適配器模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java讀取PHP接口數(shù)據(jù)的實現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava讀取PHP接口數(shù)據(jù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
Maven多模塊之父子關(guān)系的創(chuàng)建
這篇文章主要介紹了Maven多模塊之父子關(guān)系的創(chuàng)建,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03
java實現(xiàn)微信掃碼登錄第三方網(wǎng)站功能(原理和代碼)
為避免繁瑣的注冊登陸,很多平臺和網(wǎng)站都會實現(xiàn)三方登陸的功能,增強用戶的粘性。這篇文章主要介紹了java實現(xiàn)微信掃碼登錄第三方網(wǎng)站功能(原理和代碼),避免做微信登錄開發(fā)的朋友們少走彎路2022-12-12
springBoot項目中的static和templates文件夾的使用
本文主要介紹了springBoot項目中的static和templates文件夾的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
詳解Spring DeferredResult異步操作使用場景
本文主要介紹了Spring DeferredResult異步操作使用場景,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10

