Re: Generics and subclasses
- From: <Mike>
- Date: Sun, 30 Oct 2005 01:50:24 +0200
"EdwardH" wrote
>> Will I, as I have feared, have to make a subclass (extends) of Manager to
>> handle each type of Data?
>
> I've solved my problem, as elegantly as it appears to be possible.
>
> Manager handles Data.
>
> Manager1 handles Data1.
> Manager2 handles Data2.
>
> The add() method in Manager is overloaded in each of the subManagers. Not
> very elegant, forces one to keep track of all the manager subclasses but
> it's better than writing the same code over and over again.
Why don't you just add an argument to the add-method of your Manager class?
Class Manager {
private Vector<Data> dataVector;
add(Data data){
dataVector.add(data);
}
}
Data is an interface. You create instances of Data1, Data2, Data3 (all
implementing the Data interface) outside the Manager and add them using the
add method.
If you don't want to create the instances outside the Manager you still need
a way to tell the Manager what kind of Data must be created (unless it's ok
to create them randomly), so you need some kind of argument in the
add-method and use it to determine what kind of Data must be created.
.
- References:
- Re: Generics and subclasses
- From: EdwardH
- Re: Generics and subclasses
- Prev by Date: Re: Generics - good, bad or indifferent?
- Next by Date: Help in finding Journals related to Java Sound API
- Previous by thread: Re: Generics and subclasses
- Next by thread: Re: Generics and subclasses
- Index(es):
Relevant Pages
|
|