Using Interfaces to shield of custom data objects

R

ru

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: (e-mail address removed) 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. |
| |
----------------------------------------------------
 
V

VisionSet

ru said:
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.
 
?

=?ISO-8859-1?Q?Fabian_B=FCttner?=

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
 
R

ru

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: (e-mail address removed) 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 |
| |
----------------------------------------------------
 
V

VisionSet

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.
 
R

Roedy Green

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.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top