设计模式——接口隔离原则

文章目录

基本介绍

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

应用实例

  1. 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,请编写代码完成此应用实例。
  2. 看代码-没有使用接口隔离原则代码
java 复制代码
public class Segregation1 {
	public static void main(String[] args) {
		
		A a = new A();
		a.depend1(new B());
		//无法调用,即B中的 方法 白写了,造成了浪费.
		a.depend4(new B());
		
	}
}

interface Interface1 {

	void operation1();

	void operation2();

	void operation3();

	void operation4();

	void operation5();
}

class B implements Interface1 {

	@Override
	public void operation1() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation1");
	}

	@Override
	public void operation2() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation2");
	}

	@Override
	public void operation3() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation3");
	}

	@Override
	public void operation4() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation4");
	}

	@Override
	public void operation5() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation5");
	}

}

class D implements Interface1 {

	@Override
	public void operation1() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation1");
	}

	@Override
	public void operation2() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation2");
	}

	@Override
	public void operation3() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation3");
	}

	@Override
	public void operation4() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation4");
	}

	@Override
	public void operation5() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation5");
	}

}

class A { // A类通过接口 依赖(使用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类通过接口 依赖(使用B类) 但是只会用到1,4,5方法
	public void depend1(Interface1 i) {
		i.operation1();
	}

	public void depend2(Interface1 i) {
		i.operation5();
	}

	public void depend3(Interface1 i) {
		i.operation5();
	}

}

应传统方法的问题和使用接口隔离原则改进

  1. 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法
  2. 将接口 Interface1 拆分为独立的几个接口,类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
  3. 接口 Interface1 中出现的方法,根据实际情况拆分为三个接口
  1. 代码实现
java 复制代码
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.depend2(new D());
		c.depend3(new D());
	}
}

//接口1
interface Interface1 {

	void operation1();

}

//接口2
interface Interface2 {

	void operation2();

	void operation3();
}

//接口3
interface Interface3 {

	void operation4();

	void operation5();

}

class B implements Interface1, Interface2 {

	@Override
	public void operation1() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation1");
	}

	@Override
	public void operation2() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation2");
	}

	@Override
	public void operation3() {
		// TODO Auto-generated method stub
		System.out.println("B中实现了 operation3");
	}

}

class D implements Interface1, Interface3 {

	@Override
	public void operation1() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation1");
	}

	@Override
	public void operation4() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation4");
	}

	@Override
	public void operation5() {
		// TODO Auto-generated method stub
		System.out.println("D中实现了 operation5");
	}

}

class A { // A类通过接口 依赖(使用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类通过接口 依赖(使用B类) 但是只会用到1,4,5方法
	public void depend1(Interface1 i) {
		i.operation1();
	}

	public void depend2(Interface3 i) {
		i.operation4();
	}

	public void depend3(Interface3 i) {
		i.operation5();
	}

}

不至于造成资源浪费!

相关推荐
不吃香菜学java3 小时前
Redis的java客户端
java·开发语言·spring boot·redis·缓存
captain3763 小时前
事务___
java·数据库·mysql
北漂Zachary3 小时前
四大编程语言终极对比
android·java·php·laravel
小江的记录本3 小时前
【网络安全】《网络安全常见攻击与防御》(附:《六大攻击核心特性横向对比表》)
java·网络·人工智能·后端·python·安全·web安全
嗑嗑嗑瓜子的猫4 小时前
Java!它值得!
java·开发语言
回忆2012初秋4 小时前
工厂方法模式完整实现:GPS转换
设计模式·工厂方法模式
2401_895521345 小时前
【Spring Security系列】Spring Security 过滤器详解与基于JDBC的认证实现
java·后端·spring
皮卡蛋炒饭.6 小时前
线程的概念和控制
java·开发语言·jvm
一只大袋鼠6 小时前
MyBatis 入门详细实战教程(一):从环境搭建到查询运行
java·开发语言·数据库·mysql·mybatis
程序员老邢6 小时前
【人生底稿・番外篇 05】我的电影江湖:从录像带时代,到港片陪伴的青春岁月
java·程序人生·职场发展·娱乐