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

[3704 byte] By [s_narraa] at [2007-10-2 3:33:54]
# 1

The solution to this is simple: according to encapsulation in OO, only class A should expose the getMaxValue() method. Make that method final, and subclasses cannot tamper with the value of maxValue other than to call setMaxValue(), in which case A's internal state is always consistent. If you find you need additional behavior in class C, you can always create a new method getMaxValueAlternate() (you get the idea).

This is one reason why declaring protected variables in a super-class is dangerous. A sub-class can modify them without 'informing' the super-class. You should, generally, make all variables that are not constants private and then provide protected (or public or package-private) mutators/accessors to alter that private variable.

- Saish

Saisha at 2007-7-15 22:46:26 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
> Can some one suggest an optimal and better Object> Oriented way of doing this? Appreciate any help.> the ultimal help or solution is this:go to the new java forum and stay there for a while before posting anything here.
Saisha at 2007-7-15 22:46:26 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> > Can some one suggest an optimal and better Object

> > Oriented way of doing this? Appreciate any help.

> >

>

> the ultimal help or solution is this:

>

> go to the new java forum and stay there for a while

> before posting anything here.

That's a bit harsh. "OO Design" is in the title and does represent a concept the OP is asking about.

- Saish

Saisha at 2007-7-15 22:46:26 > top of Java-index,Other Topics,Patterns & OO Design...