Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Q: class access

Reply
Thread Tools

Q: class access

 
 
Jakob Bieling
Guest
Posts: n/a
 
      06-24-2005
Hi,

I am in a situation where I need a language feature, which I am not
sure exists. I have two classes, one (let's say A) contains a list of
data and the other (say B) contains the functionality to display that
list. Now I want to have private data inside A, which should not be
accessed by user code. Problem is, that I want B to have access to this
data as well.
The only 'solution' I can come up with, is to make that data public
and document that it must not be modified. Does not feel like clean code
tho. I am also trying to avoid sophisticated work-arounds, since the
code will run on devices with very limited memory and cpu.

Thanks for your help!
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
 
 
 
Bjorn Borud
Guest
Posts: n/a
 
      06-24-2005
["Jakob Bieling" <(E-Mail Removed)>]
|
| The only 'solution' I can come up with, is to make that data public
| and document that it must not be modified. Does not feel like clean code
| tho. I am also trying to avoid sophisticated work-arounds, since the
| code will run on devices with very limited memory and cpu.

you can put the classes in the same package and use package-private
access, but what you *really* want to do is to think long and hard
about your design; if you need to expose the inner workings of one
class to another class, chances are that your code will be very hard
to debug, understand and maintain.
bottom line: _I do not recommend you do this_!


at the very least I would define a getter which returns a reference to
the data in question, using the most general data type you can live
with, so that access to the internals are defined, and if possible,
somewhat restricted. you could also ensure read-only access or make a
defensive copy of the data, but from what I understand, your
environment is memory-constrained so the defensive copy might defeat
the purpose.

for example if you have an instance of a HashMap inside one class that
you wish to return you could for instance do:

public Map getFoo() {
return Collections.unmodifiableMap(myHashMapInstance);
}

which would give you (reasonably) safe access to the underlying data
without making too many commitments.

-Bjørn


 
Reply With Quote
 
 
 
 
Bjorn Borud
Guest
Posts: n/a
 
      06-24-2005
[Bjorn Borud <(E-Mail Removed)>]
|
| public Map getFoo() {
| return Collections.unmodifiableMap(myHashMapInstance);
| }

actually, since you said "list" I would assume that the following
might be more along what you need:

public List getFoo() {
return Collections.unmodifiableList(myList);
}

-Bjørn
 
Reply With Quote
 
Tim Miller
Guest
Posts: n/a
 
      06-24-2005
Jakob Bieling wrote:
> Hi,
>
> I am in a situation where I need a language feature, which I am not
> sure exists. I have two classes, one (let's say A) contains a list of
> data and the other (say B) contains the functionality to display that
> list. Now I want to have private data inside A, which should not be
> accessed by user code. Problem is, that I want B to have access to this
> data as well.
> The only 'solution' I can come up with, is to make that data public
> and document that it must not be modified. Does not feel like clean code
> tho. I am also trying to avoid sophisticated work-arounds, since the
> code will run on devices with very limited memory and cpu.
>
> Thanks for your help!


You can use a method to access the data (which is good programming
practise anyway), but make the return value a copy of the list in
the method before it is returned. This will stop any user code
changing which elements are in the list, but will take up more memory
 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      06-24-2005
Hi,

thanks for the quick responses. I rethought my design and did find
the cause. The data in A that B needed to access, should have been in B
alltogether (because it partly resembled the displaying state of the
data). It also solved the other implementation problems that came along
with that

Thanks again
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
John Currier
Guest
Posts: n/a
 
      06-25-2005
That's a situation where I long for C++'s const keyword...you'd return
the collection of objects that can't be modified without explicitly
breaking rules.

John

 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      06-26-2005
"John Currier" <(E-Mail Removed)> writes:

> That's a situation where I long for C++'s const keyword...you'd return
> the collection of objects that can't be modified without explicitly
> breaking rules.


Check out java.util.Collections.unmodifiable*();
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-26-2005
On Fri, 24 Jun 2005 15:17:26 +0200, "Jakob Bieling"
<(E-Mail Removed)> wrote or quoted :

> Problem is, that I want B to have access to this
>data as well.


You can make the data protected, then do B extends A.

You might make B an inner class of A. You can create a small package
and put both A and B in it and make the data default.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-26-2005
On 24 Jun 2005 15:34:32 +0200, Bjorn Borud <(E-Mail Removed)> wrote
or quoted :

>at the very least I would define a getter which returns a reference to
>the data in question, using the most general data type you can live
>with, so that access to the internals are defined, and if possible,
>somewhat restricted. you could also ensure read-only access or make a
>defensive copy of the data, but from what I understand, your
>environment is memory-constrained so the defensive copy might defeat
>the purpose.


These getters and setters can be optimised out of existence by a
compiler like JET. They give you the flexibility to rearrange the data
structure of A without impacting B.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
John Currier
Guest
Posts: n/a
 
      06-29-2005
The unmodifiable* versions of the various collections do not protect
the contents of the collection, only the collection itself.

John
http://schemaspy.sourceforge.net

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
How do I let people access the internet via an access point but not allow them access to my network yar Wireless Networking 4 09-21-2004 03:48 AM



Advertisments