OONumerics User :

From: David Bruhwiler (bruhwile_at_[hidden])
Date: 2007-09-21 12:56:33


Hi Georg,

You don't say what language you're using, but your problem can be solved
in C++ with a "policy class" which uses template techniques:
   http://www.boost.org/more/generic_programming.html#policy

Each algorithm has to be implemented in a class that contains no data
(or only static data). Although there's no base class or derived
classes, each of these "policy" classes must obey the same interface.

You then have one class that holds the data and exercises the various
algorithms. This class must be templated over the policy (i.e.
algorithm) that is to be invoked.

There's no overhead due to virtual function calls, because all is known
at compile time, and the methods are invoked with syntax such as
   TEMPLATE_PARAMETER::execute_method_a(...);

The run-time polymorphism requires additional work. For example, you
can instantiate your templated class with all relevant algorithms at
compile time, then use a standard factory pattern to create a map that
takes a string (or integer) and serves up a pointer to the desired instance.

This sort of thing is done routinely in the parallel C++ framework VORPAL:
   http://www.txcorp.com/products/VORPAL/

Cheers,
David

-- 
David Bruhwiler  --  bruhwile_at_[hidden]  --  303.448.0732
Tech-X Corp., 5621 Arapahoe Ave, Suite A, Boulder CO 80303
       http://www.txcorp.com  --  fax:  303.448.7756
Georg Holzmann wrote:
> Hallo!
> 
> I have the following problem: In my class (= an algorithm) I have data 
> and some algorithms and it should be possible to exchange the algorithms 
> - perfect would be at runtime.
> 
> So usually one would maybe use the strategy design pattern for such a 
> problem.
> However, I want to avoid virtual functions (performace critical methods) 
> and so it is unfortunately not possible with strategy (?) - yes, one can 
> implement strategy with early binding (with mixins or e.g. the Curiously 
> Recurring Template Pattern), but I want late binding if possible ...
> 
> Has anyone an idea which other approach I could try ?
> I also thought about function pointers (or FastDelegates as e.g. 
> described here: http://www.codeproject.com/cpp/FastDelegate.asp), but 
> then its not such a nice OO design ...
> 
> Thanks for any hint,
> LG
> Georg
> _______________________________________________
> oon-list mailing list
> oon-list_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/oon-list