• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Read-only objects in cache

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'd like a bit of a sanity check and some help.

We have an object that's reasonably big and expensive to construct (processing and database usage). This object basically provides a structure for the system to work - the objects are referenced to perform operations, but never changed.

So, this expensive object can be cached and shared between all users as it should not be changed by any user operation.

So my question is: is there a way to ensure the application only reads from this object (and it's sub-object arrays), but never changes the object or any of it's sub-objects?

Basically, I need to create a read-only version of the object (and it's arraylists of objects).

Any help is much appreciated on how best to achieve this?

Cheers,

Steve
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the object immutable (think of String as an example).
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Making the structure final ought to be sufficient.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if the object has public variables or setter methods, then it isn't read-only. I'd recommend that the class:

be declared final
have no setters
be tightly encapsulated
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>is there a way to ensure the application only reads from this object (and >it's sub-object arrays), but never changes the object or any of it's >sub-objects?

Arrays? Don't expose your data structure's original arrays because arrays are never "read only":


>Basically, I need to create a read-only version of the object (and it's arraylists of objects).

Check out java.util.Collections' unmodifiableList method.
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:
But if the object has public variables or setter methods, then it isn't read-only.


I feel like I missed something. Was there a suggestion that the structure in question have either of these things?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve wants his application to only read from this object (and it's sub-object arrays), but never change the object or sub-objects. So, one of the things that the developer needs to do is ensure that a client cannot change the object's variables. Hence my recommendation that the class be immutable.

Jeff has made a good point that any mutable objects referenced by private variables need to be defensively copied in the getters. I think that a class written like this should be as close to read-only as possible.
 
reply
    Bookmark Topic Watch Topic
  • New Topic