Question

I am trying to digest a large Java Application by modeling it in UML using ArgoUML. In the properties section of the UML diagramming tool there is a helpful checklist that I am assuming is used to help design good software. Most of the checklist items make sense except for 3.

1). Could you write an invariant for this class? 
2). Do all constructors establish the class invariant? 
3). Do all operations maintain the class invariant?

What is meant by these questions? I am new to object oriented design so an explanation would be helpful.

Thank you in advance.

Was it helpful?

Solution

To give an easy example: When you model a triangle, you want to make sure that the sum of the angles always adds up to 180 degrees.

class Triangle {
    number angle1, angle2, angle3;
}

This is the invariant (a condition that is supposed to be always true):

degrees (angle1 + angle2 + angle3) = degrees 180

Now the second question makes sense: since a constructor must make sure not to construct an object where the invariant doesn't hold.

The third question, then, asks if there are operations that could invalidate the invariant. For example:

    void setAngle1(number whatever) { angle1 = whatever; }

And here it turns out that the design above is not good. Why? Becasue there are redundant information. A better design would be this:

class Triangle {
    number angle1, angle2;   // two angles
    number side;             // and the connecting line
}

We can always compute angle3 from angle1 and angle2. And we just need to enforce the simpler invariant that angle1 + angle2 < 180° and side > 0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top