Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > General rule for constructors

Reply
Thread Tools

General rule for constructors

 
 
pek
Guest
Posts: n/a
 
      08-14-2008
Hello, I've been reading lately Effective Java and Josh Bloch suggests
more than once to avoid using the default public constructors in order
to solve particular problems. More specifically, Item 1 says "Consider
static factory methods instead of constructors" or Item 2 "Consider a
builder when faced with many constructors" etc etc..

While all these paradigms are great (I was stunned when reading the
Builder pattern) I do feel that his general advice for constructors is
thinking like method visibility (start from private and build up to
public):

First try to see if all other are relevant to your problem and then,
as a final solution, create a public constructor.

Is this the way I should be thinking? Is creating a public constructor
the least desired way of constructing an object? Or are these
solutions good only for the problems they specifically solve?

Not that I'm against it. On the contrary, his solutions blow my mind
away (obviously, I'm not the guy you would call an advanced
programmer).

Thank you,
Panagiotis
 
Reply With Quote
 
 
 
 
Danger_Duck
Guest
Posts: n/a
 
      08-14-2008
On Aug 14, 1:02*pm, pek <kimwl...@gmail.com> wrote:
> Hello, I've been reading lately Effective Java and Josh Bloch suggests
> more than once to avoid using the default public constructors in order
> to solve particular problems. More specifically, Item 1 says "Consider
> static factory methods instead of constructors" or Item 2 "Consider a
> builder when faced with many constructors" etc etc..
>
> While all these paradigms are great (I was stunned when reading the
> Builder pattern) I do feel that his general advice for constructors is
> thinking like method visibility (start from private and build up to
> public):
>
> First try to see if all other are relevant to your problem and then,
> as a final solution, create a public constructor.
>
> Is this the way I should be thinking? Is creating a public constructor
> the least desired way of constructing an object? Or are these
> solutions good only for the problems they specifically solve?
>
> Not that I'm against it. On the contrary, his solutions blow my mind
> away (obviously, I'm not the guy you would call an advanced
> programmer).
>
> Thank you,
> Panagiotis


What are you trying to do?
 
Reply With Quote
 
 
 
 
pek
Guest
Posts: n/a
 
      08-14-2008
On Aug 14, 8:43*pm, Danger_Duck <ganggang3s...@gmail.com> wrote:
> On Aug 14, 1:02*pm, pek <kimwl...@gmail.com> wrote:
>
>
>
> > Hello, I've been reading lately Effective Java and Josh Bloch suggests
> > more than once to avoid using the default public constructors in order
> > to solve particular problems. More specifically, Item 1 says "Consider
> > static factory methods instead of constructors" or Item 2 "Consider a
> > builder when faced with many constructors" etc etc..

>
> > While all these paradigms are great (I was stunned when reading the
> > Builder pattern) I do feel that his general advice for constructors is
> > thinking like method visibility (start from private and build up to
> > public):

>
> > First try to see if all other are relevant to your problem and then,
> > as a final solution, create a public constructor.

>
> > Is this the way I should be thinking? Is creating a public constructor
> > the least desired way of constructing an object? Or are these
> > solutions good only for the problems they specifically solve?

>
> > Not that I'm against it. On the contrary, his solutions blow my mind
> > away (obviously, I'm not the guy you would call an advanced
> > programmer).

>
> > Thank you,
> > Panagiotis

>
> What are you trying to do?


Nothing in particular. Just asking what is the best way of thinking
when it comes to constructors..
 
Reply With Quote
 
Andrea Francia
Guest
Posts: n/a
 
      08-14-2008
pek wrote:
> On Aug 14, 8:43 pm, Danger_Duck <ganggang3s...@gmail.com> wrote:
>> On Aug 14, 1:02 pm, pek <kimwl...@gmail.com> wrote:
>>
>>
>>
>>> Hello, I've been reading lately Effective Java and Josh Bloch suggests
>>> more than once to avoid using the default public constructors in order
>>> to solve particular problems. More specifically, Item 1 says "Consider
>>> static factory methods instead of constructors" or Item 2 "Consider a
>>> builder when faced with many constructors" etc etc..
>>> While all these paradigms are great (I was stunned when reading the
>>> Builder pattern) I do feel that his general advice for constructors is
>>> thinking like method visibility (start from private and build up to
>>> public):
>>> First try to see if all other are relevant to your problem and then,
>>> as a final solution, create a public constructor.
>>> Is this the way I should be thinking? Is creating a public constructor
>>> the least desired way of constructing an object? Or are these
>>> solutions good only for the problems they specifically solve?
>>> Not that I'm against it. On the contrary, his solutions blow my mind
>>> away (obviously, I'm not the guy you would call an advanced
>>> programmer).
>>> Thank you,
>>> Panagiotis

>> What are you trying to do?

