• 特例模式(Special Case Pattern)与空对象模式(Null Pointer Pattern)—— 返回特例对象而非 null


    返回 null 值,基本上是在给自己增加工作量,也是给调用者添乱。只有一处没有检查返回的是否为 null,程序就会抛 NullPointerException 异常。

    如果你打算在方法中返回 null 值,不如:

    • 抛出异常,或者返回特例对象。
    • 如果你在调用某个第三方 api 中可能返回 null 值的方法,可以考虑用新方法中进一步打包(封装)这个方法,在更上层的代码中抛出异常或返回特例对象;

    如下的一段代码:

    List<Employee> employees = getEmployees();
    if (employees != null) {
        for (Employee employee : employees) {
            totalPay += employee.getPay();
        }
    }

    之所以这样处理,自然 getEmployees() 可能返回 null,不妨对 getEmployees 函数做进一步的封装:

    public List<Employee> getNoNullEmployees() {
        List<Employee> employees = getEmployees();
        if (employees == null) {
            return Collections.emptyList();
        }
        return employees;
    }

    1. Special Case : 特例模式

    该模式的类图如下:


    这里写图片描述

    特例类同样继承自普通基类,只是其封装的是异常处理。

    我们将如下两种不友好的(返回 null 或特殊值的异常处理),改造为特例类的处理方式:

    // 不要返回 null
    class MissingCustomer implements Customer {
        public Long getId() {
            return null;
        }
    }
    // 不要返回特殊值
    class MissingCustomer implements Customer {
        public Long getId() {
            return 0;
        }
    }

    而是采用如下的方式(抛出有意义的特殊异常):

    class MissingCustomer implements Customer {
        public Long getId() throws MissingCustomerException {
            throw new MissingCustomerException();
        }
    }

    2. 特例类的默认方法

    特例类封装了异常处理,有时当异常发生时,我们并不想让程序直接崩溃退出,而是继续运行下去。此时在特例类中,还需给出当异常情况发生时,特例实例的异常处理方式:

    class MissingSprite implements Sprite {
        public void draw() {
            // draw nothing or a generic something.
        }
    }

    references

  • 相关阅读:
    HTTP接口测试
    1 ThinkPHP MVC框架初识
    解决python2.7 scripts和pip缺失问题
    图片重绘
    Oracle数据库提权
    二阶注入
    去重算法,简单粗暴&优化版
    常用集合
    冒泡排序 -- 算法原理
    MySQL-配置环境变量及修改密码(附-mysql安装教程)
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9421045.html
Copyright © 2020-2023  润新知