Re: Calling methods based on derived type
On Apr 27, 4:09 pm, Juli=E1n Albo <JULIANA...@terra.es> wrote:
flopbucket wrote:
I guess that you have written the processObject() functions. Your
difficulty exhibits a design problem rather than an implementation
problem. Your FactoryMethod returns a pointer to an interface (B). In
'classic' OOP style you are supposed to program against the interface,
not an implementation (C, D, E, ...). The problem can be solved by
either re-writing the processObject() functions to one function that
accepts the interface (processObject(B*)) or by writing special
factory methods for derived classes (e.g. FactoryMethodForC()) and
preserve the type information throughout the program. The latter is of
course 'less OO'.
Well, actually I didn't write either, but am tasked with glueing the
two together. The processObject() methods already exist from a legacy
application, and I need to call the correct one based on the object
created by the factory.
You can create a new hierarchy of classes, a base with all member functio=
ns
required in the original interface, and a processObject virtual member, a=
nd
derived classes with his own processObject member that call corresponding
the non-member processObject function. Then make a factory that return
object from this hierarchy that contains (or have a pointer, smart pointe=
r,
whatever way you want) the corresponding object in the original hierarchy.
The drawback is that you have a lot of boring code to write.
Not that much, really, since all you need is a template, and to
instantiate that. On the other hand, he will have to duplicate
whatever logic is currently present in SomeFactoryMethod for
choosing the type. If the logic is simple, and based on easily
available data, fine. If not, he may still need to call
SomeFactoryMethod, obtain whatever type, and then use a map to
determine which of his types he needs to create.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34