图解java设计模式_07_接口隔离原则

2024-04-20 15:18

本文主要是介绍图解java设计模式_07_接口隔离原则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一个类对另一个类的依赖应该建立在最小的接口上

package com.atguigu.principle.segregation;public class Segregation1 {public static void main(String[] args) {// TODO Auto-generated method stub}}//接口
interface Interface1 {void operation1();void operation2();void operation3();void operation4();void operation5();
}class B implements Interface1 {public void operation1() {System.out.println("B 实现了 operation1");}public void operation2() {System.out.println("B 实现了 operation2");}public void operation3() {System.out.println("B 实现了 operation3");}public void operation4() {System.out.println("B 实现了 operation4");}public void operation5() {System.out.println("B 实现了 operation5");}
}class D implements Interface1 {public void operation1() {System.out.println("D 实现了 operation1");}public void operation2() {System.out.println("D 实现了 operation2");}public void operation3() {System.out.println("D 实现了 operation3");}public void operation4() {System.out.println("D 实现了 operation4");}public void operation5() {System.out.println("D 实现了 operation5");}
}
//A 类通过接口Interface1 依赖(使用) B类,但是只会用到1,2,3方法
class A { public void depend1(Interface1 i) {i.operation1();}public void depend2(Interface1 i) {i.operation2();}public void depend3(Interface1 i) {i.operation3();}
}//C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
class C { public void depend1(Interface1 i) {i.operation1();}public void depend4(Interface1 i) {i.operation4();}public void depend5(Interface1 i) {i.operation5();}
}

改进后

package com.atguigu.principle.segregation.improve;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());a.depend3(new B());C c = new C();c.depend1(new D()); // C类通过接口去依赖(使用)D类c.depend4(new D());c.depend5(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 {public void operation1() {System.out.println("B 实现了 operation1");}public void operation2() {System.out.println("B 实现了 operation2");}public void operation3() {System.out.println("B 实现了 operation3");}}class D implements Interface1, Interface3 {public void operation1() {System.out.println("D 实现了 operation1");}public void operation4() {System.out.println("D 实现了 operation4");}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();}
}

 

这篇关于图解java设计模式_07_接口隔离原则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/920602

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内