In my application I have a bunch of object of different types: say A, B, C, D. Some of those objects can have a "link" with the others object meaning that, as example, the object A can own a set of the objects of type B.
I personally handled this problem creating an interface:
public interface Linkable<T extends Item>{
public link(List<T extends Item>);
}
So that the object which can have a link with the others have to implement this interface: let's say A, B and C implements Linkable
Moreover, I want to restrict the type of the objects that can be linked to some objects. As example, I want that objects of type A can be only linked to objects of type B and viceversa (the link always works in both ways).
My idea was to create then a hierarchy of interfaces in order to allow only specific type of links: Let's say:
ITEM
|
------------
| |
I1 I2
A implements I1, I2
B implements I1
C implements I2
My example purpose is that instances of B can only be linked with instances of A, while instances of C cannot be linked with instances of B.
I personally perceive some holes in this design. In fact, I think that there is a need for more constraints I am not considering. When I will override the Linkable::link
method in the classes A, B, C , it will have to accept subtypes of Item so, in the case of C, I will be able to add a link to instances of B, simply because I1
and I2
are both subtype of ITEM
.
Moreover, I feel that also the Item
type should be constrained to those who implement the Linkable
interface.
How can I restrict the types during override? Am I missing something?
Aucun commentaire:
Enregistrer un commentaire