I am given a class Shop
that maintains a collection of items of type T
. It provides buy and sell functions for single or multiple items and uses a List<T>
as a container for buying and selling:
import java.util.*;
public class Shop<T> {
List<T> stock;
public Shop() { stock = new LinkedList<T>(); }
public T buy() {
return stock.remove(0);
}
void sell(T item) {
stock.add(item);
}
void buy(int n, List<T> items) {
for (T e : stock.subList(0, n)) {
items.add(e);
}
for (int i=0; i<n; ++i) stock.remove(0);
}
void sell(List<T> items) {
for (T e : items) {
stock.add(e);
}
}
}
Now, I need to modify this class so that I can buy/sell items with any Collection
type...not just List
. I figured I would start by commenting out most everything and trying to convert things one by one, beginning with the stock
:
public class Shop<T> {
// List<T> stock;
Collection<T> stock;
// public Shop() { stock = new LinkedList<T>(); }
public Shop() { stock = new Collection<T>(); }
...
...
}
The first declaration works, but trying to instantiate the interface in the constructor, as expected, does not work. But as far as I can tell, stock
needs to be a Collection
so that I can use any Collection
subtype in the other functions that deal with it. And I'm pretty sure I cannot use a wildcard as a type parameter in this case. So what exactly can I do to construct the stock
here...or how should I declare the stock
differently in the first place?
The actual collection implementation you use inside your class is completely immaterial to the clients/users of the class as long as your interface definition accepts Collection
, i.e.
void buy(int n, List<T> items)
void sell(List<T> items)
should be
void buy(int n, Collection<T> items)
void sell(Collection<T> items)
This will not restrict anyone to use only List
types. Then, your internal member stock
can be and should be instantiated with any concrete subtype of Collection
.