Exercise 11.1

This forum is to discuss the book "C++ design patterns and derivatives pricing."

Exercise 11.1

Postby pzling » Tue Mar 13, 2012 7:31 am

Hi

I'm having problems implementing the Factory Pattern with the Monostate Pattern. As I understand, the Monostate Pattern is like the Singleton Pattern except the behaviour is controlled via ensuring static data members rather than static functions. As such, all data members need to be static and private while all class methods that access them have to be non-static and public.

The part which has me stumped is how to deal with the map datatype. Labelling the map variable static results in compilation errors (LNK2001), which I'm guessing is due to it being a variable container. I think this is cause of why the payoffs are not being registered, as each PayOffFactory object created is updating it's own map TheCreatorFunctions variable.

What is the recommended way to work around this?

Many thanks
pzling
 
Posts: 41
Joined: Sat Jan 14, 2012 8:52 pm

Re: Exercise 11.1

Postby pzling » Tue Mar 13, 2012 9:57 pm

Sorry, actually I've found my answer.

After declaring all data members static, the variables still have to be created. Since the variables's role is to act as a common variable, they need to be created with global scope.
pzling
 
Posts: 41
Joined: Sat Jan 14, 2012 8:52 pm

Re: Exercise 11.1

Postby pzling » Tue Mar 13, 2012 11:33 pm

Having read a bit more on Singleton vs Monostate patterns, there seems to be a sentiment in the wider programming community that Monostate is superior to Singleton at least because:
a) it allows more flexibility since you can add particular features to each object created, or
b) if the Singleton class is no longer desired, you don't have to worry about all the other classes that communicate with the Singleton class.

Generally speaking for mathematical finance, is there such a preference or either is generally as good as the other?
pzling
 
Posts: 41
Joined: Sat Jan 14, 2012 8:52 pm

Re: Exercise 11.1

Postby mj » Wed Mar 14, 2012 1:05 am

i don't really know. I mentioned Monostate in the book as i thought it was cool but I've never actually used it myself.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am


Return to C++ design patterns and derivatives pricing

Who is online

Users browsing this forum: No registered users and 1 guest