Having methods with different names for roughly the same task makes sense, for example
open(String filename);
createThenOpen(String filename); // First create the file with default contents, then process the file.
This naming approach does not work for constructors. Imagine the constructor takes a filename as above. I have various options to handle the two cases:
- If-statement in the constructor: if the file does not exist then create it. This is not nice as the behaviour of the constructor is implicit and the caller might create unwanted files instead of opening existing files.
- Add a flag to the constructor:
MyClass(String filename, boolean createNew)
. Not very nice because a call likeMyClass("hello.txt", true)
is cryptic. - Overload so that a single argument always assumes file exists, and the presence of an additional dummy parameter means the file should be created. This is also ugly.
- Add a String-flag as in
RandomAccessFile(File file, String mode)
wheremode
is"r"
,"rw"
and so on. This feels very clunky for my purposes. - Add an
enum
-flag similar toFiles
'scopy(Path source, Path target, CopyOption... options)
. Feels pretty clunky too. - Have constructor that takes no argument and then separate methods like above to be called immediately after the object is created. Not nice as it makes no sense to have an instance of my object without instantiating it using data from the specified file.
Currently I seem to actually favour number (6) above and simply have two methods with different names to be called immediately after a no-parameter constructor. Have I overlooked any options, or is there a "given" approach for these scenarios?
Edit: as pointed out by others below there is a 7th, perhaps most obvious option, of course:
- Use factory methods!