Hi,
I'm
trying to get my head around using policy based design in C++ using templates;
it seems that quantlib uses this approach a lot. My basic question
involves trying to figure out under what circumstances templates should be
utilized in certain design features (with regard to policy based design -
not so much with regards to generic container classes etc.) As an example,
imagine I was going to create a Bond class; I might do it as follows:
class Bond : public Security
{
protected:
PrinicipalSchedule
principalSchedule;
CouponSchedule
couponSchedule;
TerminationProvision
terminationProvision;
DefaultGuarantee
defaultProvision;
public:
//various
useful methods go here
}
The basic idea is to abstract away certain things to provide
enough flexibility to deal with issues like callabillity, amortizations, bizarre
payment dates, whatever. Of course classes like TerminationProvision would
have to be carefully crafted in order to handle the wide variety in which bonds
can cease to exist. This class would be used by various pricing engine
objects to perform calculations. But in any case I would think that my approach
is more or less what someone with a Java background would attempt - with the
understanding that occasionally some situations might require using 'instanceof'.
My question then is: in c++ is the above approach optimal or
is there some more powerful paradigm that would utilize templated classes?
Or more relevant to quantlib:
Can someone elaborate on why
SwaptionPricer is templated:
template<class ModelType>
class SwaptionPricer : public PricingEngine,
public Patterns::Observer,
public Patterns::Observable
as opposed to something like
class SwaptionPricer :
public PricingEngine,
public Patterns::Observer,
public Patterns::Observable {
Handle<SwaptionModel>
model_;
}
class SwaptionModel {
SwaptionResults
calcSwaption(SwaptionParameters parameters);
}
Thanks,
Nehal