Join Bytes! wrote:[color=blue]
> Hello All
>
> We have a system where customer request (of text form) are processed.
>
> The number of different request types is more than a thousand.
>
> With current ( C-langauage ) design request a parsed, type of the
> request is determined (and encoded as unsigned int)
> and request data is packed in request specific structure.
>
> Then processing function for this type of the request is looked up
> in a table by binary search and this function is
> called and request specific data is passed to it(as void*).
>
>
> The system is now being rewritten in C++. Is it practical to
> derive each request from a common base class and have virtual function
> run()
> instead of current 'processing' function?[/color]
Yes, it probably is, and indeed this is a common use of inheritance and
virtual functions. You should look into the Command design pattern
(_Design Patterns_ by Gamma et al.) and object factories (e.g., the one
in chapter 8 of _Modern C++ Design_ by Alexandrescu).
[color=blue]
> Is virtual table lookup of such size as fast or faster then binary
> search?[/color]
You can't do a "virtual table lookup" as such. However, you could have
a (pure) virtual function (run, in your case) in a base class and then
any number of derived classes that implement it for specific types of
requests. What you need then is a way to create an instance of the
particular derived class that corresponds to a particular request. An
object factory will do that for you, but the factory will basically be
a fancy front end to a balanced binary tree lookup with search
complexity O( log N ) if implemented optimally.
[color=blue]
> Is design with so many derived classes good in this situation?[/color]
It can be.
[color=blue]
>
> Best regards, Konstantin Kivi
>
> PS.
> It comes to my mind, that Java programmers haven't first
> option (as there is no pointer to function). So they
> can use only second option.[/color]
I think you must misunderstand what virtual functions are. Java
certainly has the ability to override (pure) virtual functions. All the
pointers to functions and such are done behind the scenes in C++ and in
Java.
Cheers! --M