Re: General rule for constructors



I'm a big fan of JB and his Effective Java.. but look at the JDK, and
work out the ratio of public constructors to factory methods. Public
constructors are in an overwhelming majority.. so clearly most people
are perfectly happy with constructors which, yes, hardcode the
implementation type in your logic. Even Bloch's Collections API uses
mostly the constructors approach.

Last but not least, I would argue strongly that software needs to be
thought of as fluid. With modern refactoring tools, we can quickly
make the kinds of changes to whole codebases which just a few years
ago would have been major headaches. JB's best advice is "If in doubt,
leave it out." So given that your static factories are a bit more
complex than plain constructors, I'd say leave them out (initially).

.



Relevant Pages

  • Re: Constructor or getInstance() method
    ... Minus java.util.Calendar (it has protected constructors) and plus ... >> reasons to have both public constructors and factory methods. ... > can be distinguished even if their signatures are identical. ... > form, plus `static Complex polar' ...
    (comp.lang.java.programmer)
  • Re: =?UTF-8?B?SmF2Ye+/vXMgQnJva2VuIEJvb2xlYW5z?=
    ... Ian Shef wrote: ... Obviously the choice balances the cost of the one idiom against the other. ... Joshua Bloch's /Effective Java/, Item 1, is, "Consider static factory methods instead of constructors". ...
    (comp.lang.java.programmer)
  • Re: General rule for constructors
    ... work out the ratio of public constructors to factory methods. ... Connection con = DriverManager.getConnection; ...
    (comp.lang.java.programmer)
  • Re: Could an object refuse to be created!?
    ... Justin Rogers wrote: ... > to valid input criterion, that you might instead use a factory method. ... > Factory methods demonstrate, better than constructors, that work is going ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cant use New?
    ... > any public constructors, but if there were, wouldn't the New keyword ... Public Sub New(s As String) ... Dim myX As New X ...
    (microsoft.public.dotnet.languages.vb)