Re: Separation of API and implementation



hforco2@xxxxxxxxxxxxxx wrote:

I'm programming in Java, actually developing plugins in Eclipse, but I
think the general questions are relevant not only to Java.

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.

But the implementation in B only works if I assume Y is actually B's
implementation of Y, say YImpl.

Cows are Animals, Tigers are Animals, Hay is Food, Meat is Food, Animals
eat Food. It's an old conundrum. I'll be using the "Animals eat Food"
example below because it makes the problem a little more concrete than
your X and Y. :-)

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?

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?

The design is poor because you are mixing desperate abstractions. Cows
and Tigers can be treated the same only in certain contexts, Hay and
Meat can be treated the same only in certain contexts, but there is no
context in which both can be treated the same. (In the zoo, the
caretaker does not indiscriminately lump the hay with the meat and dump
shovelfuls of either in whatever cage happens to be closest. He has to
keep track of which cages hold HayEaters and which hold MeatEaters and
dole the proper food to the proper place.)

In the zoo metaphor above lies the answer to a proper design. However,
since we are talking about programs and not zoos, we have a few extra
resources available to us. Think of the six ways X can acquire a Y to
use:

1) X can create a Y inside 'foo'
2) X can create its Y as a contained object
3) X can be given its Y as a parameter of 'foo'
4) X can be handed a Y though some other method
5) X can use a global Y
6) X can ask a third party for its Y

For the first 2 cases, each animal simply makes its own food when told
to feed itself. Now the caretaker need not bother with what sub-type of
animal he is dealing with.

Case 3 is what you are dealing with now, the caretaker has to keep track
of which sub-type of Food goes with which sub-type of Animal.

Case 4 allows one to separate the concerns of deciding which food to
provide and deciding when to eat. I.E., someone other than the caretaker
keeps track of which sub-type of Food goes with which sub-type of
Animal, then the caretaker later tells all the animals to eat, confident
that they already have the proper food in their cages.

Case 5 requires all Animals to go to the same trough.

Case 6 is mentioned for completeness (it only postpones the problem,
because now we have to figure-out how X acquires access to the third
party.)

Think about the cases above, but however an X gets its Y, make sure that
It know that it has the proper Y without having to ask the Y object.
.



Relevant Pages

  • Re: Eating "Naturally"
    ... a long time before domesticated animals, agriculture, ... Their Food & Diet ... The Aborigines who lived in areas which included waterways such as ... Fish species were also caught by the use of fish ...
    (sci.med.nutrition)
  • Re: Caloric restriction and longevity?
    ... restriction in animals leads to significant increase in lifespans. ... The ALS society describes some possible causes of the motor neuron disease ... Enormous but mostly unnecessary and costly over food processing has ... E and omega oils, that also contain significant numbers of damaging ...
    (sci.bio.evolution)
  • NYTM: Kosher Wars
    ... The Food Issue ... Kastner spent the previous year studying how to slaughter animals ... according to Jewish law. ...
    (soc.culture.jewish)
  • Re: Front Garden and the Food Bill
    ... same food at all. ... Apart from the high color of the egg yolks, these eggs ... of the animals that eat that grass. ... that fish contain higher levels of the most valuable omega-3s than land ...
    (rec.gardens)
  • Re: Book Summary: Anger - Wisdom for Cooling the Flames
    ... Here Thich speaks of eating organically grown and free-range food. ... Vegans neither kill nor use animals. ... To me, eggs are for foetuses, milk for babies and meat for adults... ...
    (talk.religion.buddhism)