domain driven design - How can class invariant strengthen pre and post-conditions? -
you can think of class invariant health criterion, must fulfilled objects in between operations. precondition of every public operation of class, can therefore assumed class invariant holds. in addition, can assumed postcondition of every public operation class invariant holds. in sense, class invariant serves general strengthening of both precondition , postcondition of public operations in class. effective precondition formulated precondition in conjunction class invariant. similarly, effective postcondition formulated postcondition in conjunction class invariant.
public class server { // other code ommited public output foo(input cmdin) { ... return cmdout; } } public class caller { // other code ommited /* calls server.foo */ public void call() {...} } public class input { // other code ommited public int length {...} } public class output { // other code ommited public int length {...} }
1. if class invariant defined on server
class:
a) preconditions typically formulated in terms of formal parameters of called operation, how can class invariant strengthen method's ( foo
's ) preconditions?
b) postcondition formulated in terms of called method's return value, how can class invariant strengthen method's ( foo
's ) postconditions?
2. can class invariant defined on caller
class in way strengthen foo
's preconditions or postconditions?
3. if class invariant defined on foo
's cmdin
parameter:
a) if precondition on foo
states cmdin.length
within range 1-20
, 1 of class invariants defined on input
states input.length
should within range 2-19
, foo
's precondition indeed strenghten?
b) isn't logic in a) bit flawed, since if class invariant states input.length
should within range 2-19
, isn't error foo
define precondition isn't true
( cmdin.length
can't hold values 1
or 20
)
c) if class invariant defined on input
states input.length
should within range 0-100
, foo
's precondition isn't strengthen?
d) can class invariants defined on cmdin
somehow strengthen foo
's postcondition?
4. if class invariant defined on foo
's return value
a) if postcondition on foo
states cmdout.length
within range 1-20
, 1 of class invariants defined on output
states output.length
should within range 2-19
, foo
's postcondition indeed strengthen?
b) if invariant defined on output
states output.length
should within range 0-100
, foo
's postcondition wasn't strengthen?
c) can class invariants defined on output
somehow strengthen foo
's precondition?
5. impression quoted article meant having class invariant ( , if invariant doesn't in way operate ( directly or indirectly ) on foo
's parameters and/or return value, still strengthen foo
's preconditions , postcondition? if that's article implying, how possible?
thanks
a) preconditions typically formulated in terms of formal parameters of called operation, how can class invariant strengthen method's ( foo's ) preconditions?
i suspect that's key misunderstanding. pre-conditions may include formal parameters - not restricted them. can - , - refer class features (attributes/operations). in general, combination of invariants , pre-condition defines set of constraints must satisfied before operation obliged meet post-condition when called. similarly, operation must guarantee both post condition , invariants satisfied when completes. take example of bounded buffer:
class boundedbuffer<t> { public int max // max #items buffer can hold public int count // how many items in buffer void push(t item) {...} t pop() {...} }
a pre-condition push()
buffer has not reached maximum size:
pre: count < max
so here pre-condition doesn't mention operation's formal parameter. can state invariant buffer:
inv: count >=0 //can't have -ve number of elements in buffer
it strengthens pre-condition because entends must true before push()
operation obliged meet post condition. 2 clauses logically anded together. effective pre-condition count >=0 , count < max
. that's stronger (more restrictive) constraint pre-condition alone.
note concept isn't restricted situations pre-condition refers class features. let's add constraint size of individual item being added buffer must less upper limit:
pre: count < max , item.size() <= max_item_size
adding invariant still strengthens effective pre-condition become:
pre: count < max , item.size() <= max_item_size , count >=0
so in summary: invariants must hold before operation invoked , after operation completes. hence strengthen both.
- can class invariant defined on caller class in way strengthen foo's preconditions or postconditions?
no. invariants apply class defined on only.
answers remaining questions flow logically above.
hth.
Comments
Post a Comment