• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data Transfer Object query

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Annie Smith
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic