• 0x02、设计模式原则 —— 接口隔离原则


    概念

    客户端不应该依赖它不需要的接口,即:一个类对另一个类的依赖应该建立在最小的接口上;(比较难理解这句话,但不要紧,请继续看下面)

    演示

    看下图,Inerface1接口中有5个抽象方法,其中,B和D为 抽象接口Interface1 的 实现类,A和C依赖于抽象接口 Interface1:

    上图中,假设:类A 只用抽象类中的1、2、3三个方法,而4、5这两个方法类A是不需要的,而B类只用抽象接口中的1、4、5三个方法,同样2、3这两个方法类B是不需要的,我们就说,客户端A和C都有依赖了它们不需要的接口;违反了 接口隔离原则!

    上图中的代码的实现:

    interface Interface1
    {
        void operation1();
        void operation2();
        void operation3();
        void operation4();
        void operation5();
    
    }
    
    public class B : Interface1
    {
        public void operation1()
        {
            Console.WriteLine("实现类B中的func1方法");
        }
    
        public void operation2()
        {
            Console.WriteLine("实现类B中的func2方法");
        }
    
        public void operation3()
        {
            Console.WriteLine("实现类B中的func3方法");
        }
    
        public void operation4()
        {
            Console.WriteLine("实现类B中的func4方法");
        }
    
        public void operation5()
        {
            Console.WriteLine("实现类B中的func5方法");
        }
    }
    
    public class D : Interface1
    {
        public void operation1()
        {
            Console.WriteLine("实现类D中的func1方法");
        }
    
        public void operation2()
        {
            Console.WriteLine("实现类D中的func2方法");
        }
    
        public void operation3()
        {
            Console.WriteLine("实现类D中的func3方法");
        }
    
        public void operation4()
        {
            Console.WriteLine("实现类D中的func4方法");
        }
    
        public void operation5()
        {
            Console.WriteLine("实现类D中的func5方法");
        }
    }
    
    class A
    {
        // 类 A 中,通过接口 Interface1 依赖(使用)类B,但是只会用到 1、2、3 这两个方法
        public void depend1(Interface1 interf)
        {
            interf.operation1();
        }
    
        public void depend2(Interface1 interf)
        {
            interf.operation2();
        }
        public void depend3(Interface1 interf)
        {
            interf.operation3();
        }
    }
    
    
    class C
    {
        // 类 C 中,通过接口 Interface1 依赖(使用)类D,但是只会用到 1、4、5 这三个方法
        public void depend1(Interface1 interf)
        {
            interf.operation1();
        }
    
        public void depend4(Interface1 interf)
        {
            interf.operation4();
        }
        public void depend5(Interface1 interf)
        {
            interf.operation5();
        }
    }

    上面代码中,类B 实现了接口中5个方法,而 类D 也实现了接口中的5个方法,但是 类A 中,我们并没有用到 operation4 和 operation5 这两个方法,同样的,类C 中页没有用到 operation2 和 operation3 这两个方法,既然没用到,那实现类不是白写了?

    总结:上面代码中,类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法

    按隔离原则应当这样处理:将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则!

    改进:接口Interface1中出现的方法,根据实际情况拆分为三个接口:

    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.depend4(new D());
        c.depend5(new D());
    }
    
    interface Interface1
    {
        void operation1();
    }
    interface Interface2
    {
        void operation2();
        void operation3();
    
    }
    interface Interface3
    {
        void operation4();
        void operation5();
    }
    
    public class B : Interface1,Interface2
    {
        public void operation1()
        {
            Console.WriteLine("实现类B中的 operation1 方法");
        }
    
        public void operation2()
        {
            Console.WriteLine("实现类B中的 operation2 方法");
        }
    
        public void operation3()
        {
            Console.WriteLine("实现类B中的 operation3 方法");
        }
    }
    
    public class D : Interface1, Interface3
    {
    
        public void operation1()
        {
            Console.WriteLine("实现类D中的 operation1 方法");
        }
    
        public void operation4()
        {
            Console.WriteLine("实现类D中的 operation4 方法");
        }
    
        public void operation5()
        {
            Console.WriteLine("实现类D中的 operation5 方法");
        }
    }
    
    class A
    {
        // 类A 通过接口 Interfacel,Interface2 依赖(使用)类B,但是只会用到1,2,3方法
        public void depend1(Interface1 interf)
        {
            interf.operation1();
        }
    
        public void depend2(Interface2 interf)
        {
            interf.operation2();
        }
    
        public void depend3(Interface2 interf)
        {
            interf.operation3();
        }
    }
    
    
    class C
    {
        // 类C 通过接口 Interfacel,Interface3 依赖(使用)类B,但是只会用到1、4、5方法
        public void depend1(Interface1 interf)
        {
            interf.operation1();
        }
    
        public void depend4(Interface3 interf)
        {
            interf.operation4();
        }
        public void depend5(Interface3 interf)
        {
            interf.operation5();
        }
    }

     说白了就是:一个类依赖的接口中有我不需要的方法,那么我们需要将这个接口拆分成两个或多个接口,这样,拆分成多个接口之间就相当于隔离了,这就叫 接口隔离  ,我们依赖的才分后的接口就是最小了,这就叫:一个类对另一个类的依赖应该建立在最小的接口上;

    即:我们依赖的接口中如果有用不到的成员,我们就将这个接口隔离,方式就是:拆,拆成我需要的接口,这种接口就是最小的;

    这章其实不算难,如果有不懂的,欢迎评论区评论,我将会适度修改上面文章让大家都能大白话的理解它;

    下一章,我们将继续深入:依赖倒置(倒转)原则

  • 相关阅读:
    java RSA 加签验签【转】
    json遍历 分别使用【原】
    oracle 递归和connect by【转】
    css before after基本用法【转】
    Java Web基础——Action+Service +Dao三层的功能划分
    JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
    UUID
    在ssh框架中service-action-jsp-formbeam-dao的调用顺序
    Singleton模式(单例模式)
    真正理解、区分Action,Service和Dao功能
  • 原文地址:https://www.cnblogs.com/abc1069/p/16088903.html
Copyright © 2020-2023  润新知