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

上面這張圖呢,就違反了接口隔離原則。它對應(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類,但是只會用到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類,但是只會用到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類,但是只會用到頂級接口中的1、2、3這三個方法;而C類依賴了D類,但是只會用到頂級接口中的1、4、5這三個方法,也就是說在A和B這兩個類的層面上而言,和頂級接口中的4、5兩個方法是沒什么關(guān)聯(lián)的,那么B類在實(shí)現(xiàn)頂級接口的時候就沒必要重寫4、5這兩個方法了。但是這里有一個問題就是頂級接口中包括了1到5這五個方法,你如果實(shí)現(xiàn)這個接口就必須重寫這五個方法,那么我們就可以考慮將頂級接口拆分成多個接口,需要用到哪個就實(shí)現(xiàn)哪個,這也就是所謂的接口隔離了。
3.改進(jìn)代碼

經(jīng)過上面的一番敘述,我們可以將代碼改寫成下面的形式。
即將頂級接口拆分成3個小接口,B、D兩個類根據(jù)實(shí)際情況該實(shí)現(xiàn)哪個接口就實(shí)現(xiàn)哪個接口(因?yàn)檫@五個方法已經(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類,但是只會用到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類,但是只會用到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對于類A和類C來說不是最小接口,那么類B和類D必須去實(shí)現(xiàn)他們不需要的方法。
- 將接口Interface1拆分為獨(dú)立的幾個接口,類A和類C分別與他們需要的接口建立依賴關(guān)系。也就是采用接口隔離原則。
到此這篇關(guān)于Java設(shè)計模式之接口隔離原則精解的文章就介紹到這了,更多相關(guān)Java 接口隔離原則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis insert foreach循環(huán)插入方式
這篇文章主要介紹了mybatis insert foreach循環(huán)插入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
java數(shù)據(jù)結(jié)構(gòu)與算法之簡單選擇排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之簡單選擇排序,結(jié)合實(shí)例形式分析了選擇排序的原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
SpringBoot參數(shù)校驗(yàn)之@Valid與@Validated的用法與場景
這篇文章主要介紹了SpringBoot參數(shù)校驗(yàn)的用法與場景,在實(shí)際開發(fā)中,參數(shù)校驗(yàn)是保證接口安全性和數(shù)據(jù)完整性的重要手段,Spring Boot提供了@Valid和@Validated兩個核心注解來實(shí)現(xiàn)參數(shù)校驗(yàn),但許多開發(fā)者對它們的區(qū)別和使用場景存在疑惑,需要的朋友可以參考下2025-02-02
IntelliJ?IDEA運(yùn)行SpringBoot項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IntelliJ?IDEA如何運(yùn)行SpringBoot項(xiàng)目,步驟一配置maven,步驟二配置JDK環(huán)境,緊接著通過步驟三檢查數(shù)據(jù)庫的配置,最后一步數(shù)據(jù)庫連接,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Curator實(shí)現(xiàn)zookeeper的節(jié)點(diǎn)監(jiān)聽詳解
這篇文章主要介紹了Curator實(shí)現(xiàn)zookeeper的節(jié)點(diǎn)監(jiān)聽詳解,Curtor框架中一共有三個實(shí)現(xiàn)監(jiān)聽的方式,一種是NodeCache監(jiān)聽指定節(jié)點(diǎn),一種是pathChildrenCache監(jiān)聽子節(jié)點(diǎn),一種是TreeCache可以監(jiān)控所有節(jié)點(diǎn) 相當(dāng)于以上兩種的合集,需要的朋友可以參考下2023-12-12

