Q: class access

J

Jakob Bieling

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!
 
B

Bjorn Borud

["Jakob Bieling" <[email protected]>]
|
| 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
 
B

Bjorn Borud

[Bjorn Borud <[email protected]>]
|
| 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
 
T

Tim Miller

Jakob said:
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
 
J

Jakob Bieling

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 :)
 
J

John Currier

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
 
T

Tor Iver Wilhelmsen

John Currier said:
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*();
 
R

Roedy Green

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/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Roedy Green

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/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top