Re: Generics and subclasses



"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.



.



Relevant Pages

  • Re: How to create large files with c?
    ... My manager told me to create a large file of 2GB ... and the secod 1GB is filled with "World" ... Sorry to say that I don't know how to do that in an elegant way. ... int main ...
    (comp.lang.c)
  • Re: How to create large files with c?
    ... My manager told me to create a large file of 2GB ... and the secod 1GB is filled with "World" ... Sorry to say that I don't know how to do that in an elegant way. ... int main ...
    (comp.lang.c)
  • Re: Generics and subclasses
    ... Manager2 handles Data2. ... The addmethod 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. ...
    (comp.lang.java.programmer)
  • Re: It it possible to redirect events?
    ... the Manager, and the Manager has many 'Items'. ... I'm going to assume that what you want is an event in the Manager class ... for clients of the Manager class (specifically, ... 4.Client should respond to ItHappened events. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: It it possible to redirect events?
    ... the Manager, and the Manager has many 'Items'. ... For one, the code you posted wouldn't compile (the Manager class constructor needs a "()" after the name of the constructor), and even if it did, it wouldn't do anything because the instance of Item isn't initialized, nor is it referenced anywhere other than in a local variable. ... But assuming you really want to get rid of the forwarding, you could implement your event explicitly in the Manager class, subscribing to the underlying Item event. ... Note that this actually changes the reference relationships between your instances. ...
    (microsoft.public.dotnet.languages.csharp)