美文网首页六大原则Android知识Android开发
走向面向对象的六大原则--接口隔离原则

走向面向对象的六大原则--接口隔离原则

作者: 孑然自安 | 来源:发表于2016-09-19 18:42 被阅读374次

    面向对象编程的六大原则


    让项目拥有变化的能力--接口隔离原则
    接口隔离原则的英文全称是:InterfaceSegregation Principles,缩写是ISP
    定义是:客户端不应该依赖它不需要的接口。

    这里我们同样回到之前的代码,在之前的DiskCache中,我们有将图片的outputStream关闭的操作,如下:

    public void put(String url,Bitmap bitmap){
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(url);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            // 进行判断
            if (outputStream!=null){
                try {
                    // 关闭输出流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    这段代码的可读性非常差,各种try...catch相互嵌套,如果一行代码写在了错误的方法体内,那就。。相信大家对于这样的代码也是非常反感的,那么,如何解决它呢?
    在Java中有一个接口名为Closeable,该接口标识了一个可以被关闭的对象,它只有一个close方法,代码会在下面贴出,我们现在所使用的FileOutputStream就实现了接口,除此之外,还有一百多个类都实现了这个接口,这就意味着,你在使用这相关的一百多个类的时候,都必须要执行close方法,不觉得这样很难忍受吗?反正我是忍不了。我们先看看FileOutputStream和Closeable的代码:

    // Closeable 
    public interface Closeable extends AutoCloseable {    
        void close() throws IOException;
    }
    
    // OutputStream 
    public abstract class OutputStream implements Closeable, Flushable {
                    public OutputStream() {
                        throw new RuntimeException("Stub!");
                    }
    
                    public abstract void write(int var1) throws IOException;
    
                    public void write(byte[] b) throws IOException {
                        throw new RuntimeException("Stub!");
                    }
    
                    public void write(byte[] b, int off, int len) throws IOException {
                        throw new RuntimeException("Stub!");
                    }
    
                    public void flush() throws IOException {
                        throw new RuntimeException("Stub!");
                    }
    
                    public void close() throws IOException {
                        throw new RuntimeException("Stub!");
                    }
                }
    

    具体的代码实现不是我们所关心的问题,我们所在意的是,如何去解决这个麻烦?在此处,我新建了一个工具类去完成统一关闭实现此接口的类的功能,代码如下:

    public class CloseUtils {
        
        public static void close(Closeable closeable) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    实际使用中是这样的:

    public void put(String url, Bitmap bitmap) {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(url);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            CloseUtils.colse(outputStream );
        }
    }
    

    这样一来代码是不是简洁了很多?并且定义的工具类可以运用到各种需要进行关闭的对象中,保证了代码的重用性。此处的工具类中的方法close(Closeable closeable)不正体现了我们在依赖倒置原则中所强调的,尽量以来抽象而不是具体实现。同时呢,该方法所需要知道的仅仅只是这个类是可以关闭的,其他的什么都不需要关心,这也就是我们本章所讲述的接口隔离原则


    写在最后#

    我们的Bob(Robert C Martin)大叔,将单一原则、开闭原则、里氏替换原则、接口隔离原则、依赖倒置原则五个原则定义为SOLID原则,当这五个原则贯彻在你的代码中时,可以使得项目更清晰、简单、同时拥有最大程度的拓展性。在开发中如何使用,如何规范,那就是各位同仁需要思考的问题了。
    学而不思则罔,在明天结束的迪米特原则后,我们的走向面向对象的六大原则主题就要结束了。这段时间作者在写博客的时候也收获很多,认识了很多新朋友,交流了蛮多新奇的想法,同时也希望各位读者在评论区畅所欲言,作者会一一答复的。
    好的,今天就到这里,谢谢!

    相关文章

      网友评论

      • KnifeStone:感谢分享
      • Aldrich_N:这不是就是换了个地方么?
        Aldrich_N: @YeziCN 嗯,谢谢
        孑然自安: @Aldrich_N 你说的很对,从代码层次上是这样的,本章内容主讲接口隔离原则,我是通过该示例解释的。

      本文标题:走向面向对象的六大原则--接口隔离原则

      本文链接:https://www.haomeiwen.com/subject/xpenettx.html