Re: Abstract Factory/Abstract(Virtual) Constructor Pattern
Posted by
Plamen Neykov on
Jun 01, 2005; 9:08am
URL: http://quantlib.414.s1.nabble.com/Abstract-Factory-Abstract-Virtual-Constructor-Pattern-tp10814p10816.html
Hi Luigi,
> 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:
I was thinking more about using the following pattern (or at least some parts
of it, like the class registration mechanism):
http://www.cs.wustl.edu/~schmidt/europlop-96/papers/paper26.psIt could be that a more generic mechanism would also be usefull for other
classes as for example the family of the DayCounter derived classes.
>
> 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.
Sure - as you said, if this stuff is out of the Library's scope, I'll take the
approach you suggested.
Thanks,
Plamen