首页 > 编程语言 >对于面向接口编程的理解

对于面向接口编程的理解

时间:2023-02-22 08:33:35浏览次数:25  
标签:void 编程 接口 Test static 面向 test public HttpCommunication

1:举例

  public static class HttpCommunication{
        public void communicate(){
            Log.d("test", "HttpCommunication");
        }
    }
    
    public static class Test{
        public void test(HttpCommunication httpCommunication){
            httpCommunication.communicate();
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        HttpCommunication httpCommunication = new HttpCommunication();
        test.test(httpCommunication);
    }

类HttpCommunication作为被调用者,Test作为调用者,理解为用户,这时候的参数是HttpCommunication ,通讯为http方式。

如果有一天,我不想要Http为通讯方式,想换一个socket:

public class SocketCommunication{
        public void communicate(){
            Log.d("test", "SocketCommunication");
        }
    }

对于Test类来说,即对于用户类来说,其实是不想有变化的,但是因为test方法的参数是固定的HttpCommunication,导致想要增加一种通信方式的时候

,必须修改Test类,这就违背了低耦合高内聚的原理。

以下是不使用接口的情况:

public static class HttpCommunication{
        public void communicate(){
            Log.d("test", "HttpCommunication");
        }
    }

    public static class SocketCommunication{
        public void communicate(){
            Log.d("test", "SocketCommunication");
        }
    }


    public static class Test{
        public void test(HttpCommunication httpCommunication){
            httpCommunication.communicate();
        }

        public void test2(SocketCommunication socketCommunication){
            socketCommunication.communicate();
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
       /* HttpCommunication httpCommunication = new HttpCommunication();
        test.test(httpCommunication);*/
        SocketCommunication socketCommunication = new SocketCommunication();
        test.test2(socketCommunication);
    }

 

修改为使用接口如下:最重要的就是对于Test类,即使用者来说,不关心你添加了几种使用方式,

不需要去修改Test类里面的test()方法。

test持有的是接口communication,test只关心communcation调用的结果。任何所有实现了Communication的类都可以传进来,并且

可以给我返回结果,至于过程是怎么实现的,test不关心。

可以想象成Communication相关的是一个模块,Test是一个模块,Communication里面的改变,不应该是Test有改变,才是低耦合。

 public interface Communication{
        public void communicate();
    }

    public static class HttpCommunication implements Communication{

        @Override
        public void communicate() {
            Log.d("test", "HttpCommunication");
        }
    }

    public static class SocketCommunication implements Communication{

        @Override
        public void communicate() {
            Log.d("test", "SocketCommunication");
        }
    }


    public static class Test{
        public void test(Communication communication){
            communication.communicate();
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        Communication httpCommunication = new HttpCommunication();
        test.test(httpCommunication);
        
        Communication socketCommunication = new SocketCommunication();
        test.test(socketCommunication);
    }

 

标签:void,编程,接口,Test,static,面向,test,public,HttpCommunication
From: https://www.cnblogs.com/wnpp/p/17143117.html

相关文章

  • 七十年编程语言发展漫谈
     客户端开发在多种生态中生存就要熟练使用各种平台的环境和语言。回顾自己的职业生涯,涉及多个平台生态以及对应的原生编程语言。最近又用起了Dart,经常在写代码的时候......
  • 读Java实战(第二版)笔记17_反应式编程
    1. 再次出现在聚光灯下的原因1.1. 基本思想已经有二三十年的历史1.2. 大数据1.2.1. 以PB计量的大数据1.2.2. 当前互联网中流量最大的部分是移动流量1.2.3. 物......
  • Queue接口
    queue的底层结构是队列,一般用于缓冲、并发访问queue也有size、isEmpty、contains、clear等方法更多信息参考https://blog.csdn.net/rocling/article/details/103834......
  • 28-DRF框架-搭建环境开发restful接口
    #将程序中的一个数据结构类型转换为其他格式(字典、JSON、XML等),例如将Django中的模型类对象装换为JSON字符串,这个转换过程我们称为序列化#将其他格式(字典、JSON、XML等)转......
  • 面向对象
    三大特性封装封装是把客观事物抽象成类,并且把自己的属性和方法让可信的类或对象操作,对不可性的隐藏。继承继承是指这样一种能力:它可以使用现有类的所有功能,并在无......
  • 1.7编程基础之字符串
    06:合法C标识符1.描述给定一个不包含空白符的字符串,请判断是否是C语言合法的标识符号(注:题目保证这些字符串一定不是C语言的保留字)。C语言标识符要求:1.非保留字;2.......
  • Java网络编程
    UDP和TCP网络协议,基于Socket的UDP和TCP网络编程的介绍。Author:MsuenbDate:2023-02-21网络基础知识每个计算设备上都有若干个网卡,每个网卡上有(全球唯一)单独的硬件......
  • 分层测试(三):接口测试
    分层测试系列文章https://www.cnblogs.com/yuxiuyan/tag/分层测试/1.什么是接口测试接口测试是通过测试模块接口来检测模块整体逻辑是否符合预期的测试方法。接口测试......
  • 类与接口
    类与对象类定义一种全新的数据类型,包含一组变量和函数;对象是类这种类型对应的实例。比如类相当于人类这个概念,而对象指的是人类中的每个人都是这个类中的一个对象;源文......
  • go 高级编程中一些注意点
    数组、字符串和切片三者是密切相关的数据结构,因底层都是相同的结构。go中除了闭包函数以引用的方式对外部变量访问之外,其它赋值和函数传参数都是以传值的方式处理。数......