Yesterday I asked this question, and the solution posted by @JB Nizet worked perfectly. However, that answer, as well as a few other answers/comments got me thinking in a different direction altogether.
Essentially, I have the following classes:
LoadHttpLoad extends LoadTargetHttpTarget extends TargetController
The Controller‘s job is to Target::fire() a Load, and doesn’t care which Target is firing which Load:
// Inside Controller.java
Target target = getTarget();
Load load = getLoad();
target.fire(load);
However, I might some day write a FtpLoad extends Load, and I don’t want to be able to fire a FtpLoad at an HttpTarget. So the essence of the above-referenced question was how do I do this, to which, the answer was generics.
However, as the answerer pointed out, this solution is a violation of the Liksov Substitution Principle. Other answerer/commenters seemed to indicate that what I was doing wasn’t necessarily good OOP practices.
So now I’m asking: how do I expose an API so that the Controller can be Load– and Target-agnostic, but still enforce that the proper Load subclass is fired on the proper Target type, all without violating Liskov Substitution?
And, if this is impossible to do (without violating Liskov), then what is the normal approach to a problem like this? Thanks in advance!
The easy way is to do some checking in your code to make sure the classes match up. You can use the
instanceofkeyword to check if it’s the correct class.