>
> Nothing in particular. Just asking what is the best way of thinking
> when it comes to constructors..


Josh Bloch hints are fine.

Only keep in minds that JavaBeans needs at least a public constructor
with no parameters. This apply only for JavaBeans.

--
Andrea Francia
http://andreafrancia.blogspot.com/20...o-windows.html
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-14-2008
pek wrote:
> Hello, I've been reading lately Effective Java and Josh Bloch suggests
> more than once to avoid using the default public constructors in order
> to solve particular problems. More specifically, Item 1 says "Consider
> static factory methods instead of constructors" or Item 2 "Consider a
> builder when faced with many constructors" etc etc..
>
> While all these paradigms are great (I was stunned when reading the
> Builder pattern) I do feel that his general advice for constructors is
> thinking like method visibility (start from private and build up to
> public):
>
> First try to see if all other are relevant to your problem and then,
> as a final solution, create a public constructor.
>
> Is this the way I should be thinking? Is creating a public constructor
> the least desired way of constructing an object? Or are these
> solutions good only for the problems they specifically solve?
>
> Not that I'm against it. On the contrary, his solutions blow my mind
> away (obviously, I'm not the guy you would call an advanced
> programmer).
>
> Thank you,
> Panagiotis


So, the best advice that I can give you on this subject is borrowed from
a book (I believe Effective C++):

"Make your classes easy to use correctly, and difficult to use incorrectly."

You should consider that rule when deciding on whether you want to use
constructor versus some sort of factory (be it a builder, configurable
factory, static factory method, or something else).

The other important consideration is in the future when you create a
sub-class, or a replacement implementation, will it be easy for users of
your current "instantiation approach" to benefit from your enhancements
(whether it Just Happens, or is a simple change in one place)

And remember Rule #1: Every rule has an exception. Except this one.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-14-2008
Daniel Pitts wrote:

> So, the best advice that I can give you on this subject is borrowed from
> a book (I believe Effective C++):
>
> "Make your classes easy to use correctly, and difficult to use
> incorrectly."


Also, once you think you have correctly outlined how you class should
work, check out some Design Patterns and see if they do something
markedly different. There are patterns for code too, not just design.
Best ways to implement something, and all that.

I think your original statement is good as a general rule. You should
make everything private by default, and then see what you absolutely
have to make public. Don't forget about things like Facades and
Proxies, which can help you hide the real object that does the work.

However, there will also be some specific patterns for what you are
doing, and also some anti-patterns too. Try to find the most specific
ones and apply those. Too much generalization isn't good either.

Time and familiarity will help you find the best patterns quickly. I'm
definitely still learning here too. And mis-designed code that is
nevertheless designed clearly and with forethought shouldn't be that
hard to refactor if you later decide the use of a particular pattern
wasn't the best. "Build one to throw away."

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-14-2008
pek wrote:
> Hello, I've been reading lately Effective Java and Josh Bloch suggests
> more than once to avoid using the default public constructors in order
> to solve particular problems. More specifically, Item 1 says "Consider
> static factory methods instead of constructors" or Item 2 "Consider a
> builder when faced with many constructors" etc etc..
>
> While all these paradigms are great (I was stunned when reading the
> Builder pattern) I do feel that his general advice for constructors is
> thinking like method visibility (start from private and build up to
> public):
>
> First try to see if all other are relevant to your problem and then,
> as a final solution, create a public constructor.
>
> Is this the way I should be thinking? Is creating a public constructor
> the least desired way of constructing an object? Or are these
> solutions good only for the problems they specifically solve?
>
> Not that I'm against it. On the contrary, his solutions blow my mind
> away (obviously, I'm not the guy you would call an advanced
> programmer).


Note that consider != always.

There are some classes where using a constructor ties a lot
of your code to an implementation that you may want to
change.

JAXP W3C DOM Document is a good example of an alternative
approach and why it is beneficial.

You could argue that Spring is fundamentally based on the
concept.

So consider it and and chose it when it makes sense.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-14-2008
Andrea Francia wrote:
> Only keep in minds that JavaBeans needs at least a public constructor
> with no parameters. This apply only for JavaBeans.


It is also sometimes needed with serialization and externalization.

Arne
 
Reply With Quote
 
softwarepearls_com
Guest
Posts: n/a
 
      08-15-2008
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).

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-15-2008
Arne Vajhøj wrote:
> Andrea Francia wrote:
>> Only keep in minds that JavaBeans needs at least a public constructor
>> with no parameters. This apply only for JavaBeans.

>
> It is also sometimes needed with serialization and externalization.

Sometimes, but only if you are doing custom work. Standard
serialization will bypass the object initialization phase completely.
No constructor, no default field values (other than the minumum
null/false/0), etc.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
how to add validation rule for url in the validation-rule.xml ,I added some thing like this but......... shailajabtech@gmail.com Java 0 10-12-2006 08:36 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57