Class Design Question
-
I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com
-
I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com
first a question: do you need two implementations - are you having two because you are worrying about performance? why not just have a DoubmePoint class - see if you have a problem then. if that does not work then: create a class called SanderNumber which can use either a integer or a double. then create your abstract class SanderNumberUtil that uses SanderNumber. then create a concrete class for integer and double. This puts most of the complexity into SanderNumber - but allows you to use it polymorphically. hope this helps.
"When the only tool you have is a hammer, a sore thumb you will have."
-
first a question: do you need two implementations - are you having two because you are worrying about performance? why not just have a DoubmePoint class - see if you have a problem then. if that does not work then: create a class called SanderNumber which can use either a integer or a double. then create your abstract class SanderNumberUtil that uses SanderNumber. then create a concrete class for integer and double. This puts most of the complexity into SanderNumber - but allows you to use it polymorphically. hope this helps.
"When the only tool you have is a hammer, a sore thumb you will have."
why not just have a DoubmePoint class I am not sure what you mean by "DoubmePoint class". if that does not work then I somewhat understand the theory of what your second solution is but I also can't really invision it very well. Could you give a little more detail? Mark Sanders sanderssolutions.com
-
why not just have a DoubmePoint class I am not sure what you mean by "DoubmePoint class". if that does not work then I somewhat understand the theory of what your second solution is but I also can't really invision it very well. Could you give a little more detail? Mark Sanders sanderssolutions.com
type - i meant why not just have the DoublePoint class - do you really need the integer class? otherwise class SanderNumber { bool _isInteger = true; int _valueInt; double _valueDouble; public SanderNumber(int value) { _valueInt = value; } public SanderNumber(double value) { _valueDouble = value; _isInteger = false; } public int SomeFunction() { // if (!_isInteger) throw(some exception) return _valueInt; } public double SomeFunction() { // if (_isInteger) throw(some exception) return _valueDouble; } } class SanderUtil() { SanderNumber _number; public SanderNumber(SanderNumber value) { _number = value; } public SomeCleverStuff() { if (_number.SomeFunction()) > 24.0) return; } } this would give you what you want and would be type safe.....
"When the only tool you have is a hammer, a sore thumb you will have."
-
I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com
You could implement an Interface for your methods which the two classes will inherit and have to implement. That is if the implementation of the methods are different.
-
You could implement an Interface for your methods which the two classes will inherit and have to implement. That is if the implementation of the methods are different.
The implementations are identical. Mark Sanders sanderssolutions.com
-
I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com
you could make the integerPoint class, then derive the doublepoint class from that, and override the variables/properties inherited....just a thought.
| Website: http://www.onyeyiri.co.uk | Sonork: 100.21142 : TheEclypse | "If a dolar was a chicken would the chicken be evil?"