Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a good or bad way to use constructor chaining? (... to allow for testing)

My motivation for chaining my class constructors here is so that I have a default constructor for mainstream use by my application and a second that allows me to inject a mock and a stub.

It just seems a bit ugly 'new'-ing things in the ":this(...)" call and counter-intuitive calling a parametrized constructor from a default constructor , I wondered what other people would do here?

(FYI -> SystemWrapper)

using SystemWrapper;

public class MyDirectoryWorker{

    //  SystemWrapper interface allows for stub of sealed .Net class.
    private IDirectoryInfoWrap dirInf;

    private FileSystemWatcher watcher;

    public MyDirectoryWorker()
        : this(
        new DirectoryInfoWrap(new DirectoryInfo(MyDirPath)),
        new FileSystemWatcher()) { }


    public MyDirectoryWorker(IDirectoryInfoWrap dirInf, FileSystemWatcher watcher)
    {
        this.dirInf = dirInf;
        if(!dirInf.Exists){
            dirInf.Create();
        }

        this.watcher = watcher;

        watcher.Path = dirInf.FullName;

        watcher.NotifyFilter = NotifyFilters.FileName;
        watcher.Created += new FileSystemEventHandler(watcher_Created);
        watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
        watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
        watcher.EnableRaisingEvents = true;
    }

    public static string MyDirPath{get{return Settings.Default.MyDefaultDirPath;}}

    // etc...
}
like image 259
Grokodile Avatar asked Mar 23 '10 23:03

Grokodile


People also ask

Is constructor chaining mandatory?

We use this() keyword if we want to call the current class constructor within the same class. The use of this() is mandatory because JVM never put it automatically like the super() keyword. Note that this() must be the first line of the constructor.

What is constructor chaining?

Constructor chaining is the process of calling one constructor from another constructor with respect to current object. One of the main use of constructor chaining is to avoid duplicate codes while having multiple constructor (by means of constructor overloading) and make code more readable.

What is constructor in testing?

Constructor is an object initializer. Constructor testing is a test of the fact of initialization. That is, verification that the object's fields were initialized by the values passed to the constructor.

Why do we need constructor chaining in Java?

Need for Constructor Chaining in Java Constructor Chaining in Java is used when we want to pass parameters through multiple different constructors using a single object. Using constructor chaining, we can perform multiple tasks through a single constructor instead of writing each task in a single constructor.


1 Answers

Including a default constructor is a code smell as now the class is coupled to the concrete implementation of the IDirectoryInfoWrap. To make your life easier, use an IOC container external to the class to inject different dependencies depending on whether you are running test code or the mainstream application.

like image 111
Bermo Avatar answered Sep 25 '22 06:09

Bermo