Java設(shè)計(jì)模式之接口隔離原則精解
1.什么是接口隔離原則?
客戶端不應(yīng)該依賴它不需要的接口,即一個(gè)類對(duì)另一個(gè)類的依賴應(yīng)該建立在最小的接口范圍上。
2.對(duì)應(yīng)代碼

上面這張圖呢,就違反了接口隔離原則。它對(duì)應(yīng)的代碼如下:??????
package com.szh.principle.segregation;
/**
*
*/
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
public void operation1() {
System.out.println("B 實(shí)現(xiàn)了 operation1");
}
public void operation2() {
System.out.println("B 實(shí)現(xiàn)了 operation2");
}
public void operation3() {
System.out.println("B 實(shí)現(xiàn)了 operation3");
}
public void operation4() {
System.out.println("B 實(shí)現(xiàn)了 operation4");
}
public void operation5() {
System.out.println("B 實(shí)現(xiàn)了 operation5");
}
}
class D implements Interface1 {
public void operation1() {
System.out.println("D 實(shí)現(xiàn)了 operation1");
}
public void operation2() {
System.out.println("D 實(shí)現(xiàn)了 operation2");
}
public void operation3() {
System.out.println("D 實(shí)現(xiàn)了 operation3");
}
public void operation4() {
System.out.println("D 實(shí)現(xiàn)了 operation4");
}
public void operation5() {
System.out.println("D 實(shí)現(xiàn)了 operation5");
}
}
class A { //A 類通過接口Interface1 依賴(使用) B類,但是只會(huì)用到1,2,3方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
class C { //C 類通過接口Interface1 依賴(使用) D類,但是只會(huì)用到1,4,5方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
public class Segregation1 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B()); // A類通過接口去依賴B類
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D()); // C類通過接口去依賴(使用)D類
c.depend4(new D());
c.depend5(new D());
}
}
代碼雖然很長,但是不難理解。A類依賴了B類,但是只會(huì)用到頂級(jí)接口中的1、2、3這三個(gè)方法;而C類依賴了D類,但是只會(huì)用到頂級(jí)接口中的1、4、5這三個(gè)方法,也就是說在A和B這兩個(gè)類的層面上而言,和頂級(jí)接口中的4、5兩個(gè)方法是沒什么關(guān)聯(lián)的,那么B類在實(shí)現(xiàn)頂級(jí)接口的時(shí)候就沒必要重寫4、5這兩個(gè)方法了。但是這里有一個(gè)問題就是頂級(jí)接口中包括了1到5這五個(gè)方法,你如果實(shí)現(xiàn)這個(gè)接口就必須重寫這五個(gè)方法,那么我們就可以考慮將頂級(jí)接口拆分成多個(gè)接口,需要用到哪個(gè)就實(shí)現(xiàn)哪個(gè),這也就是所謂的接口隔離了。
3.改進(jìn)代碼

經(jīng)過上面的一番敘述,我們可以將代碼改寫成下面的形式。
即將頂級(jí)接口拆分成3個(gè)小接口,B、D兩個(gè)類根據(jù)實(shí)際情況該實(shí)現(xiàn)哪個(gè)接口就實(shí)現(xiàn)哪個(gè)接口(因?yàn)檫@五個(gè)方法已經(jīng)被分開了)。
package com.szh.principle.segregation.improve;
/**
*
*/
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1, Interface2 {
public void operation1() {
System.out.println("B 實(shí)現(xiàn)了 operation1");
}
public void operation2() {
System.out.println("B 實(shí)現(xiàn)了 operation2");
}
public void operation3() {
System.out.println("B 實(shí)現(xiàn)了 operation3");
}
}
class D implements Interface1, Interface3 {
public void operation1() {
System.out.println("D 實(shí)現(xiàn)了 operation1");
}
public void operation4() {
System.out.println("D 實(shí)現(xiàn)了 operation4");
}
public void operation5() {
System.out.println("D 實(shí)現(xiàn)了 operation5");
}
}
class A { // A 類通過接口Interface1,Interface2 依賴(使用) B類,但是只會(huì)用到1,2,3方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
class C { // C 類通過接口Interface1,Interface3 依賴(使用) D類,但是只會(huì)用到1,4,5方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
public class Segregation2 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B()); // A類通過接口去依賴B類
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D()); // C類通過接口去依賴(使用)D類
c.depend4(new D());
c.depend5(new D());
}
}

4.接口隔離原則總結(jié)
- 類A通過接口Interface1依賴類B,類C通過接口Interfacel依賴類D,如果接口Interface1對(duì)于類A和類C來說不是最小接口,那么類B和類D必須去實(shí)現(xiàn)他們不需要的方法。
- 將接口Interface1拆分為獨(dú)立的幾個(gè)接口,類A和類C分別與他們需要的接口建立依賴關(guān)系。也就是采用接口隔離原則。
到此這篇關(guān)于Java設(shè)計(jì)模式之接口隔離原則精解的文章就介紹到這了,更多相關(guān)Java 接口隔離原則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PowerJob的ProcessorLoader工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的ProcessorLoader工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
簡(jiǎn)單了解Spring IoC相關(guān)概念原理
這篇文章主要介紹了簡(jiǎn)單了解Spring IoC相關(guān)概念原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Windows10 Java環(huán)境變量配置過程圖解
這篇文章主要介紹了Windows10 Java環(huán)境變量配置過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

