鱼C论坛

 找回密码
 立即注册
查看: 2673|回复: 1

[学习笔记] 接口隔离原则

[复制链接]
发表于 2019-7-14 15:41:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


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

应用实例
    1.类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,请编写代码完成此应用实例。
    2.看老师代码

  1. public class Segregation1{
  2.     public static void main(String[]args){

  3.     }
  4. }

  5. interface Interface1{
  6.     void operation1();
  7.     void operation2();
  8.     void operation3();
  9.     void operation4();
  10.     void operation5();
  11. }

  12. class B implements Interface1{
  13.     public void operation1(){
  14.         System.out.println("B 实现了operation1");
  15.     }
  16.     public void operation2(){
  17.         System.out.println("B 实现了operation2");
  18.     }
  19.     public void operation3(){
  20.         System.out.println("B 实现了operation3");
  21.     }
  22.     public void operation4(){
  23.         System.out.println("B 实现了operation4");
  24.     }
  25.     public void operation5(){
  26.         System.out.println("B 实现了operation5");
  27.     }
  28. }
  29. class D implements Interface1{
  30.     public void operation1(){
  31.         System.out.println("D 实现了operation1");
  32.     }
  33.     public void operation2(){
  34.         System.out.println("D 实现了operation2");
  35.     }
  36.     public void operation3(){
  37.         System.out.println("D 实现了operation3");
  38.     }
  39.     public void operation4(){
  40.         System.out.println("D 实现了operation4");
  41.     }
  42.     public void operation5(){
  43.         System.out.println("D 实现了operation5");
  44.     }
  45. }

  46. class A{
  47.     public void depend1(Interface1 i){
  48.         i.operation1();
  49.     }
  50.     public void depend2(Interface1 i){
  51.         i.operation2();
  52.     }
  53.     public void depend3(Interface1 i){
  54.         i.operation3();
  55.     }
  56. }


  57. class C{
  58.     public void depend1(Interface1 i){
  59.         i.operation1();
  60.     }
  61.     public void depend4(Interface1 i){
  62.         i.operation4();
  63.     }
  64.     public void depend5(Interface1 i){
  65.         i.operation5();
  66.     }
  67. }
复制代码


应用传统方法的问题和使用接口隔离原则改进
    1.类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1
    对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。
    2.将接口Interface1拆分为独立的几个接口,类A和类C分别于他们需要的接口建立依赖关系,也就是采用接口隔离原则
    3.接口Interface1中出现的方法,根据实际情况拆分为三个接口
    4.代码实现


  1. interface Interface1{
  2.     void operation1();
  3. }

  4. interface Interface2{
  5.     void operation2();
  6.     void operation3();
  7. }

  8. interface Interface3{
  9.     void operation4();
  10.     void operation5();
  11. }

  12. class B implements Interface1,Interface2{
  13.     public void operation1(){
  14.         System.out.println("B 实现了operation1");
  15.     }
  16.     public void operation2(){
  17.         System.out.println("B 实现了operation2");
  18.     }
  19.     public void operation3(){
  20.         System.out.println("B 实现了operation3");
  21.     }
  22.     public void operation4(){
  23.         System.out.println("B 实现了operation4");
  24.     }
  25.     public void operation5(){
  26.         System.out.println("B 实现了operation5");
  27.     }
  28. }
  29. class D implements Interface1,Interface3{
  30.     public void operation1(){
  31.         System.out.println("D 实现了operation1");
  32.     }
  33.     public void operation2(){
  34.         System.out.println("D 实现了operation2");
  35.     }
  36.     public void operation3(){
  37.         System.out.println("D 实现了operation3");
  38.     }
  39.     public void operation4(){
  40.         System.out.println("D 实现了operation4");
  41.     }
  42.     public void operation5(){
  43.         System.out.println("D 实现了operation5");
  44.     }
  45. }

  46. class A{
  47.     public void depend1(Interface1 i){
  48.         i.operation1();
  49.     }
  50.     public void depend2(Interface2 i){
  51.         i.operation2();
  52.     }
  53.     public void depend3(Interface2 i){
  54.         i.operation3();
  55.     }
  56. }


  57. class C{
  58.     public void depend1(Interface1 i){
  59.         i.operation1();
  60.     }
  61.     public void depend4(Interface3 i){
  62.         i.operation4();
  63.     }
  64.     public void depend5(Interface3 i){
  65.         i.operation5();
  66.     }
  67. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-23 09:42:03 | 显示全部楼层
学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 18:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表