aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Data Transfer Object query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Data Transfer Object query" Watch "Data Transfer Object query" New topic
Author

Data Transfer Object query

Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
We have are building a J2ee application with a lot of data transfer objects who have private attributes with public getter and setter methods,the normal stuff.
One of the review comments which we got is that why to use the getter and setter methods instaed make the private attributes public instead of private and use them to set and get the data just like struct.
Is that a better approach or using getter and setter is better?
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Make the properties of Data Transfer Object public has the advantage of simplicity. However it might limit the reusability of these objects.

Firstly, obviously when using public attributes, the read/write actions towards these attributes are uncontrolled. Imagine that you need to have immutable data transfer object where the contents not suppose to change.

Secondly, the separation of interface and implementation. You might want to use different storage mechanism internally in the data transfer object but expose them using different types.

And, lastly there are more reasons of why you are recommended against using public attributes and too hard to list them all here. Basic tenet is to follow proper object oriented design principles.

Hope it's helpful.


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
S Venkatesh
Ranch Hand

Joined: Jun 27, 2005
Posts: 464

Hai,

I recently read an article "why getter and setter methods are evil?".

http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

and see this also
http://weblogs.java.net/blog/editors/archives/2003/09/the_evil_umpire.html


Regards
Venkatesh S
[ October 17, 2005: Message edited by: Venkatesh Sai ]
Annie Smith
Ranch Hand

Joined: Mar 05, 2005
Posts: 172
So what did you conclude (or confuse) from that article?!?

The article talks about a good OO practice. Simply having public getters and setters is IMO equivalent to providing a public access to the corresponding member variables. The getters should be used to fetch some information about the object, which may not essentially be contained in one member variable. Similarly, setters should be used to set the state of some variable and perform some computations on other member variables (where the implementation details need not be known).

In several applications, getters and setters are provided to only fetch / set the member variables, when the purpose is to contain the data (without the need of any computation / logic). The article and its author adopts a pedantic approach, which may not be necessary in all the cases - though I often agree to that viewpoint.
[ October 17, 2005: Message edited by: Annie Smith ]

Cheers!<br /><b>Annie</b>
S Venkatesh
Ranch Hand

Joined: Jun 27, 2005
Posts: 464

Hai,

1. In general scenario where the producer itself is not the consumer, it is better to have getter and setter methods. May be in some frameworks u write that is used by many applications.

2. In scenario where in u just write some java beans to tranfer some data across/within your application, u need not have getter and setters, u can directly make your class variables public.

This is my conclusion.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
In my experience, there is rarely a need to have any setters in DTOs. To pick up on one of Eddy's points, see if the DTO can be made immutable (which is possible in most cases). If so, then why not make it immutable (by tightly encapsulate the class, declaring no setters and preventing extensibility) and add only those getters which are needed.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Annie Smith
Ranch Hand

Joined: Mar 05, 2005
Posts: 172
Originally posted by Venkatesh Sai:

2. In scenario where in u just write some java beans to tranfer some data across/within your application, u need not have getter and setters, u can directly make your class variables public.


In my experience, it is better to have getter/setter methods in the first place. If some computational logic related to the data is to be added later on (possibly due to a change in the requirement), the bean class should be able to handle that without impacting the complete application.

Making member variables public instead of having a get/set method is quite a micro-optimization.
S Venkatesh
Ranch Hand

Joined: Jun 27, 2005
Posts: 464

Originally posted by Gavi Raaghav:
We have are building a J2ee application with a lot of data transfer objects who have private attributes with public getter and setter methods,the normal stuff.
One of the review comments which we got is that why to use the getter and setter methods instaed make the private attributes public instead of private and use them to set and get the data just like struct.
Is that a better approach or using getter and setter is better?


I strongly belive that in this scenario he need not use the getter and setter methods. I was more specific to his quote.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Transfer Object query