Search code examples
javanullnull-object-pattern

Template NullObject: Is it a sin?


I coded this little Template-NullObject in Java and wanted to ask you wether it would be considered a sin to use one of these. When creating a NullObject, you typically make one which was specifically designed for one type of object, this one isn't ...

public class NullObject<T> {

    T object;

    public NullObject(T object) {
        this.object = object;
    }

    public T get() {
        return object;
    }

    public void set(T object) {
        this.object = object;
    }

    public boolean isNull() {
        return object == null;
    }

}

Solution

  • I think it's OK. But I think you're reinventing the wheel, you may check for Optional in java 8 : Optional in Java 8. It's easy to work with and makes your code clearer.