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
2
votes
3 answers

How can I make NHibernate aware of a first-class "Null Object", without database persistence?

I would like to make use of the Null Object pattern in my domain, but I don't want to have records in my database that relate to it - I would prefer it if NHibernate were able to map a SQL null value to my Null object, and vice versa. Is this…
2
votes
2 answers

Can null be inserted into Cache?

I actively use caching in my ASP.NET web site. Though, some objects requested from db are really absent. For instance, I'm checking if there is a Discount for the particular product by looking record with ProductId=@ProductId in ProductsDiscount…
Budda
  • 18,015
  • 33
  • 124
  • 206
1
vote
0 answers

is this implementation of the Singleton and Object null Patterns Thread Safe?

Im trying to write a simple code to implement the Singleton and object null patterns. the code should check if the new customer has a name, if yes put it in the real customer, and if not in the fakecustomer. My focus in this question is: Is the…
1
vote
1 answer

C++ Nullptr vs Null-Object for potential noop function arguments?

TL;DR : Should we use fn(Interface* pMaybeNull) or fn(Interface& maybeNullObject) -- specifically in the case of "optional" function arguments of a virtual/abstract base class? Our code base contains various forms of the following pattern: struct…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
1
vote
1 answer

EF Core optional ValueObject as identity

I am using value objects as identities and would like to know how to best deal with nulls in EF core. For example if I have an employee with an optional title (mr, mrs, etc): public class Employee : Entity, IAggregateRoot { public…
1
vote
3 answers

Why not implement singleton pattern for the NULL object in Kotlin?

class Foo(val param1: String, var param2: Int) { // class body companion object { val NULL = Foo("", 0) } } Is the NULL object valid in Kotlin? Although Kotlin allows adding default values in the…
1
vote
2 answers

Implement null object pattern

I found this question which implements a null object pattern in Kotlin in a certain way. I usually do it a little different in Java: class MyClass { static final MyClass INVALID = new MyClass(); public MyClass() { // empty …
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
1
vote
5 answers

Handling associations w/ null objects in Rails

I'm using the Null Object pattern in my Rails application to implement the concept of a guest user account. Like many apps, I have a method on ApplicationController called current_user. In the case of a non-logged in user, I want to use my guest…
metahamza
  • 1,405
  • 10
  • 26
1
vote
2 answers

NullObject Pattern: How to handle fields?

Suppose we have Book class which contains year_published public field. If I want to implement NullObject design pattern, I will need to define NullBook class which behaves same as Book but does not do anything. Question is, what should be the…
Mahdi
  • 1,871
  • 2
  • 24
  • 41
1
vote
2 answers

Rails instance methods conditional depending on attributes being set

I have a model that has several attributes that are optional when the model is being saved. I have several instance methods that use these attributes and perform calculations but I would like to check first if they are not nil as I will get the…
Sam Mason
  • 1,037
  • 1
  • 8
  • 29
1
vote
1 answer

How do I mock a specific attribute on a model for a Rails view test?

I'm writing tests for a rails view, lets say the view is something like: show.json.rep r.element :user, @user do |u| r.element :id, u.id r.element :somefield1, u.somefield1 // a lot of fields here r.element :somefield100,…
Freewind
  • 193,756
  • 157
  • 432
  • 708
1
vote
1 answer

Good case for a Null Object Pattern? (Provide some service with a mailservice)

For a website I'm working on, I made an Media Service object that I use in the front end, as well as in the backend (CMS). This Media Service object manipulates media in a local repository (DB); it provides the ability to upload/embed video's and…
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
1
vote
2 answers

Null Object design pattern troubles with behaviors

I want share some of my thoughts and ask some questions Null object design pattern supposed to implement class with neutral behavior. So, if i need implement interface: public interface IProduct { double Cost(); string Name(); } probably,…
zzfima
  • 1,528
  • 1
  • 14
  • 21
1
vote
3 answers

c++ doubly linked list with null object model

I'm trying to create a doubly-linked list with the null object model. So far, I've implemented a method to add a node to the beginning of the list and a method to display the node. My problem is that the display function always displays 0. Can…
Derek
  • 97
  • 1
  • 1
  • 11
0
votes
3 answers

Null Object Design Pattern in LinkedList

I am trying to Implement a doubly linked with null objects at the beginning and end of the list using null object design pattern. So an empty list will contain two null objects. So I wrote this code Does this follow null object design pattern? If…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294