1

私は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;

}
4

3 に答える 3

0

実は、この状況で探しているのはTemplate Methodと呼ばれるパターンです。インターフェイスは実装を決定しません。これは、 executeContract() メソッドで実行しようとしていることです。

于 2009-03-17T16:34:37.693 に答える