zoukankan      html  css  js  c++  java
  • 设计模式之接口分离原则

    定义:

    以上截图的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();
        }
    }
     
  • 相关阅读:
    【转载】兼容php5,php7的cURL文件上传示例
    解决CURL 请求本地超时
    PHP 二维数组根据某个字段排序
    JS监听输入框值变化兼容 onpropertychange、oninput
    PHP AES的加密解密-----【弃用】
    PHP 开发API接口签名验证
    Python 逐行修改txt每条记录的内容
    Python 修改电脑DNS
    带小数点时间分钟转换
    Python 判断字符串是否为数字
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/12374556.html
Copyright © 2011-2022 走看看