Login  Register

Re: [Quantlib-dev] Abstract Factory/Abstract(Virtual) Constructor Pattern

Posted by Luigi Ballabio on Jun 01, 2005; 7:47am
URL: http://quantlib.414.s1.nabble.com/Abstract-Factory-Abstract-Virtual-Constructor-Pattern-tp3817p3818.html

On 06/01/2005 12:02:33 AM, Plamen Neykov wrote:
>
> I need to construct a Currency object from a given ISO three letter  
> code.
>
> What is the "QuantLib" way to this?

Hi,
        currently there's no "QuantLib way" to do it---I'm not sure  
that class instantiation based on textual input falls into the scope of  
a library. However, as Currency instances are stateless, you might use  
a prototype pattern; the simplest way would be something like:

std::map<std::string, Currency> knownCurrencies;

void initializeCurrencyMap() {
     knownCurrencies["EUR"] = EURCurrency();
     knownCurrencies["GBP"] = GBPCurrency();
     knownCurrencies["USD"] = USDCurrency();
     ...
}

you'll have to call the initialization procedure explicitly, after  
which you can write:

void foo(const std::string& iso, ...) {
     ...
     Currency c = knownCurrencies[iso];
     ...
}

A possible development would be to encapsulate the map in a singleton;  
this would give you the possibility of calling the initialization  
procedure automatically when the singleton is first accessed.

HTH,
        Luigi


----------------------------------------

Newton's Law of Gravitation:
        What goes up must come down.  But don't expect it to come down
        where you can find it.  Murphy's Law applies to Newton's.