473,910 Members | 7,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic decide derivated obj

Hello,

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

class Container{
Base* p_base;
void create_child(in t 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?

Jul 23 '05 #1
4 1273

<fi******@yahoo .com.tw> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello,

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

class Container{
Base* p_base;
void create_child(in t 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

Jul 23 '05 #2
fi******@yahoo. com.tw wrote:
Hello,

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

class Container{
Base* p_base;
void create_child(in t 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...e+Search&meta=

HTH
Andrew

Jul 23 '05 #3
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.

Jul 23 '05 #4

<fi******@yahoo .com.tw> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
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


Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2906
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when the user selects shirts another combo box called 'size' would contain sizes in relation to shirts (ie. chest/neck size). the same would occur for trousers and hats. when the user selects an option in the garment combo box, the options available...
7
3425
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password are input in login screen. The password in the database is a number field. I am writing the dynamic sql statement as follows below. I believe I am going wrong in the password section of the code. I appreciate any help. Thanks. Regards.
9
3098
by: firegun9 | last post by:
Hello all, After looking for the solution for a while in the group, I know this is not possible in C++. So I just say my problme here. The input file is: dev1 1 1 dev2 0 0 0 6 There is a base class "Base" and two derivated classes "dev1" and
2
8866
by: klh | last post by:
We use DB2 Connect v 7.2 FP7 in Windows NT hitting a OS/390 DB2 v7.1 database. We have a Websphere (java) application that issues dynamic SQL. Most of the time when we issue dynamic SQL SELECT statements, like through a DB2 command window, the command will be processed using a package like SQLLF000 which uses an isolation level of Cursor Stability. However sometimes in the Websphere application when a dynamic SELECT statement is issued...
3
3991
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
3
2080
by: Erland Sommarskog | last post by:
I've uploaded a new version of my article on Dynamic Search Conditions on http://www.sommarskog.se/dyn-search.html. I've revised the article to cover SQL 2005, and made a general overhaul of the content. There was a *very* embarrassing error that I've corrected. I've also added a new interesting method for static SQL. I've found that if you say: SELECT ... FROM tbl
7
13450
by: skeddy | last post by:
In a nutshell, I'm trying to dynamically create a select box with ResultSet code in vbscript and then need to be able to access the value of that select box later with a Save button. I've got the select box filling with code similar to below: <SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER> Public Sub BuildComboBox(rs, dispname, val, name, selected) 'rs = the recordset 'val = fieldname to place in the val of the option
10
4947
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're called, an example of what I have in mind is index.php?page=). However, I can not figure out how to do this. I will eventually want to use SEF urls, but for now I'll be content just to have the dynamic urls. If anyone can tell me how to do this, I'd...
11
1484
by: Ronald S. Cook | last post by:
I've read a few posts on the stored procedure vs dynamic sql debate. I ran a few performance test for myself and it appears to be a wash. Given that, I'm leaning toward dynamic sql mostly because it would mean one fewer place to have things. But, before we go that route we wanted to ask the question: Is there any compelling reason why we shouldn't abandon all of our stored procs and just write the SQL inside inside our functions in...
0
10921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6142
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3360
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.