I am working on an assignment, and required is that I create two generic collections (called ComputerOrder and PartyTrayOrder) of several classes: ComputerPart, Peripheral, and Service, and and of Cheese, Fruit, and Service respectively. All of these classes (ComputerPart, Fruit, Service, etc) extend one class called Product.
The problem I have encountered is that it seems to be impossible to bound more than one class in Java, as you may only bound multiple interfaces, and I am not allowed to modify the existing class structure.
I've read around and seen it suggested that I create and interface that "codes in" all of the relevant classes, but I fail to see how this is possible without ComputerPart, Peripheral, etc ALSO being interfaces and then using the extend keyword. I've seen it further suggested that I create abstract classes called ComputerProduct and PartyTrayProduct, but I fail to see how this will in any way restrict the types.
I am not allowed to post images, but here is a class diagram, including some unrelated assignment objects
Here is my current code for ComputerOrder, though it only bounds the parent class product, meaning it isn't really as the assignment specifies.
public class ComputerOrder<T extends Product> extends GenericOrder<T> {
public ComputerOrder() {
super();
}
}
Here are the assignment specifications:
Design and implement a generic container called GenericOrder that acts as a collection of an arbitrary number of objects in Products.java. Design a mechanism that gives each instance of the container a unique identifier. You must use Java generics features.
Design and implement a subclass of GenericOrder called ComputerOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, and Service objects. Implement as many methods as necessary.
Design and implement a subclass of GenericOrder called PartyTrayOrder that takes an arbitrary number of different classes of Cheese objects, Fruit objects and Service objects. Implement as many methods as necessary. (...)
Here's the only solution that I came up with, which follows the assignment to the letter and still makes some sense (even though I think that it is not a "good" solution - see the notes below): The
ComputerOrderandPartyTrayOrdercould offer methods that only accept the specialized types ofProduct:This way, the order implementations could exactly accept the right types:
I assume that this is the intended solution, because the assignment contains the broad hint:
So most likely, the goal is not to implement one method that is magically bounded for multiple classes. Instead, one method for each "category" has to be added.
An aside: My gut feeling is that this design could be improved. Just imagine you'd have to create classes
CarOrderandFahsionOrderetc. for new types of orders. Or imagine thePartyTrayOrderwould have to be extended to also handle classes likeMeatorDiporSalad: You'd end up with dozens of classes with dozens of specialized methods.This could all be avoided by introducing a dedicated "product type" that exactly matches the types that are acceptable for specific "order type". So I think that (
Productshould be an interface to begin with, and) there should be interfaces likeComputerProductandPartyTrayProduct, as inThat way, the required bounds for the specific
ComputerOrderandPartyTrayOrderare already modeled with the class hierarchy. The advantage then is: You don't need theComputerOrderandPartyTrayOrderclasses any more! TheGenericOrdercould then be non-abstract, and in order to create the specific types of orders you'd just properly use the generic type bound.Here is a complete example, where I've just thrown in
Saladas a newPartyTrayProduct, andCarPartas a new type of product, without having to extend or modify any of the "infrastructure classes":