接口隔离原则2-Java设计模式

2 928

接口隔离原则演示

package src.Sefrefation;
/* 
    接口隔离原则:一个类不能依赖于它不使用的方法,因此要把一个大的接口拆分成小的接口
    和单一职责原则类似,不同的是一个关注的是类粒度拆分,一个是对接口粒度拆分。
*/
public class Sefrefation2 {
    interface interface1 {
        void operation1();
       
        
    }
    interface interface2 {
        void operation2();
        void operation3();
        
    }
    interface interface3 {
        void operation4();
        
    }
    class B implements interface1,interface2{

        @Override
        public void operation1() {
            System.out.println("B 实现了 operation1");
            
        }

        @Override
        public void operation2() {
            System.out.println("B 实现了 operation2");
            
        }

        @Override
        public void operation3() {
            System.out.println("B 实现了 operation3");
            
        }
        
    }
    class D implements interface1,interface3{

        @Override
        public void operation1() {
            System.out.println("D 实现了 operation1");
            
        }

        @Override
        public void operation4() {
            System.out.println("D 实现了 operation4");
            
        }
        
    }
    class A {
        public void depend1(interface1 i) {
            i.operation1();
        }
        public void depend2(interface2 i) {
            i.operation2();
        }
        public void depend3(interface2 i) {
            i.operation3();
        }
    }
    class C{
        public void depend1(interface1 i) {
            i.operation1();
        }
        public void depend4(interface3 i) {
            i.operation4();
        }
        public void depend5(interface3 i) {
            i.operation4();
        }
    }
}



Prev Post 接口隔离原则1-传统接口的使用方法-Java设计模式
Next Post 依赖倒转原则-通过构造器传入依赖-Java设计模式