Design Patterns

์›๋ฆฌ๋ฅผ ์•Œ๋ฉด ๋น„์Šทํ•œ ๋„๊ตฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋ ๊ฒƒ์ธ๊ฐ€ ๋„๊ตฌ๋ฅผ ์ดํ•ดํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋ ๊ฒƒ์ธ๊ฐ€.

What is a Pattern?

  1. Proven - ํŒจํ„ด์ด๋ผ๋Š” ๊ฒƒ์€ ๊ฒ€์ฆ์ด ๋๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  2. Reusable - ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. out of the box solution - ๋ฐ•์Šค์—์„œ ๊บผ๋‚ด์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

  3. Expressive - ํ‘œํ˜„์ ์ด๋‹ค. ๋ช…์นญ์ด๋‚˜ ์ด๋ฆ„์„ ๋ณด๊ณ  ์œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ณ„ํš(๋ฐฉํ–ฅ์„ฑ)์ด์ง€ ์†”๋ฃจ์…˜์„ ์ฃผ๋Š”๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

Object Creation Pattern

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž์ฃผ ์“ฐ์ด๋Š” ํŒจํ„ด์„ ๋งํ•œ๋‹ค

  1. 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๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์€ ์žฌ์‚ฌ์šฉ์ด ์–ด๋ ต๋‹ค.

  • ๊ผญ ์จ์•ผํ•œ๋‹ค๋ฉด ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋‚˜๋งŒ์˜ ๊ณต๊ฐ„์„ ๋šซ์–ด๋†“๋Š”๋‹ค. window.VANILLA_CODING = {} ํ•ด์„œ ๊ณต๊ฐ„์„ ๋งŒ๋“ ๋‹ค.

  • ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜

  • ํŒฉํ† ๋ฆฌํ•จ์ˆ˜๋ฅผ ์ ‘๊ทผ๋ชปํ•˜๊ฒŒ ํ•˜๊ธฐ

  1. SingleTons

    • ๊ฐ์ฒด๊ฐ€ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค์–ด ์ง€๋„๋ก ํ•œ๋‹ค. ์ œํ•œ์„ ๊ฑธ์–ด์ค€๋‹ค.

  2. Factory Pattern

  3. Mixin - ๊ณต์œ ํ•œ๋‹ค. ์ฝ”๋“œ๋ฅผ ์žฌํ™œ์šฉํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ํ™•์žฅ์„ ํ•ด์ฃผ๊ธฐ๋„ ํ•œ๋‹ค.

    • ๋ณ€ํ™”์—†์ด ํ™•์žฅํ•ด์ค€๋‹ค.

  4. Chaining

  5. publish - subscribe (๊ณ ๋ฆด๋ผ ์—…๋ฌด์— ๋“ค์–ด ์žˆ๋‹ค.)

Last updated

Was this helpful?