Wednesday 16 March 2011

CSMInstrument creation

Although it's obvious really, you should avoid doing this:

CSMInstrument i = new CSMInstrument(sicovam);
CSMSwap s = i;

and you should instead do this:

CSMInstrument i = CSMInstrument.GetInstance(sicovam);
CSMSwap s = i;

The CSMInstrument constructor should not be used. It should not be public; it really ought to be protected at the very least.

What the first example does is create an instance of the base class. Now, clearly, even if the sicovam is that of a derived class, the additional fields will not be populated. So use the factory. That's what it's there for! This is particularly pertinent given that one tends to pass around CSMInstrument references and utilise the implicit cast, as shown above, which will unaccountably fail.

In fact, as I think about it, CSMInstrument ought to be an abstract class. Ho hum!

Thanks for listening.

No comments:

Post a Comment