私はJavaライブラリの契約による設計を検討しています.これは、これまでのところインターフェースの観点から思いついたものです.
ユーザーは executeContract を呼び出すことができ、executeContract は「require」を呼び出した後に invokeContract を呼び出します。enable は executeContract の後に呼び出され、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;
}