Re: General rule for constructors



softwarepearls_com wrote:
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.

It is only a small part of classes where one will want to be
able to replace the implementation.

Nobody wants to use a factory class for everything. Think Spring - you
want to dynamically load the critical classes not every class.

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.

I disagree.

Changing lots of code to use a specific implementation is a
bad practice even if it is somewhat automated.

Noone will prefer:

Connection con = new MySqlConnection(constr);

over:

Connection con = DriverManager.getConnection(conurl);

just because you have a fancy tool that can change MySqlConnection
to OracleConnection in the entire application.

Arne
.



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: 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: General rule for constructors
    ... work out the ratio of public constructors to factory methods. ...
    (comp.lang.java.programmer)
  • 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)