作者:冰河
星球:http://m6z.cn/6aeFbs
博客:https://binghe.gitcode.host
文章匯總:https://binghe.gitcode.host/md/all/all.html
源碼地址:https://github.com/binghe001/java-simple-design-patterns/tree/master/java-simple-design-facade
沉淀,成長(zhǎng),突破,幫助他人,成就自我。
- 本章難度:★★☆☆☆
- 本章重點(diǎn):用最簡(jiǎn)短的篇幅介紹外觀模式最核心的知識(shí),理解外觀模式的設(shè)計(jì)精髓,并能夠靈活運(yùn)用到實(shí)際項(xiàng)目中,編寫(xiě)可維護(hù)的代碼。
大家好,我是冰河~~
今天給大家介紹《Java極簡(jiǎn)設(shè)計(jì)模式》的第08章,外觀模式(Facade),多一句沒(méi)有,少一句不行,用最簡(jiǎn)短的篇幅講述設(shè)計(jì)模式最核心的知識(shí),好了,開(kāi)始今天的內(nèi)容。
一、概述
為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,F(xiàn)acade模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。
二、適用性
1.當(dāng)為一個(gè)復(fù)雜子系統(tǒng)提供一個(gè)簡(jiǎn)單接口時(shí),子系統(tǒng)往往因?yàn)椴粩嘌莼兊迷絹?lái)越 復(fù)雜。大多數(shù)模式使用時(shí)都會(huì)產(chǎn)生更多更小的類。這使得子系統(tǒng)更具可重用性,也更容 易對(duì)子系統(tǒng)進(jìn)行定制,但這也給那些不需要定制子系統(tǒng)的用戶帶來(lái)一些使用上的困難。 Facade可以提供一個(gè)簡(jiǎn)單的缺省視圖,這一視圖對(duì)大多數(shù)用戶來(lái)說(shuō)已經(jīng)足夠,而那些需 要更多的可定制性的用戶可以越過(guò)facade層。
2.客戶程序與抽象類的實(shí)現(xiàn)部分之間存在著很大的依賴性。引入facade將這個(gè)子系統(tǒng)與客 戶以及其他的子系統(tǒng)分離,可以提高子系統(tǒng)的獨(dú)立性和可移植性。
3.當(dāng)需要構(gòu)建一個(gè)層次結(jié)構(gòu)的子系統(tǒng)時(shí),使用facade模式定義子系統(tǒng)中每層的入口點(diǎn)。 如果子系統(tǒng)之間是相互依賴的,你可以讓它們僅通過(guò)facade進(jìn)行通訊,從而簡(jiǎn)化了它們 之間的依賴關(guān)系。
三、參與者
1.Facade 知道哪些子系統(tǒng)類負(fù)責(zé)處理請(qǐng)求。 將客戶的請(qǐng)求代理給適當(dāng)?shù)淖酉到y(tǒng)對(duì)象。
2.Subsystemclasses 實(shí)現(xiàn)子系統(tǒng)的功能。 處理由Facade對(duì)象指派的任務(wù)。 沒(méi)有facade的任何相關(guān)信息;即沒(méi)有指向facade的指針。
四、類圖
五、示例
Facade
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Facade
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public class Facade {
ServiceA sa;
ServiceB sb;
ServiceC sc;
public Facade() {
sa = new ServiceAImpl();
sb = new ServiceBImpl();
sc = new ServiceCImpl();
}
public void methodA() {
sa.methodA();
sb.methodB();
}
public void methodB() {
sb.methodB();
sc.methodC();
}
public void methodC() {
sc.methodC();
sa.methodA();
}
}
Inerfaces
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ServiceA
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public interface ServiceA {
void methodA();
}
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ServiceB
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public interface ServiceB {
void methodB();
}
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ServiceC
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public interface ServiceC {
void methodC();
}
Subsystemclasses
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Subsystemclasses
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public class ServiceAImpl implements ServiceA{
@Override
public void methodA() {
System.out.println("這是服務(wù)A");
}
}
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Subsystemclasses
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public class ServiceBImpl implements ServiceB{
@Override
public void methodB() {
System.out.println("這是服務(wù)B");
}
}
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Subsystemclasses
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public class ServiceCImpl implements ServiceC{
@Override
public void methodC() {
System.out.println("這是服務(wù)C");
}
}
Test
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Test
* @github https://github.com/binghe001
* @copyright 公眾號(hào): 冰河技術(shù)
*/
public class Test {
public static void main(String[] args) {
ServiceA sa = new ServiceAImpl();
ServiceB sb = new ServiceBImpl();
sa.methodA();
sb.methodB();
System.out.println("========");
//facade
Facade facade = new Facade();
facade.methodA();
facade.methodB();
}
}
Result文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-725952.html
這是服務(wù)A
這是服務(wù)B
========
這是服務(wù)A
這是服務(wù)B
這是服務(wù)B
這是服務(wù)C
好了,今天就到這兒吧,相信大家對(duì)外觀模式有了更清晰的了解,我是冰河,我們下期見(jiàn)~~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-725952.html
到了這里,關(guān)于《Java極簡(jiǎn)設(shè)計(jì)模式》第08章:外觀模式(Facade)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!