2008-04-02
Null Object Pattern
关键字: pattern
In object-oriented computer programming, a Null Object is a behavioral pattern designed to act as a default value of an object.
Motivation
In most object-oriented languages, such as Java, references may be null. These references need to be checked to ensure they are not null before invoking any methods, because you can't invoke anything on a null reference. This tends to make code less readable. Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), you use an object which implements the expected interface, but whose method body is empty. The advantage of this approach over a working default implementation, is that a Null Object is very predictable and has no side effects: it does nothing.
It can also be used to act as a stub for testing, if a certain feature, such as a database, is not available for testing.
Links
http://en.wikipedia.org/wiki/Null_Object_pattern
http://www.cs.oberlin.edu/~jwalker/nullObjPattern/
Motivation
In most object-oriented languages, such as Java, references may be null. These references need to be checked to ensure they are not null before invoking any methods, because you can't invoke anything on a null reference. This tends to make code less readable. Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), you use an object which implements the expected interface, but whose method body is empty. The advantage of this approach over a working default implementation, is that a Null Object is very predictable and has no side effects: it does nothing.
It can also be used to act as a stub for testing, if a certain feature, such as a database, is not available for testing.
Links
http://en.wikipedia.org/wiki/Null_Object_pattern
http://www.cs.oberlin.edu/~jwalker/nullObjPattern/
发表评论
- 浏览: 855 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
问题:列举Spring框架中使 ...
呵呵。。无中生有。有中还无啊。 模式只不过是别人经验的总结。也许经验丰富了。 ...
-- by penghao122 -
问题:列举Spring框架中使 ...
Factory Method Template MethodStrategy s ...
-- by fantasybei -
问题:列举Spring框架中使 ...
facadeabstract factorydelegateadapterabs ...
-- by willim -
问题:请写一段代码说明如 ...
TomHornson 写道http://www.ibm.com/develope ...
-- by suke -
问题:请写一段代码说明如 ...
yujianqiu 写道 问题:请写一段代码说明如何实现Singleton模式 ...
-- by wolfbrood






评论排行榜