设计模式——(三)设计模式原则___接口隔离原则

接口隔离原则

一、定义

1.客户端不应该依赖它不需要的接口。
2.类间的依赖关系应该建立在最小的接口上。

二、举例

假设我们需要满足这样的一个设计需求:
有一个接口interface1,具有五种操作方法
operation1(),operation2(),operation3(),operation4(),operation5()。
有A,B,C,D四个类,C、D实现interface1,、
A通过interface1依赖C实现,但是只会使用其中的operation1、2、3三个方法。
B通过interface1依赖D实现,但是只会使用其中的operation1、4、5三个方法。
UML图如下
在这里插入图片描述
但是我们发现,虽然C类、D类实现了interface1的所有五个方法,但是真正用到的方法却不到五个:C类只需要1、2、3三个方法、D类只需要1、4、5三个方法,其余的方法都是没有必要实现的。
代码如下:

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

class C implements Interface1{
	public void operation1() {
		System.out.println("C realized operation 1");
	}
	public void operation2() {
		System.out.println("C realized operation 2");
	};
	public void operation3() {
		System.out.println("C realized operation 3");
	};
	public void operation4() {
		System.out.println("C realized operation 4");
	};
	public void operation5() {
		System.out.println("C realized operation 5");
	};
}

class D implements Interface1{
	public void operation1() {
		System.out.println("D realized operation 1");
	}
	public void operation2() {
		System.out.println("D realized operation 2");
	};
	public void operation3() {
		System.out.println("D realized operation 3");
	};
	public void operation4() {
		System.out.println("D realized operation 4");
	};
	public void operation5() {
		System.out.println("D realized operation 5");
	};
	
class A{//A depends on C
	public void depend1(Interface1 i) {
		i.operation1();
	}
	public void depend2(Interface1 i) {
		i.operation2();
	}
	public void depend3(Interface1 i) {
		i.operation3();
	}
}

class B{//B depends on D
	public void depend1(Interface1 i) {
		i.operation1();
	}
	public void depend2(Interface1 i) {
		i.operation4();
	}
	public void depend3(Interface1 i) {
		i.operation5();
	}
}

}

我们回顾接口隔离原则的定义:
1.客户端不应该依赖它不需要的接口。
2.类间的依赖关系应该建立在最小的接口上。
为了满足这两个要求,我们将interface1拆分成3个接口。
类A和类B分别与它们需要的最小接口建立起依赖关系。
UML图如下:
在这里插入图片描述
修改过后的代码如下

//interfaces
	interface Interface1{
		void operation1();
	}
	interface Interface2{
		void operation2();
		void operation3();
	}
	interface Interface3{
		void operation4();
		void operation5();
	}
	
	class C implements Interface1,Interface2{
		public void operation1() {
			System.out.println("C realized operation 1");
		}
		public void operation2() {
			System.out.println("C realized operation 2");
		};
		public void operation3() {
			System.out.println("C realized operation 3");
		};
	}

	class D implements Interface1,Interface3{
		public void operation1() {
			System.out.println("D realized operation 1");
		}
		public void operation4() {
			System.out.println("D realized operation 4");
		};
		public void operation5() {
			System.out.println("D realized operation 5");
		};
		
	class A{//A depends on C
		public void depend1(Interface1 i) {
			i.operation1();
		}
		public void depend2(Interface2 i) {
			i.operation2();
		}
		public void depend3(Interface2 i) {
			i.operation3();
		}
	}

	class B{//B depends on D
		public void depend1(Interface1 i) {
			i.operation1();
		}
		public void depend2(Interface3 i) {
			i.operation4();
		}
		public void depend3(Interface3 i) {
			i.operation5();
		}
	}
}

有一个更为生动的例子,在面向对象六大原则——接口隔离原则中,可以参考一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值