设计模式:七大原则之接口隔离原则

345 阅读3分钟

基本介绍

  • 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
  • 类A通过接口Interface1依赖B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不许套的方法
  • 按照接口隔离原则应该这样处理:将接口Interface1拆分为独立的几个接口(这里拆分成3个接口),类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则 如下图:

应用实例

package com.braveway.principle.segregation;

//没有使用接口隔离原则的代码
public class Segregation1 {

	public static void main(String[] args) {
		
	}
}

//接口
interface Interface1{
	void operation1();
	void operation2();
	void operation3();
	void operation4();
	void operation5();
}

class B implements Interface1{

	@Override
	public void operation1() {
		System.out.println("B 实现了 operation1");
	}

	@Override
	public void operation2() {
		System.out.println("B 实现了 operation2");
	}

	@Override
	public void operation3() {
		System.out.println("B 实现了 operation3");
	}

	@Override
	public void operation4() {
		System.out.println("B 实现了 operation4");
	}

	@Override
	public void operation5() {
		System.out.println("B 实现了 operation5");
	}
	
}


class D implements Interface1{

	@Override
	public void operation1() {
		System.out.println("D 实现了 operation1");
	}

	@Override
	public void operation2() {
		System.out.println("D 实现了 operation2");
	}

	@Override
	public void operation3() {
		System.out.println("D 实现了 operation3");
	}

	@Override
	public void operation4() {
		System.out.println("D 实现了 operation4");
	}

	@Override
	public void operation5() {
		System.out.println("D 实现了 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();
	}
}

存在的问题及改进

  • 类A通过接口Interface1依赖类B,类C通过接口Interface1依赖D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法
  • 将接口Interface1拆分为独立的几个接口,类A和类C分别于他们需要的接口建立依赖关系。也就是采用接口隔离原则
  • 将几口Interface1中的出现的方法,根据实例情况拆分为三个接口 如下图:

改进后实例

package com.braveway.principle.segregation.improve;

//根据接口隔离原则改进后的代码
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());
	}
}

//接口
interface Interface1{
	void operation1();
	
}

interface Interface2{
	void operation2();
	void operation3();
}

interface Interface3{
	void operation4();
	void operation5();
}




class B implements Interface1,Interface2{

	@Override
	public void operation1() {
		System.out.println("B 实现了 operation1");
	}

	@Override
	public void operation2() {
		System.out.println("B 实现了 operation2");
	}

	@Override
	public void operation3() {
		System.out.println("B 实现了 operation3");
	}
	
}


class D implements Interface1,Interface3{

	@Override
	public void operation1() {
		System.out.println("D 实现了 operation1");
	}

	@Override
	public void operation4() {
		System.out.println("D 实现了 operation4");
	}

	@Override
	public void operation5() {
		System.out.println("D 实现了 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();
	}
}