I am looking at design by contract for a Java library, this what I came up with so far in terms of the interface.
The user could call executeContract and executeContract invokes invokeContract after calling 'require'. ensure is called after executeContract to ensure the correctness of what is returned by invokeContract.
This code also works as a callback method (anonymous inner class call).
What are your thoughts? Is this design by contract?, so far this helps me write testable Java code.
public interface IContractHandler {
/**
* Execute contract will invoke the #invokeContract method. In the execute method,
* check for the validity of the preconditions and the post conditions.
*
* The precondition can be null.
*
* @param precondInput - Precondition Input Data, can be null.
* @return Post condition output
*/
public Object executeContract(final Object precondInput) throws ContractError;
/**
* Require that the preconditions are met.
*/
public Object require(final Object precondInput) throws ContractError;
/**
* Ensure that the postconditions are met.
*/
public Object ensure(final Object precondInput) throws ContractError;
/**
* The precondition can be null if the contract allows for that.
*
* @param precondInput - Precondition Input Data, can be null.
* @return Post condition output
*/
public Object invokeContract(final Object precondInput) throws ContractError;
}