OOD question
Hi,
I've the following set of classes. I would like to know if there is a better way of handling this behavior:
FYI:: This is not a complete program
Class A{
privateint maxValue;
// Constructor
A(int maxValue){
this.maxValue = maxValue
}
void doValidation (int value){
if (value < getMaxValue()){
//...do something
}
}
int getMaxValue (){
return maxValue;
}
}
Class Bextends A{
B(int maxValue){
super(maxValue);
}
int getMaxValue (){
return (C.getMode())? 256: maxValue;
}
}
Class C
{
booleanstatic getMode (){
//returns true or false based on some condition
if (someCondition){
returntrue;
}
else{
returnfalse;
}
}
}
This will work, but this does not seem like a good solution because, when other developers adds a new method in class A
say, like
void testMethod (int value){
if (value < maxValue){// instead of getMaxValue()
// ...do comething
}
}
This will fail because the maxValue is directly used and not the method getMaxValue(). The value of maxValue and getMaxValue() will be same or different based on the return value of the static method "C.getMode
()". We cannot enforce the user to use "getMaxValue()" instead of maxValue in class A. Is there a better way of handling this?
Another approach that i thought of is to update the class B instance whenever the "mode" value changes in Class C. But, that is not an optimal solution because there could be 100's of instances of class B, out of which only one instance need to updated with the new max value at any given point of time in my scenario. So, if we updated all the 100 instances, it will be performance bottle neck.
Can some one suggest an optimal and better Object Oriented way of doing this? Appreciate any help.
Thanks,
SW

