サーチ…


前書き

Bashで一般的なデザインパターンを達成する

パブリッシュ/サブスクライブ(パブ/サブ)パターン

Bashプロジェクトがライブラリに変わると、新しい機能を追加するのが難しくなる可能性があります。関数名、変数、およびパラメータは、通常、それらを利用するスクリプトで変更する必要があります。このようなシナリオでは、コードをデカップリングし、イベント駆動型の設計パターンを使用すると便利です。前記パターンでは、外部スクリプトがイベントにサブスクライブすることができる。そのイベントがトリガされる(公開される)と、スクリプトはイベントで登録したコードを実行できます。

pubsub.sh:

    #!/usr/bin/env bash

    #
    # Save the path to this script's directory in a global env variable
    #
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

    #
    # Array that will contain all registered events
    #
    EVENTS=()

    function action1() {
        echo "Action #1 was performed ${2}"
    }

    function action2() {
        echo "Action #2 was performed"
    }

    #
    # @desc   :: Registers an event
    # @param  :: string $1 - The name of the event. Basically an alias for a function name
    # @param  :: string $2 - The name of the function to be called
    # @param  :: string $3 - Full path to script that includes the function being called
    #
    function subscribe() {
        EVENTS+=("${1};${2};${3}")
    }

    #
    # @desc   :: Public an event
    # @param  :: string $1 - The name of the event being published
    #
    function publish() {
        for event in ${EVENTS[@]}; do
            local IFS=";"
            read -r -a event <<< "$event"
            if [[  "${event[0]}" ==  "${1}" ]]; then
                ${event[1]} "$@"
            fi
        done
    }

    #
    # Register our events and the functions that handle them
    #
    subscribe "/do/work"           "action1" "${DIR}"
    subscribe "/do/more/work"      "action2" "${DIR}"
    subscribe "/do/even/more/work" "action1" "${DIR}"

    #
    # Execute our events
    #
    publish "/do/work"
    publish "/do/more/work"
    publish "/do/even/more/work" "again"

実行:

chmod +x pubsub.sh
./pubsub.sh


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow