You are here

Null-Object Pattern

15 January, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

In much of the current programming practice, the special value null is often used as a fag to represent a gamut of different and often disconnected concepts such as emptiness and falseness. This can result in a lot of confusion and complex control structures in the code. In our view, null should only be used to denote the non-existence of an object. null is not an object and has no "intelligence" (i.e. behavior) for other objects to interact with. Therefore, whenever we work with a union of objects and discover that one or more of these objects may be referencing a null value, we almost always have to write code to distinguish null as a special case. To avoid this problem, we should think of adding a special object, called the null object, to the union with appropriate behavior that will allow us to treat all object references in a consistent and uniform way, devoid of special case consideration. This design pattern is called the null object pattern. We have used the null object pattern in one occasion: the EmptyNode to represent the empty state of a (mutable) list (LRStruct).

In Frame3 the view adapter, _v2wAdapter, is initialized to an (anonymous) IView2World object. It is there to guarantee that _v2wAdapter is always referencing a c oncrete IView2World object, and , since setV2WAdapter(...) only allows a non-null assignment, we can always call on _v2WAdapter to perform any method without checking for null.