2

This is more of a style question than an "I'm having trouble question". For Null placeholder objects (am I using the right term?), is it generally preferred to use the Singleton pattern? For ease of discussion, here's an example:

public interface Foo {
    void myMethod();
}

public class RealFoo implements Foo {
    void myMethod() { /* Do something productive */ }
}

public class MyUniverse {
    public static void main(String args[]) {
        Foo[] fooArray = new Foo[10];
        // do something productive that might result in Null objects in the array
        for (Foo f : fooArray) {
            f.myMethod(); // I DONT WANT TO DO if (f != null) blah blah
        }
    }
}

Ok so that's my scenario, is it preferred to do A or B?

// A
public class NullFoo implements Foo {
    public NullFoo() {}       
    public void myMethod() { /* don't need to do anything */ }
}

// B
public class NullFoo implements Foo {
    private static NullFoo _instance = null;
    protected NullFoo() {}
    public static NullFoo getInstance() {
        if (_instance == null) _instance = new NullFoo();
        return _instance;
    }
    public void myMethod() {  /* don't need to do anything */ }
}

Thanks! My instinct that B is almost always superior but maybe I'm missing something, so I ask...

4
  • 3
    Do I see you initializing an Interface?
    – agirish
    Oct 23, 2012 at 14:58
  • @AbhishekGirish: I can't see it...
    – Tudor
    Oct 23, 2012 at 14:59
  • Foo[] fooArray = new Foo[10];
    – agirish
    Oct 23, 2012 at 15:00
  • 4
    @AbhishekGirish: That's an array of interfaces. No actual object construction takes place. You'd need to call constructors for each of the items afterwards.
    – Tudor
    Oct 23, 2012 at 15:01

5 Answers 5

3

I would go for the latter. I may be something of a trend-bucker on this, but I don't see singletons as inherently evil. If a class has no mutable state, a singleton is just fine.

I'd make the constructor private, though, not protected. Also, your lazy loading is race-prone; I would just instantiate NullFoo on the declaration line (private static final Foo instance = new NullFoo();). And lastly, don't forget to have the class actually implement the interface. :)

0
1

From the point of view of the virtual machine, it would be better the B solution as java would instantiate at most one object.

0

Well, interesting. Can't say I've ever been faced with this but...

I'd suggest if the instance is to be used consistently (i.e. it will always do the same thing) then go with B. That way, you have only a single shared instance representing your null object. This is a space-saving optimisation (rather than clutter your memory with NullFoos. Much the same as Collections#emptyList.

I must say though, in terms of dsign, I can't see why you'd //do something productive that might result in Null objects in the array which would take the Foo in your fooArray and then replace it with null. I'm sure you have your reasons though.

0
-1

I'd implement the singleton as an enum.

enum NullFoo implements Foo{
  INSTANCE{
    // implement methods here
  }
}

See: Singleton > The Enum Way or read
Item 3: Enforce the singleton property with a private constructor or an enum type
in Effective Java 2nd Ed by Joshua Bloch

-1

I think you shouldn't be using interfaces here. An interface describes a set of operations that a class that implements it is required to support. But you're using the NullFoo class to basically circumvent that requirement.

So why not just use a base class that does nothing in its implementation of myMethod:

public class BaseFoo
{
    public void myMethod()
    {
        // do nothing
    }
}

public class DerivedFoo extends BaseFoo
{
    public void myMethod()
    {
        // do actual work
    }
}

Or, if your actual situation does call for interfaces, then just go ahead and write the if (foo != null) ... tests.

1
  • 1
    I don't want to do if (foo != null) because in the real code, the class is used in a lot of places and it makes all the other code have to very similar checks. Why not hand it off to a class?
    – durron597
    Oct 23, 2012 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.