我正在研究 Java 库的合同设计,这是我迄今为止在接口方面提出的。
用户可以调用executeContract,并且executeContract 在调用'require' 后调用invokeContract。在executeContract 之后调用ensure 以确保invokeContract 返回的内容的正确性。
此代码也可用作回调方法(匿名内部类调用)。
你怎么认为?这是按合同设计的吗?到目前为止,这有助于我编写可测试的 Java 代码。
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;
}