在Joshua Bloch很有名的一本书《Effective in java》中建议不要在代码中返回空的collection/map/array,就像下面的代码一样:

public List<String> returnCollection() {
//remainder omitted
if (/*some condition*/) {
return null;
} else {
// return collection
}
} 而应该使用下面的模式:
public List<String> returnCollection() {
//remainder omitted
if (/*some condition*/) {
return Collections.emptyList();
} else {
// return collection
}
} 这种模式可以防止像下面这种方式调用你的代码抛出null pointer
  1. if (obj.returnCollection().size() > 0) {  
  2. // remainder omitted  

在Robert C. Martin《敏捷软件开发,原则,模式,实践》一书中给了一个类似的模式,它包含了所有的对象,不仅仅只针对collections/maps/arrays。这个模式被称作Null Object。

这里有一个实例,假定你有一个应用程序需要检查用户是否认证过。

public class User {
private String username;
private boolean authenticated;
// remainder omitted public boolean isAuthenticated() {
return authenticated;
}
// remainder omitted
} 代码像下面这样返回一个User对象的引用,
public User getUser() {
if (/*some condition*/) {
return user;
} else {
return null;
}
} 这种方式下,检查User是否认证过需要用下面的代码才可以: if (obj.getUser() != null && obj.getUser().isAuthenticated() {
        // allow  
}
// remainder omitted
上面检查对象是否为空并不只是一个样板代码,当你忘记检查对象是否为null时,就会引起bug. 这里Null Object 模式可以帮助你:
public class NullUser extends User {

    public static final NullUser INSTANCE = new NullUser();

    public static NullUser getInstance() {
return INSTANCE;
} @Override
public boolean isAuthenticated() {
return false;
} private NullUser() {
}
}
public User getUser() {
if (/*some condition*/) {
return user;
} else {
return NullUser.getInstance();
}
}

if (obj.getUser().isAuthenticated() {
// allow
}
// remainder omitted

我发现这种模式确实很管用,它避免了很多的Null pointer异常。这里仍然有一个问题,User应用是一个类还应该是一个接口;NullUser是继承一个基类还是实现一个接口,把这个问题留给你去

考虑。

你是如何思考Null Object模式的?

翻译自:http://blog.bielu.com/2008/12/null-object-design-pattern.html

转载请注明出处。





 
 

 

Null Pointer --设计模式的更多相关文章

  1. c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.

    // Note: //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of ...

  2. differences between null pointer and void pointer.

    These are two different concepts, you cannot compare them. What the difference between the skunk and ...

  3. leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...

  4. A pointer is a variable whose value is the address of another variable 指针 null pointer 空指针 内存地址0 空指针检验

    小结: 1.指针的实际值为代表内存地址的16进制数: 2.不同指针的区别是他们指向的变量.常量的类型: https://www.tutorialspoint.com/cprogramming/c_po ...

  5. 力扣 报错 runtime error: load of null pointer of type 'const int'

    runtime error: load of null pointer of type 'const int' 要求返回的是int* 解决方案 1.指针使用malloc分配空间 用 int * p = ...

  6. Unable to handle kernel NULL pointer dereference at virtual address 00000000问题的解决

    今天在编译好内核模块后,安装内核模块memdev.ko的时候,出现了Unable to handle kernel NULL pointer dereference at virtual addres ...

  7. Unable to handle kernel NULL pointer dereference at virtual address 00000000【转】

    本文转载自:https://blog.csdn.net/hpu11/article/details/72628052 这说明是非法指针的使用,才导致系统出错. [ 1023.510000] Unabl ...

  8. spring quartz job autowired 出错 null pointer

    什么情况下才能用autowired? 当当前类属于spring IOC容器管时候就可以,比如在applicationContext.xml里有定义 就是说在spring上下文里能够找到 但是比如qua ...

  9. java NPE就是空指针异常,null pointer exception

    java NPE就是空指针异常,null pointer exception

随机推荐

  1. js 类似发微博或者微信朋友圈的时间显示 刚刚 几天前

    群里的一个小伙伴(NightEagle)写的,共享出来了,我就做个记录. function getDateDiff(dateStr) { var publishTime = getDateTimeSt ...

  2. POJ3155 Hard Life

    Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 8482   Accepted: 2461 Case Time Limit:  ...

  3. Oracle- 用户管理

    Oracle一个数据库里可以分配多个用户,用户创建自己的表,自己创建的表如果不想分配给其他用户使用,其他用户是看不到自己的创建的表的. 用户管理: 创建用户: create user chunxiao ...

  4. java中用线程解决进出水问题

    //进水 class Inflow implements Runnable{ //水对象 Water wat; public Inflow(Water w){ this.wat = w; } @Ove ...

  5. How to find configuration file MySQL uses?

    http://www.dbasquare.com/2012/04/01/how-to-find-mysql-configuration-file/ A customer called me today ...

  6. 使用webview如何做超时判断

    在加载网页时给一个timer定时器,规定超时时间,然后再超时时间的方法中提示超时 如果没有超时,则在webview协议中的“加载完成”方法中 取消timer定时器 - (void)openWebVie ...

  7. 路冉的JavaScript学习笔记-2015年1月23日

    1.JavaScript的数据类型 A.原始类型:包含数值.字符串.布尔值.空值(null)和未定义值(undefined). Js原始类型均为不可改变类型.对不可变类型调用任何自带方法都不会改变原始 ...

  8. 开启、关闭数据库mysql

    1.命令行 net start mysql net start mysql 2.点控制面板→管理→服务 招到mysql 3.找到安装目录下的bin目录

  9. 在windows C++中编译并使用Lua脚本

    早前就用过LUA ,只是局部的小项目使用,突然兴起想要写一些关于LUA 的  文章,记录曾经学习过的点点滴滴. 这里我使用的是LUA5.2作为 案例 lua做为轻量级脚本语言已经被广泛应用到应用软件以 ...

  10. 两种JSON数据类型的解析

    son数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid" ...