-2

I just had a discussion about parameterized constructors with my exercise instructor. He said it is bad practice having two or more constructors, especially parameterized constructors. Instead of constructors I should use only one empty constructor and for initialisation the factory method pattern.

So this is the first time, I've ever heard something like this. I did some research, but I could not find anything related. The only bad practices I've found are:

  • too many parameters inside constructor
  • using public/protected method inside constructor (because a child class can override the methods)
  • wild calculations

So my question is, what is best practice? Is it fine to set instance variables inside constructor or should I follow the advice and use the factory method pattern?

Marco
  • 1,579
  • 1
  • 19
  • 34
  • 7
    That's just, like, his opinion, man. – John Kugelman Apr 15 '16 at 21:12
  • It's ok to have more than 1 constructor, but if you don't chain them you can get inconsistent results, for instance see [this question](http://stackoverflow.com/q/25272784/217324) i would recommend getting your terminology right, you probably mean "instance variable", not "class variable" – Nathan Hughes Apr 15 '16 at 21:13
  • You mean the builder pattern? – OneCricketeer Apr 15 '16 at 21:13
  • 3
    I suggest reading ["Consider static factory methods instead of constructors"](http://www.informit.com/articles/article.aspx?p=1216151) by Josh Bloch – Andrew Tobilko Apr 15 '16 at 21:13
  • I suspect what he meant by "factory method pattern" really is a static factory method. These are two very different things: the first relies on the use of class inheritance, while the second is not a "pattern" at all, but just an "idiom". – Rogério Apr 15 '16 at 21:19
  • Several resources like wikipedia treat the factory method as pattern. – Marco Apr 15 '16 at 21:24
  • @MarcoMartens Yes, "Factory Method" (along with "Abstract Factory") is one of 23 design patterns in the GoF book. But "static factory method" is not in the book, and is *not* a pattern. – Rogério Apr 15 '16 at 21:25

2 Answers2

0

Whether you use a factory method or multiple constructors is really just personal preference, especially with Java 8 where you can easily use a constructor reference as a factory (That's all a constructor really is - a factory for making instances of the object). It's perfectly fine to have multiple constructors for a class, and if you have too many constructors in a class, that's a sign of a class that is doing too much, not that you need to switch to a factory. Constructors very much should be parameterized when a class requires specific input to be valid and null / 0 is not a sane default value.

What you should avoid, however, is allowing an object to exist in an invalid state. For example, consider the following class and factory:

public class MyList {

    private Object[] backingArray;

    public void setBackingArray(Object[] backingArray) {
        this.backingArray = backingArray;
    }
}

public class MyListFactory() {

    MyList newMyList(int initialSize) {
         MyList list = new MyList();
         list.setBackingArray(new Object[initialSize]);
         return list;
    }



    MyList newMyList() {
         MyList list = new MyList();
         list.setBackingArray(new Object[defaultSize]);
         return list;
    }
}

This is an example of bad design because I can ignore the factory and call new MyList() directly and the object is essentially invalid until I also call setBackingArray(). When using a factory pattern, you have to be very careful to make sure that other code can't create the object directly without going through the factory. (The above classes are also bad for a host of other reasons, but they're not relevant to the point I'm trying to make).

Darth Android
  • 3,437
  • 18
  • 19
0

Best practice is up to you and up to you alone. I suggest to use Project Lombok (https://projectlombok.org). You can use annotation on class @AllArgsConstructor and it will be created automatically. Also you can use @RequiredArgsConstructor for final fields. There are many possibilities for auto-creating getters and setters. So your code will be shorter and more readable.