State Pattern1 : C++ State patternÀ» ÀÚ¹Ù½ºÅ©¸³Æ®·Î º¯È¯ State Pattern1¿¡¼ C++ State patternÀ» ÀÚ¹Ù½ºÅ©¸³Æ®·Î º¯È¯ÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ ¾Ë¾Æ º¸¾Ò´Ù. ÀÚ¹Ù½ºÅ©¸³Æ®°¡ C++ Ç༼¸¦ ÇÏ´Â ÄÚµå ó·³ º¸¿´´Ù. Äڵ尡 ¾î·Á¿öÁø ´À³¦ÀÌ µé¾ú´Ù. ÀÎÅͳݿ¡¼ ÀÚ¹Ù½ºÅ©¸³Æ® ´Ù¿î State PatternÀ» ã¾Æ º¸¾Ò´Ù. Krasimir´ÔÀÇ ºí·Î±×¿¡¼ ÀÚ¹Ù½ºÅ©¸³Æ® ´Ù¿î State PatternÀ» ã¾Ò´Ù. »óŸ¦ ¹Ù²Ü¶§ ½ÇÇàµÇ´Â enter, execute, exit ÇÔ¼ö´Â ¾øÁö¸¸ »óÅ°ü¸®´Â ±ò²ûÇØ º¸ÀδÙ. ¸ÕÀú ÄÚµåºÎÅÍ º¸ÀÚ. const loginFsm = {
currentState: 'login form', states: { 'login form': { submit: 'loading' }, 'loading': { success: 'profile', failure: 'error' }, 'profile': { viewProfile: 'profile', logout: 'login form' }, 'error': { tryAgain: 'loading' } } }; const changeState = function (name, stateObj) { const state = stateObj.currentState; if (stateObj.states[state][name]) { stateObj.currentState = stateObj.states[state][name]; console.log(`${state} + ${name} --> ${stateObj.currentState}`); return true; } else { console.error(`can't change currentState:${state} --> command:${name}`); return false; } } //changeState('aaa', loginFsm); changeState('tryAgain', loginFsm); // login form + tryAgain --> login form changeState('submit', loginFsm); // login form + submit --> loading changeState('submit', loginFsm); // loading + submit --> loading changeState('failure', loginFsm); // loading + failure --> error changeState('submit', loginFsm); // error + submit --> error changeState('tryAgain', loginFsm); // error + tryAgain --> loading changeState('success', loginFsm); // loading + success --> profile changeState('viewProfile', loginFsm); // profile + viewProfile --> profile changeState('logout', loginFsm); // profile + logout --> login form currentState´Â ÇöÀç »óŸ¦ ³ªÅ¸³½´Ù. loginFsm.states ´ÙÀ½°ú °°ÀÌ ³ªÅ¸ ³¾ ¼ö ÀÖ´Ù.
changeState ÇÔ¼ö´Â loginFsm.states ¿ÀºêÁ§Æ® ÇÁ·ÎÆÛƼ ÇöÀç »óÅ¿¡¼ ÇØ´çÇÏ´Â »óÅ º¯È¯ ¸í·ÉÀÌ ÀÖÀ¸¸é console.log·Î ÇöÀç »óÅÂ, »óÅ º¯È¯ ¸í·É, ÇöÀç »óŸ¦ Ãâ·ÂÇÑ´Ù. ÇØ´çÇÏ´Â »óÅ º¯È¯ ¸í·ÉÀÌ ÀÖÀ¸¸é console.error·Î ¿¡·¯ ¸Þ¼¼Áö¸¦ Ãâ·ÂÇÑ´Ù. ¿¡·¯³ °æ¿ì´Â ÇöÀç»óÅ¿¡¼ »óÅ º¯È¯ ¸í·ÉÀ» ãÀ»¼ö ¾ø±â ¶§¹®¿¡ ¹ß»ýÇÑ´Ù. tryAgain ¸í·ÉÀº ÇöÀç»óÅ°¡ 'error' »óÅÂÀ϶§¸¸ ½ÇÇàµÈ´Ù. submit ¸í·ÉÀº ÇöÀç»óÅ°¡ 'login form' »óÅÂÀ϶§¸¸ ½ÇÇàµÈ´Ù. ½ÇÇà °á°ú) can't change currentState:login form --> command:tryAgain login form + submit --> loading can't change currentState:loading --> command:submit loading + failure --> error can't change currentState:error --> command:submit changeState @ statePattern2.html:42 error + tryAgain --> loading loading + success --> profile profile + viewProfile --> profile profile + logout --> login form ´Ù¿î·Îµå : statePattern2.html Âü°í) http://krasimirtsonev.com/blog/article/managing-state-in-javascript-with-state-machines-stent |