什么是適配器模式
以下是百科的解釋。
在計(jì)算機(jī)編程中,適配器模式(有時(shí)候也稱包裝樣式或者包裝)將一個(gè)類的接口適配成用戶所期待的。一個(gè)適配允許通常因?yàn)榻涌诓患嫒荻荒茉谝黄鸸ぷ鞯念惞ぷ髟谝黄穑龇ㄊ菍㈩愖约旱慕涌诎谝粋€(gè)已存在的類中。
共有兩類適配器模式:
- 類適配器模式:
這種適配器模式下,適配器繼承自已實(shí)現(xiàn)的類(一般多重繼承)。
- 對(duì)象適配器模式:
在這種適配器模式中,適配器容納一個(gè)它包裹的類的實(shí)例。在這種情況下,適配器調(diào)用被包裹對(duì)象。
設(shè)計(jì)模式和編程語言無關(guān),但是二當(dāng)家的依然用Java語言去實(shí)戰(zhàn)舉例。
類的適配器模式
- 源(Adapee)角色:現(xiàn)在需要適配的接口。
- 目標(biāo)(Target)角色:這就是所期待得到的接口。注意:由于這里討論的是類適配器模式,因此目標(biāo)不可以是類。
- 適配器(Adaper)角色:適配器類是本模式的核心。適配器把源接口轉(zhuǎn)換成目標(biāo)接口。顯然,這一角色不可以是接口,而必須是具體類。
源(Adapee)角色
二當(dāng)家喜歡狗狗,所以養(yǎng)了一只狗狗,他有時(shí)候會(huì)發(fā)出叫聲。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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ì)和朋友聊天說話。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.secondgod.adapter; /** * 朋友 * * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/ */ public interface IFriend { /** * 說話 */ void speak(); } |
適配器(Adaper)角色
過了一段時(shí)間,二當(dāng)家把狗狗當(dāng)成了朋友,覺得它不是在叫,而是在說話。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.secondgod.adapter; /** * 狗狗朋友 * * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/ */ public class DogFriend extends Dog implements IFriend { /** * 說話了 */ @Override public void speak() { super .makeSound(); } } |
我們測(cè)試一下和狗狗朋友的說話。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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)家的說一句,狗狗叫一聲,我們真的像是在聊天。
增加源(Adapee)角色的后果
有一天,二當(dāng)家的又養(yǎng)了一只貓貓。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.secondgod.adapter; /** * 貓貓 * * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/ */ public class Cat { /** * 發(fā)出聲音 */ public void makeSound() { System.out.println( "貓貓:喵喵喵。。。。。。" ); } } |
過了幾天,二當(dāng)家的和貓貓也成了朋友。這時(shí)候只好再多增加一個(gè)貓朋友類。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.secondgod.adapter; /** * 貓貓朋友 * * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/ */ public class CatFriend extends Cat implements IFriend { /** * 說話了 */ @Override public void speak() { super .makeSound(); } } |
二當(dāng)家的和狗朋友,貓朋友聊天。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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)物朋友,就需要再去增加適配器類。有沒有辦法通用一點(diǎn)呢?
對(duì)象的適配器模式
二當(dāng)家的希望可以有一個(gè)和各種動(dòng)物做朋友的辦法,而不是每次有了新的動(dòng)物朋友都需要增加一個(gè)適配器。
增加一個(gè)動(dòng)物接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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)物接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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( "狗狗:汪汪汪。。。。。。" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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( "貓貓:喵喵喵。。。。。。" ); } } |
萬物擬人適配器(Adaper)角色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.secondgod.adapter; /** * 萬物擬人適配器 * * @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è)試我們的萬物擬人適配器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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(); // 萬物擬人 person.speakTo( new AnimalFriendAdaper(dog)); person.speakTo( new AnimalFriendAdaper(cat)); } } |
太好了。和動(dòng)物做朋友輕松多了。因?yàn)橛辛巳f物擬人的適配器。
缺省適配模式
目標(biāo)(Target)角色增加行為聲明
有一天,朋友的標(biāo)準(zhǔn)變了。必須得會(huì)碼磚才行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.secondgod.adapter; /** * 朋友 * * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/ */ public interface IFriend { /** * 說話 */ void speak(); /** * 碼起來 */ void coding(); } |
適配器(Adaper)角色必須跟著增加行為實(shí)現(xiàn)
修改后的萬物擬人適配器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.secondgod.adapter; /** * 萬物擬人適配器 * * @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)物:笑而不語搖搖頭。。。。。。" ); } } |
缺省適配器
二當(dāng)家的想和動(dòng)物做朋友,但是不想去考慮他們?nèi)绾未a磚,以后二當(dāng)家的要是和植物做朋友,還得為植物朋友也實(shí)現(xiàn)碼磚行為,煩哦。所以我們來個(gè)默認(rèn)空實(shí)現(xiàn)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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() { } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.secondgod.adapter; /** * 萬物擬人適配器 * * @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),我們就不需要為萬物適配器實(shí)現(xiàn)碼磚行為了。
適配器模式的用意是要改變?cè)吹慕涌冢员阌谀繕?biāo)接口相容。缺省適配的用意稍有不同,它是為了方便建立一個(gè)不平庸的適配器類而提供的一種平庸實(shí)現(xiàn)。
在任何時(shí)候,如果不準(zhǔn)備實(shí)現(xiàn)一個(gè)接口的所有方法時(shí),就可以使用“缺省適配模式”制造一個(gè)抽象類,給出所有方法的平庸的具體實(shí)現(xiàn)。這樣,從這個(gè)抽象類再繼承下去的子類就不必實(shí)現(xiàn)所有的方法了。
到此這篇關(guān)于java適配器模式之萬物擬人化的文章就介紹到這了,更多相關(guān)java適配器模式內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/leyi520/article/details/119730567