SE450: Encapsulation: Invariants, Preconditions, and Postconditions [18/47] Previous pageContentsNext page

Definition: the interfaces of classes are viewed as contracts that specify benefits and obligations of both the client and the supplier (the class itself). (Bertrand Meyer)

We can enhance interfaces by using preconditions/postconditions and invariants:

  1. Precondition - must be true to make the call. Indicates the bug is in the caller.
  2. Postcondition - must be true after the call. Indicates the bug is in the provider (or in the precondition assertion!)
  3. Invariant - must always be true (after a successful instantiation of the class, or even true for static methods if there is no object of the class created). It will be true before and after each method invocation. Indicates the bug could be in either the caller or the provider.

You can define these conditions as constraints in the UML. Just put the text describing the constraint in { }. There is also a more formal Object Constraint Language available to define constraints.

Previous pageContentsNext page