Null Pointer --设计模式
在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
- if (obj.returnCollection().size() > 0) {
- // 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 --设计模式的更多相关文章
- 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 ...
- differences between null pointer and void pointer.
These are two different concepts, you cannot compare them. What the difference between the skunk and ...
- 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: ...
- 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 ...
- 力扣 报错 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 = ...
- Unable to handle kernel NULL pointer dereference at virtual address 00000000问题的解决
今天在编译好内核模块后,安装内核模块memdev.ko的时候,出现了Unable to handle kernel NULL pointer dereference at virtual addres ...
- Unable to handle kernel NULL pointer dereference at virtual address 00000000【转】
本文转载自:https://blog.csdn.net/hpu11/article/details/72628052 这说明是非法指针的使用,才导致系统出错. [ 1023.510000] Unabl ...
- spring quartz job autowired 出错 null pointer
什么情况下才能用autowired? 当当前类属于spring IOC容器管时候就可以,比如在applicationContext.xml里有定义 就是说在spring上下文里能够找到 但是比如qua ...
- java NPE就是空指针异常,null pointer exception
java NPE就是空指针异常,null pointer exception
随机推荐
- 8-18-Exercise
8-18-小练 A.HDU 1172 猜数字 采用枚举~[赤果果的暴力~] 代码: #include <iostream> #include <cstdio> #inclu ...
- android开源项目学习
FBReaderJ FBReaderJ用于Android平台的电子书阅读器,它支持多种电子书籍格式包括:oeb.ePub和fb2.此外还支持直接读取zip.tar和gzip等压缩文档. 项目地址:ht ...
- cocos2d-x简单动画
转自:http://4137613.blog.51cto.com/4127613/759610 这里只给出最基本的动画代码,具体使用要根据实际情况自己封装.最好自己开发一个编辑器.额外说一句,开发编辑 ...
- 删除已分配IP的静态IP地址池
如果静态IP地址池已经分配了IP,则无法直接将其静态IP地址池删除,会提示出错:“已经有IP被分配,需要先将其回收,再删除” 如下: 查看IP地址池: Get-SCStaticIPAddressPoo ...
- Discuz建站教程:本地安装discuz网站
网站建目前都很简单,建站容易,管理难,网站做大优化更难.本人有建站经验,目前给大家分享一下如何建站,当然,目前使用的是本地建站,因为非本地建站需要购买域名和网站空间,当然,朋友们想真正建站的,对于一些 ...
- solr 搜索引擎
http://www.cnblogs.com/wenxinghaha/p/4088790.html
- jfinal上传下载
1. 上传 <form id="form1" method="post" enctype="multipart/form-data" ...
- ubuntu上如何安装和卸载google chrome 浏览器
切换到安装文件目录 $ sudo dpkg -i file_name.deb 如果有错误,请运行以下命令 $ sudo apt-get -f install or $ sudo apt-get ins ...
- JavaScript引擎的工作原理
http://my.oschina.net/fuckBAT/blog/318355?fromerr=jK6wCh1p#OSC_h4_4
- 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16
http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...