Questions tagged [null-object-pattern]

A pattern where a specific object is used to represent a null, rather than a true programmatic null, in order to avoid various complications that arise from using an actual null. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

Certain protocols and adapters can explode when processing an actual null. To avoid this issue, and to make code simpler (dispense with all null checks on the client side), a special "token" object that represents a "null" is used in place of a null.

It was first documented in Bobby Woolf in "Pattern Languages of Program Design 3".

See Wikipedia for more.

85 questions
3
votes
1 answer

Mongoid association & null object pattern?

How would you implement the null object pattern on a Mongoid relation? Class Owner include Mongoid::Document embeds_one :preference end Most owners won't have a preference, and thus I want them to have a NullPreference instead, as described in…
Yeggeps
  • 2,055
  • 2
  • 25
  • 34
2
votes
2 answers

Can I tell NHibernate not to save certain objects?

I am using NHibernate with a NullObject pattern to make my views simpler. I use a solution found here by James Gregory public Address GetAddressOrDefault() { return Address ?? new NullAddress(); } And Person has an Address property and so…
Eystein Bye
  • 5,016
  • 2
  • 20
  • 18
2
votes
1 answer

Is there a safe "no id" @StringRes or @DrawableRes value for a null object pattern?

Suppose I wanted to implement a null object pattern of a model so as to guarantee an object that will do nothing, without requiring null checks on nullable fields. interface Model( val textId: Int, val imageId: Int, ... ) data class…
2
votes
2 answers

Can StructureMap return a Special Case?

I want to have StructureMap return a Special Case called "None" for a certain instance. Say I Have this class MyUser which is scoped as HttpContext. I want to have a nested, dreived class None (ie. MyUser.None) which is returtned for the type MyUser…
Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
2
votes
1 answer

get all table values from firebase null object reference firebase database

public class ShowBPGraphActivity extends AppCompatActivity { public GraphView graphView; public LineGraphSeries lineGraphSeries; public String systolicbp; public String diastolicbp; public String timebp; public String datebp; public…
2
votes
0 answers

Null Object pattern and Enums

I have a class Person with an Enum for a property. It is a non-nullable Enum. I have some new requirements and I can easily take care of them by introducing a Null Object NullPerson with all the properties set to String.Empty, however the Enum…
Robotronx
  • 1,728
  • 2
  • 21
  • 43
2
votes
1 answer

Does the null object pattern make debugging more complicated?

I was wondering if the null object pattern could actually make debugging more difficult in certain cases. For example, if a method returns an "empty" object rather than a null then it will not throw an error. This is good in terms of reliable, clean…
Samuel Jones
  • 105
  • 10
2
votes
2 answers

Does Null Object Pattern break Interface Segregation principle?

The Interface Segregation principle states that: Clients should not be forced to depend on methods that they do not use. In the Null object pattern the Null class that implements the interface does nothing with it. Which is intentional. But, it…
Mahmut
  • 33
  • 6
2
votes
1 answer

Ways to implement Null Object Pattern

Till now I have seen everywhere that the steps to create Null Object Pattern, for example in BST to remove null checks, are as follows: Create an interface Node Create two classes implementing the interface Node. One of which will be real_Node and…
2
votes
3 answers

Guard clause and Null Object Pattern in Dependency Injection and C#

Using Constructor Injection, a dependency gets injected to a consumer like this (at least I hope I understood it correctly): public class SomeConsumer { private IDependency someDependency; public SomeConsumer(IDependency someDependency) …
Thaoden
  • 3,460
  • 3
  • 33
  • 45
2
votes
3 answers

NullObjectPattern and the Comparable interface

The problem I'm having has already been asked before: How to implement an interface with an enum, where the interface extends Comparable? However, none of the solutions solve my exact problem, which is this: I have a value object, similar to…
durron597
  • 31,968
  • 17
  • 99
  • 158
2
votes
1 answer

Null implementation of type boost::functions

I have something like this: boost::function redStyle = boost::bind(colorStyle, "red"); boost::function blueBlinkingStyle = boost::bind(animatedStyle, "blue", BLINKING); Is this the correct way to define a nullStyler: void…
Baz
  • 12,713
  • 38
  • 145
  • 268
2
votes
1 answer

Creating objects with reflection

I'm trying to write a NullObject creation method, where I pass in a class name that implements an ICreateEmptyInstance interface (that is empty) and it walks it's properties looking for other classes that implement ICreateEmptyInstance and will…
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
2
votes
1 answer

c# - Returning default values for null properties, when the parent of these properties can or can not be null

So I didn't find any elegant solution for this, either googling or throughout stackoverflow. I guess that I have a very specific situation in my hands, anyway here it goes: I have a object structure, which I don't have control of, because I receive…
JeanK
  • 1,765
  • 1
  • 15
  • 22
2
votes
2 answers

Groovy NullObject should be null or not?

This example can be easily tested in the groovy console. var a is evaluated to not null while b is evaluated to null. Both are instances of org.codehaus.groovy.runtim.NullObject def b = null println b.getClass() println b == null def a =…
lfrodrigues
  • 305
  • 3
  • 11