4 ms·
Those are factory methods because I think OptimizedPage.fromFile(pathName); reads cleaner than new OptimizedPage(pathName); Do you think there's a rea
by dlss 14y ago
Those are factory methods because I think
OptimizedPage.fromFile(pathName);
reads cleaner than
new OptimizedPage(pathName);
Do you think there's a real difference (besides readability) for preferring a factory method to a constructor in this context?
- lmm 14y agoAs a library vendor it gives you more flexibility, particularly if you make the method return an interface rather than the concrete type. Also it can make working with frameworks that call a constructor by reflection easier (you probably know if this is happening though). It's certainly worth separating the "inner" (i.e. the one that takes a FileReader) from the "outer" constructor so that you can call the inner one for testing (perhaps with a mock FileReader), but admittedly you can accomplish that equally well with a public constructor that calls a protected constructor. For internal code I doubt it makes a lot of difference - you can always change it if you need it - but I prefer to use static factory methods everywhere for consistency.