1. 序章

Apache Commons Chain は、Chain of Responsibility pattern を使用するライブラリです。通常、複数の受信者がリクエストを処理できる複雑な処理フローを整理するために使用されます。

この簡単な記事では、ATMからの引き出しを表す例を紹介します。

2. Mavenの依存関係

まず、Mavenを使用してこのライブラリの最新バージョンをインポートします。

<dependency>
    <groupId>commons-chain</groupId>
    <artifactId>commons-chain</artifactId>
    <version>1.2</version>
</dependency>

このライブラリの最新バージョンを確認するには–ここに移動

3. チェーンの例

ATMは数値を入力として受け取り、それをさまざまなアクションの実行を担当するハンドラーに渡します。 それらは、分配される紙幣の数を計算し、取引について銀行と顧客に通知を送ることを含みます。

4. チェーンコンテキスト

コンテキストは、アプリケーションの現在の状態を表し、トランザクションに関する情報を格納します。

ATMの引き出しリクエストの場合、必要な情報は次のとおりです。

  • 引き出される合計金額
  • 100種類の紙幣の数
  • 50種類の紙幣の数
  • 10種類の紙幣の数
  • 引き出される残りの金額

この状態は、クラスで定義されています。

public class AtmRequestContext extends ContextBase {
    int totalAmountToBeWithdrawn;
    int noOfHundredsDispensed;
    int noOfFiftiesDispensed;
    int noOfTensDispensed;
    int amountLeftToBeWithdrawn;

    // standard setters & getters
}

5. 指示

コマンドは、 C ontext を入力として受け取り、それを処理します。

上記の各手順をコマンド:として実装します。

public class HundredDenominationDispenser implements Command {

    @Override
    public boolean execute(Context context) throws Exception {
        intamountLeftToBeWithdrawn = (int) context.get("amountLeftToBeWithdrawn);
        if (amountLeftToBeWithdrawn >= 100) {
            context.put("noOfHundredsDispensed", amountLeftToBeWithdrawn / 100);
            context.put("amountLeftToBeWithdrawn", amountLeftToBeWithdrawn % 100);
        }
        return false;
    }
}

The 指示 s for FiftyDenominationDispenser TenDenominationDispenser 似ています。

6. 鎖

チェーンは、指定された順序で実行されるコマンドのコレクションです。 チェーンは、上記のコマンドと、最後にAuditFilterで構成されます。

public class AtmWithdrawalChain extends ChainBase {

    public AtmWithdrawalChain() {
        super();
        addCommand(new HundredDenominationDispenser());
        addCommand(new FiftyDenominationDispenser());
        addCommand(new TenDenominationDispenser());
        addCommand(new AuditFilter());
    }
}

チェーンコマンドがtrueを返すと、チェーンは強制的に終了します。

7. フィルター

フィルターも指示しかし、 postProcess の実行後に呼び出されるメソッド鎖。

私たちのフィルター顧客と銀行に通知を送信します:

public class AuditFilter implements Filter {

    @Override
    public boolean postprocess(Context context, Exception exception) {
        // send notification to bank and user
        return false;
    }

    @Override
    public boolean execute(Context context) throws Exception {
        return false;
    }
}

8. チェーンカタログ

これは、ChainsおよびCommandsとそれらの論理名のコレクションです。

この場合、カタログにはAtmWithdrawalChainが含まれます。

public class AtmCatalog extends CatalogBase {

    public AtmCatalog() {
        super();
        addCommand("atmWithdrawalChain", new AtmWithdrawalChain());
    }
}

9. チェーンの使用

上記のチェーンを使用して引き出しリクエストを処理する方法を見てみましょう。 最初に作成します環境そしてそれを渡します鎖。 Theを処理します環境。

AtmWithdrawalChain:を示すテストケースを作成します

public class AtmChainTest {

    @Test
    public void givenInputsToContext_whenAppliedChain_thenExpectedContext() throws Exception {
        Context context = new AtmRequestContext();
        context.put("totalAmountToBeWithdrawn", 460);
        context.put("amountLeftToBeWithdrawn", 460);
        
        Catalog catalog = new AtmCatalog();
        Command atmWithdrawalChain = catalog.getCommand("atmWithdrawalChain");
        
        atmWithdrawalChain.execute(context);
        
        assertEquals(460, (int) context.get("totalAmountToBeWithdrawn"));
        assertEquals(0, (int) context.get("amountLeftToBeWithdrawn"));
        assertEquals(4, (int) context.get("noOfHundredsDispensed"));
        assertEquals(1, (int) context.get("noOfFiftiesDispensed"));
        assertEquals(1, (int) context.get("noOfTensDispensed"));
    }
}

10. 結論

このチュートリアルでは、ApacheのApacheCommonsChainライブラリを使用した実際のシナリオを検討しました。ここで詳細を読むことができます。

そして、いつものように、この記事のコードはGithubから入手できます。