java设计模式学习笔记--接口隔离原则
接口隔离原则简述
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应建立在最小的接口上
应用场景
如下UML图

类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要实现的方法
按照接口隔离原则,将接口Interface1拆分成若干个独立的接口(如图可得拆分成3个),类A和类C分别与他们需要的接口建立依赖关系
应用实例
没有使用接口隔离原则
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{
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("B实现了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{ //A类通过接口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依赖于类B时,不需要实现接口Interface1中的operation4()和operation5()的方法,类C依赖于类D时,不需要实现operation2()和operation3()的方法,此时Interface1不是类A和类C的最小接口
使用接口隔离原则
public class Segregation2 {
public static void main(String[] args) {
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());
}
}
//接口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("B实现了operation5");
}
}
class A{ //A类通过接口interface1 依赖(使用)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{ //A类通过接口interface1 依赖(使用)D类,但是只会使用 1,4,53的方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
类A依赖类B实现operation1(),operation2()和operation3()方法,
类C依赖类D实现operation1(),operation4()和operation5()方法。
根据接口隔离原则,将原本的Interface1拆分成三个接口,其中
新的接口Interface1中拥有operation1()方法,
接口Interface2中拥有operation2()和operation3()方法,
接口Interface3中拥有operation4()和operation5()方法。
让类B继承接口Interface1和接口Interface2,
让类D继承接口Interface1和接口Interface3。
这样,就满足接口隔离原则了。
具体关系如下UML图

下一篇链接(java设计模式学习笔记--依赖倒转原则):
https://www.cnblogs.com/windowsxpxp/p/11568009.html
java设计模式学习笔记--接口隔离原则的更多相关文章
- java设计模式学习笔记--单一职责原则
单一职责原则注意事项和细节 1.降低类的复杂度,一个类只负责一项职责 2.提高可读性,可维护性 3.降低变更引起的风险 4.通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级违反单 ...
- java设计模式学习笔记--依赖倒转原则
依赖倒转原则简述 1.高层模块不应该依赖低层模块,二者都应该依赖其抽象 2.抽象不应该依赖细节,细节应该依赖抽象 3.依赖倒转得中心思想时面向接口编程 4.依赖倒转原则时基于这样得设计理念:相对于细节 ...
- java设计模式学习笔记--开闭原则
基本介绍 1.开闭(ocp)原则时编程中最基础.最重要的设计原则 2.一个软件实体如类.木块和函数应该对扩展开放,对修改关闭.用抽象构建框架,用实现扩展细节.即对提供方开放,对使用方关闭. 3.当软件 ...
- java设计模式学习笔记——里氏替换原则
oo中的继承性的思考和说明 1.继承包含这样一层含义:父类中凡是已经实现好的方法,实际上是在设定规范和契约,虽然它不强制要求所有的子类必须遵循这些七月,但是如果子类对这些已经实现的方法任意修改,就会对 ...
- java设计模式学习笔记--浅谈设计模式
设计模式的目的 编写软件的过程中,程序员面临着来自耦合性,内聚性以及可维护性,可扩展性,重用性,灵活性等多方面的挑战.设计模式为了让程序具有更好的 1.代码重用性(即:相同功能的代码,不用多次编写) ...
- Java设计模式学习笔记(四) 抽象工厂模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 抽象工厂模式概述 工厂方法模式通过引入工厂等级结构,解决了简单工厂模式中工厂类职责太重的问 ...
- Java设计模式学习笔记(二) 简单工厂模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 正文开始... 1. 简介 简单工厂模式不属于GoF23中设计模式之一,但在软件开发中应用也较为 ...
- Java设计模式学习笔记(三) 工厂方法模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 简介 上一篇博客介绍了简单工厂模式,简单工厂模式存在一个很严重的问题: 就是当系统需要引入 ...
- Java设计模式学习笔记(五) 单例模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 使用单例模式的原因 以Windows任务管理器为例,在Windows系统中,任务管理器是唯 ...
随机推荐
- pycharm 选中单列快捷键
直接键入 Alt + Shift +Insert 一次后就可以选中单列 再次键入就改回选中整行 如图: 第一键入 第二次键入
- JQuery--50个必备的实用jQuery代码段.
原文出处:http://my.oschina.net/chengjiansunboy/blog/55496?p=2#comments 1. 如何修改jQuery默认编码(例如默认UTF-8改成改GB2 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- webpack--运行npm run dev自动打开浏览器运行首页的两种方式以及热加载
作为开发人员,我们在修改了代码之后,在vscode终端运行npm run dev指令后,希望它可以自动打开浏览器方便我们调试,有两种方式可以实现: 自动打开浏览器的两种方式: 方式一: 1.webpa ...
- SDL多线程显示更新窗口
//初始化SDL2和创建一个窗口,并且将屏幕绘制成大红色 #include <iostream> extern "C" { #include <SDL.h> ...
- [xml]AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'
>>> import requests >>> res = requests.get("https://xxx.com/sitemap.xml" ...
- PySpark Rdd Cheat Sheet Python
- ncbi-blast 本地安装
详见:http://blog.shenwei.me/local-blast-installation/ Linux系统中NCBI BLAST+本地化教程 本文面向初学者(最好还是懂得基本的linux使 ...
- 最简单的windows 10 软路由
因为轻信了 小米路由器3潘多拉固件刷机教程 年前把自己的小米路由器3pro 刷程砖了,然后自己有一台 i5256 的三众小主机,连在电信光猫上,可以拨号,勉强可以用,but 家里的设备那么多尤其手机笔 ...
- 简化 Spring Boot 项目部署,Flyway 搞起来
虽然我之前录了一个微人事(https://github.com/lenve/vhr)部署视频(新版微人事部署教程来啦),但是由于这次升级涉及到了 Redis 和 RabbitMQ,所以在本地跑微人事还 ...