• 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

How to encapsulating a composition

 
Ranch Hand
Posts: 226
1
jQuery Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object representing a document that is composed of other objects. However a client should only every interact with a document's elements through the document object, and should not be able to access the elements in the composition.

What is the best way to encapsulate a composition?

For example below: should you, and if so how, encapsulate the getText() in Line so that only the Document can be its client.


[ December 17, 2008: Message edited by: marten kay ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marten kay:
...should you, and if so how, encapsulate the getText() in Line so that only the Document can be its client...


Strings are immutable, so if you simply "return text," that reference cannot be used to change the String referenced by the private variable "text."

If you were dealing with a mutable object, then one approach is to create a copy (clone) of the object and return a reference to that copy, so that the original cannot be altered.

(I think this is a little advanced for the Beginners forum, so I'm moving this to the Intermediate forum. Please continue there. )
 
marten koomen
Ranch Hand
Posts: 226
1
jQuery Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc

I may have chosen a bad example using an immutable text string.

In my case, the issue is not so much about a client corrupting a mutable object through accessing an element of a composition directly, my interest in saving the client from itself as it doesn't makes sense to access and element directly. So it is case of forcing good programming in this context.

But I gather from your response that it is standard practice to leave elements of composition exposed, especially if they are immutable. So I will leave it, it is just that it was beginning to bug me.

Cheers

Marten
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marten kay:
... I gather from your response that it is standard practice to leave elements of composition exposed, especially if they are immutable...


I wouldn't say "exposed," because the variables should be kept private.

If a "get" method is provided for a variable, then the goal is to return something that cannot be used to change the state of what the private variable is pointing to. If that's an object that is immutable, then you're okay in this respect, because a reference to that object cannot be used to alter it.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic