I\'m wondering about best practice here. Is it good practice for a factory method to return null if it can\'t create anything? Here\'s an example:
ICommand comma
It only makes sense to return Null if there's a reason why you would want the user to have to check for null every time he calls Create. Normally you'd consider the following a perfectly valid use pattern:
var obj = MyFactory.CreateThing();
obj.DoSomething();
But what you're proposing is to force the following usage pattern:
var obj = MyFactory.CreateThing();
if (obj == Null) {
// Handle null condition
} else {
obj.DoSomething();
}
Normally the Null scenario would mean some sort of failure, in which case an Exception would probably make the most sense. But ultimately you're the music maker here and have to decide what is sensible in the world you're building.