特例模式(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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值