Re: Separation of API and implementation
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Tue, 14 Aug 2007 16:18:01 +0200
On Tue, 14 Aug 2007 13:58:13 -0000, hforco2@xxxxxxxxxxxxxx wrote:
I have an API defined in one plugin, in a set of packages. Call this
plugin A.
Another plugin contains my implementation, in a set of different
packages. Call this plugin B.
I have interfaces X and Y in A where X is defined as follows:
interface X {
void foo(Y y);
}
Now I implement X in B as follows:
class XImpl implements X {
public void foo (Y y) {
// Do something with Y
}
}
The problem I have is the 'Do something with Y'. Y is an interface and
has been designed to be reasonably generic i.e. not specific to the
implementation B.
= bad design
IF foo's implementation depends on both Y and X THEN foo is double
dispatching. HENCE X and Y cannot be independent interfaces, because they
are coupled through common foo.
But the implementation in B only works if I assume Y is actually B's
implementation of Y, say YImpl.
So 'Do something with Y' becomes:
// Cast to my implementation:
YImpl myImpl = (YImpl) y;
This raises several questions:
Am I ok to assume the cast will work?
No, you cannot, because nothing in your design warranties that.
This is presumably quite a common problem. In cases like this, is it
accepted that you won't get more than one implementation being mixed
with another, i.e. if I get passed back an interface from A then I can
assume it is my B implementation?
This is the problem of parallel types hierarchies which represents a
specific case of general multiple dispatch. It is difficult in any
language. Sometimes delegation or generics help, sometimes not.
But often hierarchies aren't not really parallel. Either X or Y could be
designed class-wide. Then you dispatch along the opposite axis and no
second dispatch (poorly emulated by a cast in your example) is needed.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- Follow-Ups:
- Re: Separation of API and implementation
- From: hforco2
- Re: Separation of API and implementation
- References:
- Separation of API and implementation
- From: hforco2
- Separation of API and implementation
- Prev by Date: Separation of API and implementation
- Next by Date: Re: Separation of API and implementation
- Previous by thread: Separation of API and implementation
- Next by thread: Re: Separation of API and implementation
- Index(es):
Relevant Pages
|