Dynamic decide derivated obj

F

firegun9

Hello,

I have a design problme in my project.
The abstract is below:

class Container{
Base* p_base;
void create_child(int type, argu1, argu2...)
{
switch(type){
case 0:
p_base = new Child_1(argu1, argu2);
break;
case 1:
p_base = new Child_2(argu1, argu2, argu3);
break;
...
}
}

There is a pointer of type "Base" in the class "Container", and I hope
the pointer can be used in any kind of child class derived from "Base"
class.
The switch-case above is the solution I could come up with.
But the disadvantage is if there is a new type child in the future, I
have to come back to add one more case in create_child().
Anyone has a better idea?
 
H

Howard

Hello,

I have a design problme in my project.
The abstract is below:

class Container{
Base* p_base;
void create_child(int type, argu1, argu2...)
{
switch(type){
case 0:
p_base = new Child_1(argu1, argu2);
break;
case 1:
p_base = new Child_2(argu1, argu2, argu3);
break;
...
}
}

There is a pointer of type "Base" in the class "Container", and I hope
the pointer can be used in any kind of child class derived from "Base"
class.
The switch-case above is the solution I could come up with.
But the disadvantage is if there is a new type child in the future, I
have to come back to add one more case in create_child().
Anyone has a better idea?

Here you're having the container create a child based on some knowlege given
to it by a calling function, right? So instead of that, why not have the
calling function create the type of child it wants (since it apparently
knows that info best), and simply pass a pointer to that object to the
container (or a reference or pointer to that pointer, if you want to
transfer ownership of the object to the container)? That's the way you do
it with, for example, a vector of pointers. You create the object yoursefl,
then pass the (base-class) pointer to the push_back function.

Alternatively, since it looks like you're only going to contain one instance
of the object (which makes me wonder why you need a container at all), you
could create derived versions of the Container class, each of which creates
the appropriate derived class of internal object (assigning it to the
base-container class' p_base pointer member).

But without knowing what the purpose of the container class is, I can't say
what solution might really be more appropriate.

-Howard
 
A

Andrew McDonagh

Hello,

I have a design problme in my project.
The abstract is below:

class Container{
Base* p_base;
void create_child(int type, argu1, argu2...)
{
switch(type){
case 0:
p_base = new Child_1(argu1, argu2);
break;
case 1:
p_base = new Child_2(argu1, argu2, argu3);
break;
...
}
}

There is a pointer of type "Base" in the class "Container", and I hope
the pointer can be used in any kind of child class derived from "Base"
class.
The switch-case above is the solution I could come up with.
But the disadvantage is if there is a new type child in the future, I
have to come back to add one more case in create_child().
Anyone has a better idea?

You seem to be describing a kind of Factory - where Container is the
factory and Base derived objects are what it creates.

The problem you have come across is one of statically defined
relationships between the factory and the objects it creates.

Take a look at the 'Abstract Factory' pattern and then see how the
abstract factory is given its concrete factories. Once we can give the
Abstract factory one or more concrete factories to use, we could change
the concrete factory(ies) at runtime or compile time to allow new types
to be used.

http://www.google.co.uk/search?hl=en&q=abstract+factory&btnG=Google+Search&meta=

HTH
Andrew
 
F

firegun9

sorry I didn't make it clear.
The container contains six objects. Four of them are derived from
base_1 type, and two of them are derived from base_2 type.

Actually, what I did before is constructing the six objects outside the
container, then pass the pointers into the container. However, it is
not intuitive. I think the objects contained in container should be
created, and deleted by container.
 
H

Howard

sorry I didn't make it clear.
The container contains six objects. Four of them are derived from
base_1 type, and two of them are derived from base_2 type.

Actually, what I did before is constructing the six objects outside the
container, then pass the pointers into the container. However, it is
not intuitive. I think the objects contained in container should be
created, and deleted by container.

Personally, I think it's much more intuitive to do it that way. Think of
any container in the real world. You put things in it, you don't ask it to
manufacture those things for you. (Silly, I know.) All the standard
containers (vector, list, etc) are used in this manner. You create the
objects that go into them. This eliminates any of that switch statement
stuff, which is what is *not* intuitive to me. Object-oriented code should
rarely, if ever, have switch statements to decide which derived class needs
to be handled, whether that's creating them, destroying them, or calling
their member functions. Just store pointers to base class objects, create
whatever specific derived objects you need, and pass the pointers to them to
your container. Neat and simple.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top