Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor injection - is it ok to use new null-object if none provided in the constructor?

Imagine I have a bunch of classes, that can do logging, so I've created a logger interface and a couple of implementations (writing to file, to stdout, to db, etc.), but sometimes I don't care about logging those messages, so I've made an implementation, that simply ignores all of the messages.

Now this helps to avoid ifs and just use $this->logger->write($message), but I still have to inject the dummy logger every time.

So would it do any harm to do stuff like: $this->logger = $logger ? $logger : new DummyLogger() in the constructor.

I generally do no work in the constructor, but this kind of stuff doesn't seem too dangerous.

Would you choose this approach?

like image 406
Fluffy Avatar asked Nov 30 '11 10:11

Fluffy


Video Answer


2 Answers

Before answering your question, I'd suggest to rethink your Logger architecture. Logging is always the same. You write a message to somewhere. The only thing that differs is the somewhere part, so it makes sense to separate your Logger into a generic Logger and various Writers. That allows for greater flexibility, for instance, you can use the Composite pattern to write to multiple Loggers at once.

Regarding your question: in general you want to avoid hardcoding dependencies into your code because it will directly impact on testability and reusability, e.g. your Logger can only be used with this particular NullWriter then. If you are going to distribute the Logger you will also have to distribute the Writer as well.

However, assuming that you would only distribute the Logger with the entire package of Writers anyway and you also provide the means to do ctor injection, I dont see much of an issue. You can still swap out the Writer if needed, so all is well.

It's somewhat different when we are talking about interpackage dependencies. You will want to avoid these, e.g. your classes in the Database package should not depend on classes in the Logger package.

An alternative to assigning the NullWriter from inside the Logger would be to use a LoggerFactory that creates the Logger and a specified Writer, injects the Writer and returns the Logger.

like image 114
Gordon Avatar answered Oct 18 '22 21:10

Gordon


I would not choose that approach because it is a hard dependancy and an extra class. There are patterns to avoid these problems - and solving your dilemma.

You could implement a version of the observer pattern, something like:

class Loggable
{
    protected $aLoggers = array();

    public function addLogger(Logger $oLog) {
        $this->aLoggers[] = $oLog;
    }

    public function invokeLoggers($sMessage) {
        foreach($this->aLoggers as $oLog) {
            $oLog->write($sMessage);
        }
    }
}

These functions should ofcourse be in a seperate class so that any class using loggers can extend this functionality. You can now choose to not add loggers and thus the invoke will do nothing - or to not extend your original class at all.

class SomeClass extends Loggable
{
    public function doSomething()
    {
        // Some code
        $this->invokeLoggers();
    }
}

$oSomeClass = new SomeClass();
$oSomeClass->addLogger(new FileLogger());
$oSomeClass->addLogger(new DatabaseLogger());
$oSomeClass->addLogger(new EmailLogger());
$oSomeClass->doSomething();
like image 1
Wesley van Opdorp Avatar answered Oct 18 '22 21:10

Wesley van Opdorp