I have to update a road tunnel lighting program I wrote for a company. They now want a new standard to be applied, but with the option of switching between standards real time. And there will be other standards in the future.

Currently the single object which calculates the lighting curves along the length of the tunnel is a

CStd2003 m_Std ;

object in the application.

My immediate idea was that I could have a CStd base class and then a pointer rather than an object in the application:

CStd* m_pStd ; // Will be either CStd2003 or CStd2011

Then I looked in the docs to find out how much I'd actually have to change in the calculations. Less than 5%. And yea I felt as if I was in the wilderness being tempted by SATAN HIMSELF. And YEA Satan SAID:

"Listen sunshine, just use an enumerator inside CStd m_eStd which
can take ekStd2003 or ekStd20011 values. Then modify the calculations
according to the value of m_eStd."

And YEA I was sore tempted.

Should I give in to temptation.............?

I would go the polymorphism route. That way if you need to add another standard latter on all you have to do is create a new derived class and add the option for the new standard.

I vote with Nathan. Create a base class with the behaviors (as virtual functions) you need, and then for each subsequent standard, all you need to do is implement the deltas with a new derived class. Using a reference or pointer to the base class, it will do what you need since the functions will be called on the implementing class (Std2003, Std2011, etc). So, the basic functionality won't need to change, and you only need to instantiate a member of the appropriate standards-based class.

Thanks, I think you are both right. I'll do it the right way and not fall into sin and temptation!

This article has been dead for over six months. Start a new discussion instead.