Using Interfaces to shield of custom data objects

Discussion in 'Java' started by ru, Jun 24, 2004.

  1. ru

    ru Guest

    We use a custom java data object to exchange data between different
    modules. This was developed with several getters and setters and all
    modues with a reference to such an object could use the setters to change
    the content.


    Now, we need to provide some modules read only access to that data object.

    I was trying to use Interfaces to do this, e.g.
    IOntoBaseSubsetReadOnly declares only the getters from OntoBaseSubset.
    Thus, when I pass on a reference from a normal OntoBaseSubset instance as
    a reference to an IOntoBaseSubsetReadOnly this can only be used to read
    from the instance, not write to it.

    One of the OntoBaseSubset getters return a LexonSet, for which also a
    read only interface, ILexonSetReadOnly exists.

    Now, the problem :
    the getter method in OntoBaseSubset is defined as :
    public LexonSet getLexonSet(){...}
    the getter method in IOntoBAseSubsetReadOnly is defined as :
    public ILexonSetReadOnly getLexonSet();

    During compilation an error is thrown, saying that the return type of
    OntBaseSubset.getLexonSet() is incompatible with ILexonSetReadOnly.

    While the return type of OntoBaseSubset.getLexon() actually implements
    ILexonSetReadOnly!

    Does anybody have an idea how I can help this problem. Or an idea bout
    another strategy for providing read only access to some instances without
    any recoding?

    regards,
    Ruben

    --
    ---------------------------------------------------
    | Ruben Verlinden STARLab V.U.B. |
    | E-mail: phone: +32 (0)2 629 37 50 |
    | GnuGP ID 032901CE ICQ: 108146714 |
    | Key fingerprint = 71DC 0F5A 20C0 B76D 52B3 1ECF 8B8E 79BA 0329 01CE |
    | |
    | ---------------------------------------------------- |
    | Back when I was a boy, it was 40 miles to everywhere, uphill both |
    | ways and it was always snowing. |
    | |
    ----------------------------------------------------
    ru, Jun 24, 2004
    #1
    1. Advertising

  2. ru

    VisionSet Guest

    "ru" <> wrote in message
    news:p...
    >
    >
    > We use a custom java data object to exchange data between different
    > modules. This was developed with several getters and setters and all
    > modues with a reference to such an object could use the setters to change
    > the content.
    >
    >
    > Now, we need to provide some modules read only access to that data object.
    >
    > I was trying to use Interfaces to do this, e.g.
    > IOntoBaseSubsetReadOnly declares only the getters from OntoBaseSubset.
    > Thus, when I pass on a reference from a normal OntoBaseSubset instance as
    > a reference to an IOntoBaseSubsetReadOnly this can only be used to read
    > from the instance, not write to it.
    >
    > One of the OntoBaseSubset getters return a LexonSet, for which also a
    > read only interface, ILexonSetReadOnly exists.
    >
    > Now, the problem :
    > the getter method in OntoBaseSubset is defined as :
    > public LexonSet getLexonSet(){...}
    > the getter method in IOntoBAseSubsetReadOnly is defined as :
    > public ILexonSetReadOnly getLexonSet();
    >
    > During compilation an error is thrown, saying that the return type of
    > OntBaseSubset.getLexonSet() is incompatible with ILexonSetReadOnly.
    >
    > While the return type of OntoBaseSubset.getLexon() actually implements
    > ILexonSetReadOnly!
    >
    > Does anybody have an idea how I can help this problem. Or an idea bout
    > another strategy for providing read only access to some instances without
    > any recoding?
    >


    The simplest approach would be to supply an example in code. Supply all
    classes and interfaces but cut them all down to the bare minimum.
    I don't think there is anything wrong with your approach. It just sounds
    like a language problem. If LexonSet is a class, how can it know what other
    classes implement an interface that it implements? But it would be easier
    to comment if I could see actual code.

    --
    Mike W
    VisionSet, Jun 24, 2004
    #2
    1. Advertising

  3. if i understand you right, you are trying to return a more specific type
    in an overridden method. this works only in java 1.5.

    /* example - works only in java 1.5 */
    class A {}
    class B extends A {}
    class X {
    A f() { ... }
    }
    class Y extends X
    B f() { ... }
    }

    to solve your problem in java 1.4,
    you could provide a different method (different name) in your subclass
    which returns the read/write object type.

    regards
    fabian

    ru wrote:
    >
    > We use a custom java data object to exchange data between different
    > modules. This was developed with several getters and setters and all
    > modues with a reference to such an object could use the setters to change
    > the content.
    >
    >
    > Now, we need to provide some modules read only access to that data object.
    >
    > I was trying to use Interfaces to do this, e.g.
    > IOntoBaseSubsetReadOnly declares only the getters from OntoBaseSubset.
    > Thus, when I pass on a reference from a normal OntoBaseSubset instance as
    > a reference to an IOntoBaseSubsetReadOnly this can only be used to read
    > from the instance, not write to it.
    >
    > One of the OntoBaseSubset getters return a LexonSet, for which also a
    > read only interface, ILexonSetReadOnly exists.
    >
    > Now, the problem :
    > the getter method in OntoBaseSubset is defined as :
    > public LexonSet getLexonSet(){...}
    > the getter method in IOntoBAseSubsetReadOnly is defined as :
    > public ILexonSetReadOnly getLexonSet();
    >
    > During compilation an error is thrown, saying that the return type of
    > OntBaseSubset.getLexonSet() is incompatible with ILexonSetReadOnly.
    >
    > While the return type of OntoBaseSubset.getLexon() actually implements
    > ILexonSetReadOnly!
    >
    > Does anybody have an idea how I can help this problem. Or an idea bout
    > another strategy for providing read only access to some instances without
    > any recoding?
    >
    > regards,
    > Ruben
    >
    =?ISO-8859-1?Q?Fabian_B=FCttner?=, Jun 24, 2004
    #3
  4. ru

    ru Guest

    On Thu, 24 Jun 2004 14:45:21 +0100, VisionSet wrote:

    >
    >
    > "ru" <> wrote in message
    > news:p...
    >>
    >>
    >> We use a custom java data object to exchange data between different
    >> modules. This was developed with several getters and setters and all
    >> modues with a reference to such an object could use the setters to change
    >> the content.
    >>
    >>
    >> Now, we need to provide some modules read only access to that data object.
    >>
    >> I was trying to use Interfaces to do this, e.g.
    >> IOntoBaseSubsetReadOnly declares only the getters from OntoBaseSubset.
    >> Thus, when I pass on a reference from a normal OntoBaseSubset instance as
    >> a reference to an IOntoBaseSubsetReadOnly this can only be used to read
    >> from the instance, not write to it.
    >>
    >> One of the OntoBaseSubset getters return a LexonSet, for which also a
    >> read only interface, ILexonSetReadOnly exists.
    >>
    >> Now, the problem :
    >> the getter method in OntoBaseSubset is defined as :
    >> public LexonSet getLexonSet(){...}
    >> the getter method in IOntoBAseSubsetReadOnly is defined as :
    >> public ILexonSetReadOnly getLexonSet();
    >>
    >> During compilation an error is thrown, saying that the return type of
    >> OntBaseSubset.getLexonSet() is incompatible with ILexonSetReadOnly.
    >>
    >> While the return type of OntoBaseSubset.getLexon() actually implements
    >> ILexonSetReadOnly!
    >>
    >> Does anybody have an idea how I can help this problem. Or an idea bout
    >> another strategy for providing read only access to some instances without
    >> any recoding?
    >>

    >
    > The simplest approach would be to supply an example in code. Supply all
    > classes and interfaces but cut them all down to the bare minimum.
    > I don't think there is anything wrong with your approach. It just sounds
    > like a language problem. If LexonSet is a class, how can it know what other
    > classes implement an interface that it implements? But it would be easier
    > to comment if I could see actual code.


    At the moment I'm implementing adapters to avoid the problems.

    But I don't get why the first approach won't work :
    1) LexonSet implements ILexonSetReadOnly
    2) ILexonSetReadOnly lSet = new LexonSet() :works

    But,
    1) LexonSet implements ILexonSetReadOnly
    2) ILexonSet declares a function :
    public ILexonSetReadOnly getLexonSet();
    3) LexonSet implements the following function :
    public LexonSet getLexonSet();
    When compiling I get an error that the return type of
    LexonSet.getLexonSet() (Which is a LexonSet) is not compatible with
    ILexonSetReadOnly (which is actually implemented by LexonSet!)


    regards,
    Ruben



    --
    ---------------------------------------------------
    | Ruben Verlinden STARLab V.U.B. |
    | E-mail: phone: +32 (0)2 629 37 50 |
    | GnuGP ID 032901CE ICQ: 108146714 |
    | Key fingerprint = 71DC 0F5A 20C0 B76D 52B3 1ECF 8B8E 79BA 0329 01CE |
    | |
    | ---------------------------------------------------- |
    | Computers are like air conditioners |
    | They stop working when you open Windows |
    | |
    ----------------------------------------------------
    ru, Jun 24, 2004
    #4
  5. ru

    VisionSet Guest

    "ru" <> wrote in message
    news:p...
    > On Thu, 24 Jun 2004 14:45:21 +0100, VisionSet wrote:


    > At the moment I'm implementing adapters to avoid the problems.
    >
    > But I don't get why the first approach won't work :
    > 1) LexonSet implements ILexonSetReadOnly
    > 2) ILexonSetReadOnly lSet = new LexonSet() :works
    >
    > But,
    > 1) LexonSet implements ILexonSetReadOnly
    > 2) ILexonSet declares a function :
    > public ILexonSetReadOnly getLexonSet();
    > 3) LexonSet implements the following function :
    > public LexonSet getLexonSet();
    > When compiling I get an error that the return type of
    > LexonSet.getLexonSet() (Which is a LexonSet) is not compatible with
    > ILexonSetReadOnly (which is actually implemented by LexonSet!)
    >


    Like I said actual code would be clearer.
    But I suspect, that your interface - ILexonSetReadOnly does not specify the
    method public LexonSet getLexonSet(); Or rather more to the point you are
    not implementing ILexonSetReadOnly.

    please provide the *exact code* including interface, class declarations and
    those relevent methods.

    --
    Mike W
    VisionSet, Jun 24, 2004
    #5
  6. ru

    Roedy Green Guest

    On Thu, 24 Jun 2004 14:58:00 +0200, ru <> wrote or
    quoted :

    >Now, the problem :
    > the getter method in OntoBaseSubset is defined as :
    > public LexonSet getLexonSet(){...}
    > the getter method in IOntoBAseSubsetReadOnly is defined as :
    > public ILexonSetReadOnly getLexonSet();
    >
    > During compilation an error is thrown, saying that the return type of
    > OntBaseSubset.getLexonSet() is incompatible with ILexonSetReadOnly.
    >
    > While the return type of OntoBaseSubset.getLexon() actually implements
    > ILexonSetReadOnly!
    >
    >Does anybody have an idea how I can help this problem.


    you need an extra wrapper method that does this:

    public ReadOnlyInterface getLookOnly()
    {
    return (ReadOnlyInterface) getMutableThing();
    }

    This does not stop malice. He can always cast it back, but at least is
    stops him accidentally mutating it.

    Another way to solve this is to create a nested pair of classes. The
    core contains only the immutable methods and a protected constructor.
    The outer class exends the first and adds the mutator methods

    You return an immutable core class reference to the full mutator
    object. Again there is nothing stopping the client from casting it
    back to a full mutator.

    To be really tight you have to copy the fields from the expanded
    mutator class back to a new immutable core object, and return that.
    That can't be cast back.

    The catch is, this is a maintenance nightmare. You have to keep track
    of every field to make sure it is individually copied, and any new
    fields get dealt with too.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 24, 2004
    #6
    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. David Waz...

    Install Shield

    David Waz..., Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    432
    Yan-Hong Huang[MSFT]
    Jul 10, 2003
  2. 7stud
    Replies:
    11
    Views:
    676
    Dennis Lee Bieber
    Mar 20, 2007
  3. q

    Shield file from download?

    q, Dec 3, 2008, in forum: ASP .Net
    Replies:
    5
    Views:
    341
    Gregory A. Beamer
    Dec 4, 2008
  4. Ra Carter
    Replies:
    1
    Views:
    716
    sloan
    Mar 17, 2010
  5. Josema

    using Interfaces with web custom controls

    Josema, Jun 16, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    105
    Josema
    Jun 16, 2004
Loading...

Share This Page