473,233 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

A Design Problem

Hi All,
First of all forgive me for the length of this post.

I am developing one library for some text editor. In this library
I want to provide a set of C++ classes which will enable client (of
the library) to add tool bars to the text editor. The requirements
are:

-Client can add as many tool bars as he wants to add.
-In each toolbar there will be some tools. Tools can be of button
type or list box type like in MS word a tool bar can contain either
buttons or list box (For simplicity consider these two types of tool
only).

I took following approach to design the solution of above problem.

-There will be a class say ToolBarMgr which will manage the
collection of all tool bars.
-There will be a class say ToolBar which will manage collection of
all tools of this toolbar. This class will also contain information
particular to a toolbar.

Now I am bit confused about the design of Tool class because there
will be several functionalities which will be valid for button type of
tool but does not make sense for list box type of tool and vice
versa. Say e.g button tool can have one API to change the image of
button but this API does not make any sense to have in list box tool.

There are two approaches which I can think of to design the Tool
class.

1st Approach:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-There will be a class called Tool which will contain information
common to all type of tools. Say e,g it will contain toolbar ID to
which this tool is associated.
-There will be two more classes say ButtonTool and ListBoxTool which
will inherit from Tool class and contain information specific to their
type.
-Tool class will have all the methods which can be called on at
least one type of tool and have the default implementation which will
just throw some exception.
-ButtonTool and ListBoxTool class will override their specific
methods.
-In this approach if client calls a method which is invalid for this
tool object, he will get an exception.

Main Advantages of this approach:
->Client will always deal with Tool class. So, addition/deletion of
any tool type will not affect exisiting client code.

Main Disadvantages of this approach:
-Methods which do not make sense to be defined for some classes
will still be inherited from Tool class. Say e.g API to change the
button image does not make sense to be in ListBoxTool class but still
this method will get inherit from Tool class.
-Addition/Deletion of tool type class will mean to add/remove some
APIs from Tool class also.
2nd Approach:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-There will be a class say Tool which itself will not be templatized
but will contain member templates similar to the class boost::any.
This class will contain information which will be common for all type
of tools. Also this class will contain the object of ButtonTool class
or ListBoxTool class or anything.
-Tool class will also contain one method say GetObject() which will
also be templatized and will return the object it contains. Before
returning object the GetObject() method will make sure that it is
returning correct type of object. If it can not return correct type of
object it will throw some exception. So its implementation will be
something like this

template <typename ValueType>
ValueType* Tool::GetObject()
{
If(the ValueType typeid is same as that of type id of the
object Tool class contains)
{
return address of the object
}
else
{
throw some exception
}
}

-So now if client is having object of the Tool class and if he knows
the exact type of tool; Tool class contains, he will call the
GetObject() method of the Tool class to get the exact type of tool
and then can call methods on that tool directly. But if the client
does not know the exact type of tool; Tool class contains, still he
can call the common methods (methods which are applicable to all type
of tools).

Main Advantages of this approach:
-Addition/Deletion of tool type class will not affect any class in
the library. We just need to add new class for new tool type or
delete the existing class for that tool type.
-Methods which are specific to a tool type will belong to its class
only which makes sense.

Main Disadvantages of this approach:
-Addition/Deletion of tool type class may affect client code bcoz
now client code will deal with concrete classes.
-I am using Visual Studio6 as my IDE which has lots of bugs with
the templates. So in some cases I have to use some ugly
workarounds.
It would be great if some one can please suggest me as to which
approach is better or can suggest me a new better approach.

Thanks a lot for your help and patience :)

Aug 21 '07 #1
3 1803
1st Approach:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-There will be a class called Tool which will contain information
common to all type of tools. Say e,g it will contain toolbar ID to
which this tool is associated.
-There will be two more classes say ButtonTool and ListBoxTool which
will inherit from Tool class and contain information specific to their
type.
-Tool class will have all the methods which can be called on at
least one type of tool and have the default implementation which will
just throw some exception.
-ButtonTool and ListBoxTool class will override their specific
methods.
-In this approach if client calls a method which is invalid for this
tool object, he will get an exception.

