This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

bad encapsulation why? need help!

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me why the attribute is badly encapsulated? The only thing I see is that there is a missing constructor, but apparently, it's more than that.
Thanks.
Here is the code:
class objectContainingDate
{
private Date myDate;
public void setDate(Date theDate)
{
myDate = the Date;
}
public Date getDate()
{
return myDate;
}
}
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the date class is mutable so there is nothing stopping a user from getting the date object and changing it without the class knowing...

Dave
 
Joanne Fire
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David!

Originally posted by David O'Meara:
[B]the date class is mutable so there is nothing stopping a user from getting the date object and changing it without the class knowing...

Dave[/B]


 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of solutions to your problem...
You can use composition to create a version of Date that is immutable (hides state changing methods) like this:

The problem withtis is that it is NOT a java.util.Date or java.sql.Date
Alternatively you can make your class return DEFENSIVE COPIES of the date. ie it returns a new instance with the same value.

The third way (off the top of my head) is to merge your original class and the Date composition from my first example. ie it will have the usual calls, and the Date calls as well, which get mapped to the inner instance.
Dave.
(ps: everything I know I leant from Effective Java by Joshau Bloch, see the Javaranch Bunkhouse )
 
reply
    Bookmark Topic Watch Topic
  • New Topic