設計模式 策略模式

2021-07-10 03:49:12 字數 3162 閱讀 7115

策略模式的定義是:定義一系列的演算法,把它們乙個個封裝起來,並且使它們可以相互替換,策略模式的目的就是將演算法的使用與演算法的實現分離開來。

首先我們來看乙個情景:很多公司的年終獎是根據員工的績效來進行發放,績效為s的人,年終獎有4倍工資,績效為a的人有3倍工資,績效為b的人有2倍工資。我們怎麼用**來實現這個問題呢?

很簡單粗暴的會想到:

var calculatebonus = function

(performaccelevel, salary)

if (performaccelevel === "a")

if (performaccelevel === "b")

};

這段的**存在什麼問題呢?

過多的if-else,函式缺乏彈性,如果我們現在又增加一種績效c,那麼我們該怎麼辦,我們只有手動去改我們的**,這也就違反了我們所說的開閉原則。

下面我們利用策略模式來對這段**進行重構:

乙個基於策略模式的程式至少由兩部分組成。第乙個部分是一組策略類,策略類封裝了具體的演算法,並負責具體的計算過程。第二個是環境類context,context接受客戶的請求後,隨後把請求委託給某乙個策略類。再說明白點就是,context把客戶發起的請求,委託給了乙個策略類new出來的乙個策略物件。

// 定義三種策略類

var performances = function

() {};

performances.prototype.calculate = function

(salary) ;

var performancea = function

() {};

performancea.prototype.calculate = function

(salary) ;

var performanceb = function

() {};

performanceb.prototype.calculate = function

(salary) ;

// 環境類

var bonus = function

() ;

bonus.prototype.setsalary = function

(salary) ;

bonus.prototype.setstrategy = function

(stratrgy) ;

bonus.prototype.getbonus = function

() ;

var bonus = new bonus();

bonus.setsalary(20000);

bonus.setstrategy(new performances());

console.log(bonus.getbonus()); // 80000

bonus.setstrategy(new performancea());

console.log(bonus.getbonus()); // 60000

這種方式是仿造傳統物件導向語言實現的,利用js的特性,函式就是物件,所以我們可以將策略類定義為乙個物件,performances,performancea,performanceb設定為該物件的方法,就有如下**:

var strategies = ,

a: function

(salary) ,

b: function

(salary)

};var calculatebonus = function

(level, salary) ;

console.log(calculatebonus("s", 20000)); // 80000

console.log(calculatebonus("b", 20000)); // 40000

下面來看策略模式的兩個具體應用:

編寫乙個動畫運動,其中tween為策略類物件,其中的每一種方法,代表其每一種策略

// 策略類

var tween = ,

easein: function

(usetime, originpos, targetpos, alltime) ,

strongeasein: function

(usetime, originpos, targetpos, alltime) ,

strongeaseout: function

(usetime, originpos, targetpos, alltime) ,

sineasein: function

(usetime, originpos, targetpos, alltime) ,

};// 運動類,easing可以採取不同的策略

var animate = function

(dom) ;

animate.prototype.start = function

(propertyname, targetpos, duration, easing)

}, 20);

};animate.prototype.step = function

() pos = this.easing(time - this.starttime, this.startpos, this.endpos - this.startpos, this.duration);

this.update(pos);

};animate.prototype.update = function

(pos) ;

// 測試**

var div = document.getelementbyid("div"),

animate = new animate(div);

animate.start("left", 500, 1000, "easein");

// html**

"div" style="position: absolute;height:100px;width:100px;background:red;">div>

設計模式 策略設計模式

策略設計模式其實就是多型的使用,父類引用指向子類物件。策略模式的最大特點是使得演算法可以在不影響客戶端的情況下發生變化,從而改變不同的功能。策略模式的缺點其實也很明顯,在於策略模式把每一種具體的策略都封裝成乙個實現類,如果策略有很多的話,很顯然是實現類就會導致過多,顯得臃腫。案列 author de...

設計模式 策略模式

策略模式是一種定義一系列演算法的方法,從概念上來看,所有這些方法完成的都是相同的工作,只是實現不同,他們可以用相同的方式呼叫所有的演算法,減少了演算法類和使用演算法類之間的耦合.優點 策略模式的strategy類層次為context定義了一系列可供重用的演算法和行為,繼承有助於吸取這些演算法中的公共...

設計模式 策略模式

定義了演算法家族,分別封裝起來,讓他們之間可以相互替代,此模式讓演算法的變化,不會影響到使用演算法的客戶端 定義抽象類 分別實現抽象類,不同的物件 然後用乙個類初始化,並傳入具體的策略物件 根據具體的策略物件,呼叫其演算法的方法 客戶端 是由於例項化不同的策略,所以最終在呼叫 類時,所獲得的結果時不...