天天看點

Java設計模式七大原則之接口隔離原則

Java設計模式七大原則之接口隔離原則

基本介紹

用戶端不應該依賴在它不需要使用的接口,即一個類對另一個類的依賴應該建立在最小接口上

類圖

Java設計模式七大原則之接口隔離原則

其中A類使用了接口中1,2,3這三種方法,C類使用了1,4,5這三種方法

未使用接口隔離原則應的代碼

public class Segregation {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A a=new A();
		a.depend1(new B());
		a.depend2(new B());
		a.depend3(new B());
		C c=new C();
		c.depend1(new D());
		c.depend4(new D());
		c.depend5(new D());
	}

}

interface Interface1 {
	void operation1();

	void operation2();

	void operation3();

	void operation4();

	void operation5();
}

class B implements Interface1 {
	public void operation1() {
		System.out.println("B中實作了操作1");
	}

	public void operation2() {
		System.out.println("B中實作了操作2");
	}

	public void operation3() {
		System.out.println("B中實作了操作3");
	}

	public void operation4() {
		System.out.println("B中實作了操作4");
	}

	public void operation5() {
		System.out.println("B中實作了操作5");
	}
}

class D implements Interface1 {
	public void operation1() {
		System.out.println("D中實作了操作1");
	}

	public void operation2() {
		System.out.println("D中實作了操作2");
	}

	public void operation3() {
		System.out.println("D中實作了操作3");
	}

	public void operation4() {
		System.out.println("D中實作了操作4");
	}

	public void operation5() {
		System.out.println("D中實作了操作5");
	}
}
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();
	}
           

根據類圖寫的代碼小結:

A和C在通過接口依賴B和D時,A隻使用了1,2,3方法,C隻使用了1,4,5方法,隻用一個接口會讓類B,D去實作他們根本不需要使用的方法,是以需要将Interface1拆分為幾個獨立的接口,類A和類C分别與他們需要的接口建立依賴關系。也就是采用接口隔離原則。

使用接口隔離原則做的改進

public class Segregation1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A a=new A();
		a.depend1(new B());//a 類通過接口去依賴B類
		a.depend2(new B());
		C b=new C();
		b.depend1(new D());
		b.depend2(new D());//c類通過接口去依賴D類
	}
	
}
 
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 類實作了操作1");
	}
	public void operation2() {
		System.out.println("B 類實作了操作2");
	}
	public void operation3() {
		System.out.println("B 類實作了操作3");
	}
}
class D implements Interface1,Interface3{
	public void operation1() {
		System.out.println("D類實作了操作1");
	}
	public void operation4() {
		System.out.println("D類實作了操作4");
	}
	public void operation5() {
		System.out.println("D類實作了操作5");
	}
}
class A{
	public void depend1(Interface1 i) {
		i.operation1();
	}
	public void depend2(Interface2 i) {
		i.operation2();
		i.operation3();
	}
}
class C{
	public void depend1(Interface1 i) {
		i.operation1();
	}
	public void depend2(Interface3 a) {
		a.operation4();
		a.operation5();
	}
}