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/
评论
发表评论

您还没有登录,请登录后发表评论

yujianqiu
搜索本博客
最近加入圈子
最新评论