Can this statement be regarded as a class invariant?
-
14-04-2021 - |
Frage
This is a highly general thought, but let's use C# in this example.
Given that
- I have a disposable class
Foo
, i.e., it implementsIDisposable
. Foo
has a boolean flagdisposed
that is false untilDispose
is called, after which it's true.- All public methods of
Foo
throwsObjectDisposedException
ifdisposed
is true when they are called.
Does this statement
Any method of
Foo
, exceptDispose
, will throw anObjectDisposedException
when called on an instance ofFoo
that has been disposed.
describe an invariant of Foo
?
Lösung
No.
This is a set of rules common to all the methods of the class. Invariants are not rules for methods.
Design by Contract comprises defining the following parts of a contract:
- Method preconditions
- Method postconditions
- Class invariant
What you are describing are method postconditions. They belong to the contract of each function (which is of course part of the contract of the class), but not to the class invariant.
Andere Tipps
It's not an invariant. It's a statement about the state of object. IMO, what you described is a dispose method's postcondition and postcondition of all methods.