Design Patterns
์๋ฆฌ๋ฅผ ์๋ฉด ๋น์ทํ ๋๊ตฌ๋ฅผ ๋ง๋ค ์ ์๋ค. ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ์ฌ๋์ด ๋ ๊ฒ์ธ๊ฐ ๋๊ตฌ๋ฅผ ์ดํดํ๋ ์ฌ๋์ด ๋ ๊ฒ์ธ๊ฐ.
What is a Pattern?
Proven - ํจํด์ด๋ผ๋ ๊ฒ์ ๊ฒ์ฆ์ด ๋๋ค๋ ๊ฒ์ด๋ค.
Reusable - ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค. out of the box solution - ๋ฐ์ค์์ ๊บผ๋ด์ ๋ฐ๋ก ์ฌ์ฉํ๋ค.
Expressive - ํํ์ ์ด๋ค. ๋ช ์นญ์ด๋ ์ด๋ฆ์ ๋ณด๊ณ ์ ์ถ๊ฐ ๊ฐ๋ฅํ๋ค. ๊ณํ(๋ฐฉํฅ์ฑ)์ด์ง ์๋ฃจ์ ์ ์ฃผ๋๊ฒ์ ์๋๋ค.
Object Creation Pattern
๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์์ฃผ ์ฐ์ด๋ ํจํด์ ๋งํ๋ค
Modules - ๊ฐ์ฒด, ํฉ์ณ์ ธ์ ๋ญ๊ฐ๋ฅผ ์ด๋ฃจ๋๊ฑธ ๋งํ๋ค. ์์๊ฑฐ๋ ํฐ๊ฑฐ๋ ์ํฉ์ ๋ฐ๋ผ์ ๋ค๋ฅด๋ค.
๊ฐ์ฒด ๋ฆฌํฐ๋ด
var module = { someProp: 'hello', someMethod: function () { console.log(this.someProp); } };์ ์ ํ์ง ์๋ ์ฌํญ
var bankAccount = { balance: 900, deposit: function (amount) { this.balance += amount; }, withdraw: function (amount) { this.balance -= amount; } }; bankAccount.deposit(1000); bankAccount.withdraw(1000); bankAccount.balance = 0; // ?!
Privacy Concerns - ์ฝ๋๋ฅผ ์จ๊ธฐ๋๊ฑด ์ด๋ ต๋ค. ๊ทธ๋์ ์ ๊ทผํ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ค์ด์ผ ํ๋ค.
์ด๋ ๊ฒ ์ฆ์์คํํจ์ IIFE๋ฅผ ์ฌ์ฉํ๋๊ฒ์ ์ฌ์ฌ์ฉ์ด ์ด๋ ต๋ค.
var bankAccount = (function (){ var balance = 900; return { deposit: function (amount) { balance += amount; }, withdraw: function (amount) { balance -= amount; } }; })(); var human1BankAccount = bankAccount(); human1BankAccount.deposit();๊ผญ ์จ์ผํ๋ค๋ฉด ๋ค์์คํ์ด์ค ๋๋ง์ ๊ณต๊ฐ์ ๋ซ์ด๋๋๋ค. window.VANILLA_CODING = {} ํด์ ๊ณต๊ฐ์ ๋ง๋ ๋ค.
ํฉํ ๋ฆฌ ํจ์
function bankAccount (){ var balance = 900; return { deposit: function (amount) { balance += amount; }, withdraw: function (amount) { balance -= amount; } }; } var human1BankAccount = bankAccount(); human1BankAccount.deposit();ํฉํ ๋ฆฌํจ์๋ฅผ ์ ๊ทผ๋ชปํ๊ฒ ํ๊ธฐ
(function bankApp () { function bankAccount (){ var balance = 900; return { deposit: function (amount) { balance += amount; }, withdraw: function (amount) { balance -= amount; } }; } var human1BankAccount = bankAccount(); human1BankAccount.deposit(); })();
SingleTons
๊ฐ์ฒด๊ฐ ํ๋๋ง ๋ง๋ค์ด ์ง๋๋ก ํ๋ค. ์ ํ์ ๊ฑธ์ด์ค๋ค.
var userModule = (function () { var users = []; var userId = 0; return { create: (username, password) => { var user = { id: userId, username, password }; users.push(user); userId++; return user; }, get: (username) => { var targetUser; users.forEach((user) => { if (user.username === username) { targetUser = user; } }); return targetUser; } } })();var HTTPModule = (function () { var instance = null; return { create: function () { if (!instance) { instance = { get: url => { return $.get(url); }, post: (url, data) => { return $.post(url, data); } }; return instance; } else { return instance; } } } })();
Factory Pattern
function fight() { console.log('์ผ์์์์!!!'); } /* Factory Pattern */ function MyenuriFactory (name) { const m = {}; m.name = name; m.from = 'poor family'; m.cry = function () { console.log('์ฌ์กํจ๋ฏธ๋ค..'); }; m.fight = fight; //์ธ์ฐ๋ ๊ธฐ๋ฅ์ด ์๋ค. return m; } function SyemooniFactory (level) { const s = {}; s.level = level; s.speak = function () { console.log('์ ๊ฑฐ ์น์...'); }; s.fight = fight; // ์ธ์ฐ๋ ๊ธฐ๋ฅ์ด ์๋ค. return s; } const kim = SyemooniFactory(999); const yum = MyenuriFactory('yum');Mixin - ๊ณต์ ํ๋ค. ์ฝ๋๋ฅผ ์ฌํ์ฉํ๋ ํจํด์ด๋ค. ํ์ฅ์ ํด์ฃผ๊ธฐ๋ ํ๋ค.
๋ณํ์์ด ํ์ฅํด์ค๋ค.
var yum = MyenuriFactory('yum'); function fighterDNAMixin (goodPerson) { goodPerson.fight = function (opponent) { // ์ ์ ์ฐ์๋ค. ๊ฐ์ฒด๊ฐ ๋ค์ด์ค๋๊ตฌ๋.'ใ ' console.log(opponent.name + '.. ์ ๊ฑฐ ์น์..'); }; } fighterDNAMixin(yum);Chaining
publish - subscribe (๊ณ ๋ฆด๋ผ ์ ๋ฌด์ ๋ค์ด ์๋ค.)
Last updated
Was this helpful?