Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Passing parameter to constructor or calling setxxx()

Thread view: 
Joey.Information@gmail.com - 10 Nov 2005 10:10 GMT
Hello all,

When I write Java code. I always meet a problem . How to assign value
to a entity object attributes, Some person like to pass values as
parameter of  constructor when create this entity bean  , some persons
like to create the object ,after that call setxxx().

Of course those can work,  But when this entity class needs some new
attributes , we need to modify the class constructor or use polymorphic
to make a new constructor. but I think it make code complex, and I
don't like to modify code very often. So my opinions is using set()
method is better..

I hope some one can give me some comments.

thanks
bartekkl@gmail.com - 10 Nov 2005 12:20 GMT
Joey.Information@gmail.com napisal(a):
>  When I write Java code. I always meet a problem . How to assign value
> to a entity object attributes, Some person like to pass values as
> parameter of  constructor when create this entity bean  , some persons
> like to create the object ,after that call setxxx().

This is a tiny little problem that always makes me stop for a while and
medidate over my code ;) I've personally developed a simple rule: when
the number of attributes exceeds 4-5, or I suppose it will in future, I
use setxxx(). Otherwise, I find a constructor with args simple and good
enough.

Regards,

Bartosz Klimek
Andrew McDonagh - 10 Nov 2005 23:49 GMT
> Joey.Information@gmail.com napisal(a):
>
[quoted text clipped - 12 lines]
>
> Bartosz Klimek

when I find I need to initialised/set 4 or more variables, regardless of
how (constructor or setter) its an indication that the class is probably
doing to much and not conforming to the Single Responsibility Principle.

Its an indication - not a universal truth - but one that often proves
correct.

When it happens, I pass a single instance of a new class which to
represents 2 or more of the original parameters.

Its especially true when the parameters are all of a similar primitive
type.

Look at the JTable cell renderer interface:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableCellRenderer.html

public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column)

Returns the component used for drawing the cell. This method is used to
configure the renderer appropriately before drawing.

Parameters:
  table - the JTable that is asking the renderer to draw; can be null
  value - the value of the cell to be rendered. It is up to the
specific renderer to interpret and draw the value. For example, if value
is the string "true", it could be rendered as a string or it could be
rendered as a check box that is checked. null is a valid value
  isSelected - true if the cell is to be rendered with the selection
highlighted; otherwise false
  hasFocus - if true, render cell appropriately. For example, put a
special border on the cell, if the cell can be edited, render in the
color used to indicate editing
  row - the row index of the cell being drawn. When drawing the header,
the value of row is -1
  column - the column index of the cell being drawn

could have made it simpler by introducing a new simple class:
CellCoOrdinate - aside from encapsulating the x & y co-ords, it
simplifies the cell renderers interface, prevents a user from
accidentally getting the row and column values mixed up (as nothing to
differentiate between the two ints), etc.

public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               CellCoOrdinate)

We could also replace the isSelected and hasFocus booleans with a single
instance of a new class: CellSelectionProperties (or any better name)

leaving:

public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               CellSelectionProperties

cellSelectionProps,
                                               CellCoOrdinate coords)

so now 4 parameters instead of 6....

Andy
Bjorn Abelli - 10 Nov 2005 13:20 GMT
<Joey.Information@gmail.com> wrote...

> When I write Java code. I always meet a problem . How to assign value
> to a entity object attributes, Some person like to pass values as
> parameter of  constructor when create this entity bean, some persons
> like to create the object ,after that call setxxx().

And some choose something in between.

When talking about Java classes in general, my rule of thumb is to first
determine if the value of an attribute should be mandatory or optional. If
it's mandatory, I put it in the argument list of the constructor, if not, I
create a setter.

After a while I usually complement it with other constructors for
convenience.

// Bjorn A
Chris Uppal - 10 Nov 2005 13:46 GMT
>  When I write Java code. I always meet a problem . How to assign value
> to a entity object attributes, Some person like to pass values as
> parameter of  constructor when create this entity bean  , some persons
> like to create the object ,after that call setxxx().

Passing arguments to constructors is simple and reliable.  The downside is that
it puts very hard limits on how many settings can be passed (I'd mistrust any
number higher than about three), and that it can be inflexible.

Using lots of setXxx()'s is simple and flexible.  The downside is that there is
no way to ensure that data that /must/ be supplied actually is.  Also the set()
methods will still be available for use to change the object after it has been
initialised, which might well be something you'd prefer not to allow.

There are (at least) two patterns which allow flexibility and reliability.  The
downside is that they are not so simple to use, and require more work to set
up.

Simple, reliable, flexible; choose two.

The patterns are:

Use a parameter object.  It holds values for each of the settings.  You create
one, fill in whichever data you want, and then pass it as the single parameter
to the real constructor.  That constructor can then check that all the
necessary conditions are met before initialising the object from the data.

Use the Builder pattern.  A Builder is an object whose job is to manage the
creation of another object.  You create one, use its setXxx() methods to tell
it how you want the real object to be parameterised, and then ask it to create
the object.  It checks the validity and completion of the data, creates the
object and parameterises it (using a private API), and then returns the
completely initialised object.

There isn't, in fact, much difference between the two patterns.  Pretty much
the same code in both cases, just factored differently.

   -- chris
Thomas Schodt - 10 Nov 2005 14:00 GMT
> Hello all,
>
>  When I write Java code. I always meet a problem . How to assign value
> to a entity object attributes, Some person like to pass values as
> parameter of  constructor when create this entity bean  , some persons
> like to create the object ,after that call setxxx().

  <http://c2.com/cgi/wiki?ArgumentObject>
Thomas Schodt - 10 Nov 2005 14:07 GMT
>   <http://c2.com/cgi/wiki?ArgumentObject>

But beware of

  <http://c2.com/cgi/wiki?MagicContainer>
Joey.Information@gmail.com - 10 Nov 2005 15:54 GMT
Thanks everyone...
bartekkl@gmail.com - 10 Nov 2005 16:44 GMT
This is a very valuable pattern - but does it really help in this case?
ArgumentObject is an entity object itself, so, to create it, you need
to solve the original problem anyway.

Regards,

.b
Eike Preuss - 11 Nov 2005 12:55 GMT
> This is a very valuable pattern - but does it really help in this case?
> ArgumentObject is an entity object itself, so, to create it, you need
[quoted text clipped - 3 lines]
>
> .b

<quote author="Andrew McDonagh" src="someWhereElseInThisThread">
when I find I need to initialised/set 4 or more variables, regardless of
how (constructor or setter) its an indication that the class is probably
doing to much and not conforming to the Single Responsibility Principle.
Its an indication - not a universal truth - but one that often proves
correct.
When it happens, I pass a single instance of a new class which to
represents 2 or more of the original parameters.
</quote>

Don't put all parameters in the same parameter object, but split them up
in a sensible way.
Andrew McDonagh - 11 Nov 2005 20:18 GMT
>>This is a very valuable pattern - but does it really help in this case?
>>ArgumentObject is an entity object itself, so, to create it, you need
[quoted text clipped - 16 lines]
> Don't put all parameters in the same parameter object, but split them up
> in a sensible way.

thats what I said...well looking back I see I've inferred it.

"..I out 2 or more into a single instance of a new class .."

Also, the new class has to contain a logical subset of the parameters -
not arbitrary groupings.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.