473,765 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this scenario a good use for polymorphic functions

I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.

The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.

Does this seem like a reasonable design in this scenario?

Sep 18 '07
11 1484
Angus <an*********@gm ail.comwrote:
I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.

The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.

Does this seem like a reasonable design in this scenario?
I will give you a heuristic... If making a base class and two or more
subclasses will allow you to remove one or more switch or if statements,
then go ahead and use polymorphism, otherwise don't.
Sep 18 '07 #11
On Sep 18, 5:11 pm, Angus <anguscom...@gm ail.comwrote:
I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.
The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.
Does this seem like a reasonable design in this scenario?
Not per se. If the base class has to work out the message type,
and call a different function, then there's no point in
derivation.

If it's possible to determine the action from some information
at the beginning of the buffer, then it would be reasonable to
use that information to create a derived class of the correct
type; in this case, the base class doesn't need to work out
anything, it just calls its virtual "doAction" function. You
still need to work out which type of object to create, but this
can typically be done by looking up a factory in a table or map,
and calling the factory function for the actual construction.

In other cases, it might make more sense to have a class with
the message itself, and use the strategy pattern for the action;
the base class then determines the correct strategy to
instantiate (again, by means of table lookup and a factory).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 19 '07 #12

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

Similar topics

10
10910
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
5
8085
by: Dave Theese | last post by:
Please consider this code: class base {}; class derived: public base {}; base *ptr = new derived; cout << typeid(*base).name << endl; In this case, I see output of "class base" rather than "class derived". This is somewhat expected I suppose since my base class does not have any
9
1492
by: Dave | last post by:
What is the expected output of this program and why??? #include <iostream> using namespace std; class base {
2
3895
by: Aryeh M. Friedman | last post by:
If I have something like this: class NumberException { }; class Number { public: ... virtual unsigned long getValue() {throw(NumberException);}; ...
9
2130
by: Karel Miklav | last post by:
In lots of places in a programm I need to identify type of received messages, so I create them as virtual classes and use RTTI to find their type later. But these are simple messages, often without content, and I hate how I make their base class: by adding a dummy virtual function. Is there another way? -- Regards,
5
3422
by: Ben Pope | last post by:
Hi all, This is not something I've played around with much, but I'm designing some factories and I want a function like this: template<class T> T* Creator() { return new T; }
3
3912
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base* foo; which a complicated code allocates as one of derived's and sets up.
7
1621
by: Arindam | last post by:
#include <cstdio> struct Test { void bar() { foo(); } private: virtual void foo() { printf("Test\n"); }
23
3148
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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
9832
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
7371
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
6646
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
5272
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
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
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
3
2800
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.