0

以下は、コントロール ブレーク パターンの再利用可能なソリューションを作成する試みです。これは、コマンド パターン (アクションおよびテスト インターフェイス) に基づいて構築されています。しかし、私の古い COBOL の考え方が邪魔になっていることに気付きました。なぜなら、このソリューションは、「グローバル変数」にアクセスできる Action オブジェクトと Test オブジェクトのそれぞれに基づいているからです。そして、その直後の私の考えは、「このような変数アクセスの必要性 (より広い範囲) は、すでに発明された車輪に違いない.

以下のすべてのアクションとテストに変数のグループへのアクセスを許可する方法 - これは再利用可能なソリューションであるはずなので、不確定なグループですか??

public class ControlBreak {

public static void controlBreak(Action initialize, 
                                Test   endOfInput, 
                                Test   onChange, 
                                Action breakAction, 
                                Action detailAction, 
                                Action getNext) {

    boolean hasProcessed = false;

    getNext.execute();
    for (initialize.execute();endOfInput.test();detailAction.execute(),getNext.execute()) {
        hasProcessed = true;
        if (onChange.test()) {
            breakAction.execute();
        }
        detailAction.execute();
    }
    if (hasProcessed) {
        breakAction.execute();
    } else {
        // throw empty input exception
    }
}
}
4

2 に答える 2

1

millmoose のおかげで、目的地にたどり着きました。参考までに、肉付けされたコードを次に示します。

    public class ControlBreak<TParams> {

    public TParams controlBreak(Action<TParams> initialize, 
                                    Test<TParams>   endOfInput, 
                                    Test<TParams>   onChange, 
                                    Action<TParams> breakAction, 
                                    Action<TParams> detailAction, 
                                    Action<TParams> getNext,
                                    TParams params) {

        boolean hasProcessed = false;

        getNext.execute(params);
        for (params = initialize.execute(params);endOfInput.test(params);params = detailAction.execute(params),params = getNext.execute(params)) {
            hasProcessed = true;
            if (onChange.test(params)) {
                breakAction.execute(params);
            }
            detailAction.execute(params);
        }
        if (hasProcessed) {
            breakAction.execute(params);
        } else {
            // throw empty input exception
        }
        return params;
    }
}
于 2013-02-08T19:13:03.150 に答える
1

On a few re-reads, it seems like you're trying to abstract a certain control flow, where the parameters can be coupled. In this case, I'd look into generics. I.e. something like this:

public static void <TParams> controlBreak(Action<TParams> initialize, ..., TParams params) {
    // ...
    initialize.execute(params)
    // ...
}

That way this method will remain reusable, but the various actions / tests can still accept a strongly-typed set of parameters/variables. (The concrete type of TParam.)

于 2013-02-08T17:11:13.917 に答える