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?
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>
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 ]
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
Joined: Mar 05, 2005
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.
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com