• 设计模式之接口分离原则


    定义:

    以上截图的uml图已添加注释,代码实现如下:

    
    
    public class Segregation {
    public static void main(String[] args) {
    A a = new A();
    B b = new B();
    C c = new C();
    D d = new D();
    a.useInterface1(b);
    a.useInterface2(b);
    a.useInterface3(b);
    c.useInterface1(d);
    c.useInterface4(d);
    c.useInterface5(d);
    }
    }

    interface Interface1{
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
    }


    class B implements Interface1{

    @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");
    }

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

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


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

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

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

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

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

    class A {
    public void useInterface1(Interface1 interface1){
    interface1.operation1();
    }
    public void useInterface2(Interface1 interface1){
    interface1.operation2();
    }
    public void useInterface3(Interface1 interface1){
    interface1.operation3();
    }
    }

    class C {
    public void useInterface1(Interface1 interface1){
    interface1.operation1();
    }
    public void useInterface4(Interface1 interface1){
    interface1.operation4();
    }
    public void useInterface5(Interface1 interface1){
    interface1.operation5();
    }
    }
     

    以上代码的问题在于A类只是用接口中的1,2,3方法,C类只使用接口中的1,4、5方法。所以B类其实只要实现1,2,3方法。D类只要实现1,4,5方法。

     解决办法:

    修改后的类图:

    接口需要拆分成3个接口, 代码实现如下:

    public class Segregation {
        public static void main(String[] args) {
            A a = new A();
            B b = new B();
            C c = new C();
            D d = new D();
            a.useInterface1(b);
            a.useInterface2(b);
            a.useInterface3(b);
            c.useInterface1(d);
            c.useInterface4(d);
            c.useInterface5(d);
        }
    }
    
    interface Interface1{
        void operation1();
    }
    interface Interface2 {
        void operation2();
        void operation3();
    }
    interface Interface3{
        void operation4();
        void operation5();
    }
    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");
        }
        @Override
        public void operation5() {
            System.out.println("D实现operation5");
        }
    }
    
    class A {
        public void useInterface1(Interface1 interface1){
            interface1.operation1();
        }
        public void useInterface2(Interface2 interface2){
            interface2.operation2();
        }
        public void useInterface3(Interface2 interface2){
            interface2.operation3();
        }
    }
    
    class C {
        public void useInterface1(Interface1 interface1){
            interface1.operation1();
        }
        public void useInterface4(Interface3 interface3){
            interface3.operation4();
        }
        public void useInterface5(Interface3 interface3){
            interface3.operation5();
        }
    }
     
  • 相关阅读:
    剑指offer---第一个只出现一次的字符
    剑指offer---两个链表的第一个公共结点
    剑指offer---丑数
    剑指offer---旋转数组的最小数字
    剑指offer---滑动窗口的最大值
    剑指offer---重建二叉树
    剑指offer---数据流中的中位数
    剑指offer---二叉搜索树的第K个节点
    剑指offer--对称二叉树
    剑指offer---把二叉树打印成多行
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/12374556.html
Copyright © 2020-2023  润新知