Q: class access

Discussion in 'Java' started by Jakob Bieling, Jun 24, 2005.

  1. 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)
    Jakob Bieling, Jun 24, 2005
    #1
    1. Advertising

  2. Jakob Bieling

    Bjorn Borud Guest

    ["Jakob Bieling" <>]
    |
    | 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
    Bjorn Borud, Jun 24, 2005
    #2
    1. Advertising

  3. Jakob Bieling

    Bjorn Borud Guest

    [Bjorn Borud <>]
    |
    | 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
    Bjorn Borud, Jun 24, 2005
    #3
  4. Jakob Bieling

    Tim Miller Guest

    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
    Tim Miller, Jun 24, 2005
    #4
  5. Re: class access

    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)
    Jakob Bieling, Jun 24, 2005
    #5
  6. Jakob Bieling

    John Currier Guest

    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
    John Currier, Jun 25, 2005
    #6
  7. "John Currier" <> 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*();
    Tor Iver Wilhelmsen, Jun 26, 2005
    #7
  8. Jakob Bieling

    Roedy Green Guest

    On Fri, 24 Jun 2005 15:17:26 +0200, "Jakob Bieling"
    <> 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/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jun 26, 2005
    #8
  9. Jakob Bieling

    Roedy Green Guest

    On 24 Jun 2005 15:34:32 +0200, Bjorn Borud <> 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/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jun 26, 2005
    #9
  10. Jakob Bieling

    John Currier Guest

    The unmodifiable* versions of the various collections do not protect
    the contents of the collection, only the collection itself.

    John
    http://schemaspy.sourceforge.net
    John Currier, Jun 29, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonas
    Replies:
    1
    Views:
    392
    S. Justin Gengo
    Aug 12, 2003
  2. E11
    Replies:
    1
    Views:
    4,751
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    750
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    590
  5. xz
    Replies:
    9
    Views:
    530
    James Kanze
    Jan 8, 2008
Loading...

Share This Page