The Artima Developer Community
Sponsored Link

Legacy Design Forum
The Set/Get Question



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


The Set/Get Question

Posted by Steve on January 15, 2001 at 3:17 PM

I think the thing that nobody is mentioning is the fact that you do get a level of abstraction using get and set methods instead of just exposing the data directly. You can still change the representation of the data underneath or derive it without changing the get/set API, so client code can potentially remain as is.

I would say exposing the data (i.e. making it public) is evil, but using set/get methods is merely dangerous, but at times necessary.

> > Thanks for the reply Craig. I didn't think that I would
> > get any response because this topic was started over a year
> > ago in this forum.

> I'm just slow to respond sometimes. ;-)

> > I guess to really be independent of implementation,
> > we should make a class called Weight and our message would then
> > be
> > Weight addMyWeightToRunningSum( Weight running_sum )
> > and we can redirect all the implementation dependent
> > stuff to the Weight class. This approach seems rather anal
> > though.

> Perhaps, but from a pure OO standpoint it seems to me that it would be the most "correct" answer (especially if you have other classes that can accept Weight as a method parameter).

> > I'm afraid I must disagree with you about the "get"-ness
> > of addMyWeightToRunningSum(). At no point is the function
> > giving away its value

> Not true...addMyWeightToRunningSum( 0 ) is simply getWeight() in disguise.

> > But I suspect that there are other situations where you must
> > use get/set

> Precisely. The question is whether the get/set methods truly break encapsulation by unnecessarily exposing class data or whether they act as a service of the class by allowing controlled access to a meaningful subset of that data. In the case of a bank account, I would argue that access to balance information through a get method (I don't see the need for a direct set method off the top of my head) is a desired service of the class.

> More food for thought.

> Craig


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us