ÀÌÀü¿¡ »óÅ ÆÐÅÏ¿¡ ´ëÇؼ °øºÎ Çß¾ú´Ù. À̹øÀå¿¡¼´Â Àü·« ÆÐÅÏ¿¡ ´ëÇؼ ¾Ë¾Æ º»´Ù. Àü·« ÆÐÅÏÀº ½ÇÇà Áß¿¡ ÇàÀ§(¾Ë°í¸®Áò)À» ¼±Åà ÇÒ¼ö ÀÖ´Â ÆÐÅÏÀÌ´Ù. ÀÚ¹Ù½ºÅ©¸³Æ®¿¡¼´Â Äݹé ÇÔ¼ö¸¦ ÀÌ¿ëÇØ °£´ÜÇÏ°Ô ±¸Çö ÇÒ¼ö ÀÖ´Ù. ÀÚ¹Ù½ºÅ©¸³Æ®¿¡¼´Â "º°µµÀÇ interface strategy"¾øÀÌ ¹Ù·Î "Concrete strategy"¸¦ »ç¿ëÇÑ´Ù. Actor : Context Concrete strategy : walk, Run, Swim execute : exec ÇÔ¼ö var Actor = function(name) {
this.bagWeight = 20; this.name = name; this.velocity = 100; this.command = ""; }; Actor.prototype = { setCommand: function(command) { this.command = command; return this; }, play: function() { this.command.exec(this); } }; let walk = { exec: function(actor) { showText(actor.name + ' walk velocity : ' + String(actor.velocity)); } }; let Run = function() { this.calc = function(actor) { return actor.velocity - actor.bagWeight/2; } this.exec = function(actor) { showText(actor.name + ' run velocity : ' + String(this.calc(actor))); } }; let Swim = function() { this.disadvantage = 30; this.calc = function(actor) { return actor.velocity - (actor.bagWeight + this.disadvantage); } this.exec = function(actor) { showText(actor.name + ' swim velocity : ' + String(this.calc(actor))); } }; function run() { let actor = new Actor('kim'); let actor1 = new Actor('lee'); actor1.velocity = 120; actor.setCommand(walk).play(); actor.setCommand(new Run).play(); actor.setCommand(new Swim).play(); actor1.setCommand(new Swim).play(); } run(); function showText(str) { document.body.innerHTML += `<p>${str}</p>`; } °á°ú) kim walk velocity : 100 kim run velocity : 90 kim swim velocity : 50 lee swim velocity : 70 ´Ù¿î·Îµå: strategy_pattern.html Âü°í) https://gist.github.com/Integralist/5736427 https://www.dofactory.com/javascript/strategy-design-pattern https://monsterlessons.com/project/lessons/strategy-pattern-v-javascript |