Main Advantages of this approach:
->Client will always deal with Tool class. So, addition/deletion of
any tool type will not affect exisiting client code.

Main Disadvantages of this approach:
-Methods which do not make sense to be defined for some classes
will still be inherited from Tool class. Say e.g API to change the
button image does not make sense to be in ListBoxTool class but still
this method will get inherit from Tool class.
-Addition/Deletion of tool type class will mean to add/remove some
APIs from Tool class also.
2nd Approach:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-There will be a class say Tool which itself will not be templatized
but will contain member templates similar to the class boost::any.
This class will contain information which will be common for all type
of tools. Also this class will contain the object of ButtonTool class
or ListBoxTool class or anything.
-Tool class will also contain one method say GetObject() which will
also be templatized and will return the object it contains. Before
returning object the GetObject() method will make sure that it is
returning correct type of object. If it can not return correct type of
object it will throw some exception. So its implementation will be
something like this

template <typename ValueType>
ValueType* Tool::GetObject()
{
If(the ValueType typeid is same as that of type id of the
object Tool class contains)
{
return address of the object
}
else
{
throw some exception
}
}

-So now if client is having object of the Tool class and if he knows
the exact type of tool; Tool class contains, he will call the
GetObject() method of the Tool class to get the exact type of tool
and then can call methods on that tool directly. But if the client
does not know the exact type of tool; Tool class contains, still he
can call the common methods (methods which are applicable to all type
of tools).

Main Advantages of this approach:
-Addition/Deletion of tool type class will not affect any class in
the library. We just need to add new class for new tool type or
delete the existing class for that tool type.
-Methods which are specific to a tool type will belong to its class
only which makes sense.

Main Disadvantages of this approach:
-Addition/Deletion of tool type class may affect client code bcoz
now client code will deal with concrete classes.
-I am using Visual Studio6 as my IDE which has lots of bugs with
the templates. So in some cases I have to use some ugly
workarounds.
I think I would use somewhat a combination of those two approaches:

Have a Tool class which only contains the common API and no methods
specific to a tool type; to access specific functionality, the client
could access the tool via Tool* and dynamic_cast it to the expected type.

Or, you could do a method like:

template<typename T>
T* getTool(int id)
{
Tool* tool(getToolById(id));
T* result(dynamic_cast<T*>(tool));
if(!result)
throw some exception;
return result;
}

Which seems to be similiar to approach 2 as I understood; this way, no
client code should be dependent on tool types it does not use but the
generic Tool class does not have APIs that do not make sense.

However, I'm not quite sure about that RTTI stuff--maybe this is
considered poor style/design or is even worse supported than templates are?

Yours,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Aug 21 '07 #2
On Aug 21, 8:52 am, Mousam <mousam.du...@gmail.comwrote:
[snip]
It would be great if some one can please suggest me as to which
approach is better or can suggest me a new better approach.
Not going to do your design work for you. You need to get some
good texts on windows design and handling user input events.

Go here www.amazon.com and search for Petzold. Then go
here www.accu.org and look at some book reviews. (Though they
were not available just now. Hopefully that is short term.)
Socks

Aug 22 '07 #3
On Aug 22, 8:32 pm, Puppet_Sock <puppet_s...@hotmail.comwrote:
On Aug 21, 8:52 am, Mousam <mousam.du...@gmail.comwrote:
[snip]
It would be great if some one can please suggest me as to which
approach is better or can suggest me a new better approach.

Not going to do your design work for you. You need to get some
good texts on windows design and handling user input events.

Go herewww.amazon.comand search for Petzold. Then go
herewww.accu.organd look at some book reviews. (Though they
were not available just now. Hopefully that is short term.)
Socks
Hi,
Its not like I posted one problem and asked you guys to design it
for me. If you have read whole post I described two approaches and
asked for suggestion.

Thanks & Regards,
Mousam Dubey

Aug 23 '07 #4

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

Similar topics

3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
1
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
5
by: istillshine | last post by:
Particularly for medium-sized (10,000 ~ 20,000 lines) programs, what are useful strategies to design them before coding? My strategies are: 1. Imagine what the final program would look like....
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.