LoginSignup
0
0

More than 3 years have passed since last update.

javascriptのStrategyパターン

Posted at

Strategy パターン

Strategyパターンは、必要に応じて計算法を切り替えることが容易になり、アルゴリズムを実行時に選択することができるデザインパターンです。
オブジェクト指向における再利用のためのデザインパターン。

クラス図

figure6.png

/*Strategy*/
var levelOBJ = {
    "A": function(money) {
        return money * 4;
    },
    "B" : function(money) {
        return money * 3;
    },
    "C" : function(money) {
        return money * 2;
    } 
};
/*Context*/
var calculateBouns =function(level,money) {
    return levelOBJ[level](money);
};
/*Client*/
console.log(calculateBouns('A',10000)); // 40000

ユースケース

主に切り替えや追加・拡張を容易にする処理
例えば、ユーザーの選択に合わせて難易度の切り替えや国によって為替レートの変換

参考

1.zeng tan.2015. Design Pattern And Development Practice Of JavaScript

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0