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

  • 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();
    })();
  1. 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;
            }
          }
        }
      })();
  2. Factory Pattern

  3. 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');
  4. Mixin - ๊ณต์œ ํ•œ๋‹ค. ์ฝ”๋“œ๋ฅผ ์žฌํ™œ์šฉํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ํ™•์žฅ์„ ํ•ด์ฃผ๊ธฐ๋„ ํ•œ๋‹ค.

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

  5. var yum = MyenuriFactory('yum');
    
    function fighterDNAMixin (goodPerson) {
      goodPerson.fight = function (opponent) { // ์ ์„ ์ฐ์—ˆ๋‹ค. ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด์˜ค๋Š”๊ตฌ๋‚˜.'ใ…'
        console.log(opponent.name + '.. ์ €๊ฑฐ ์น˜์›Œ..');
      };
    }
    
    fighterDNAMixin(yum);
  6. Chaining

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

Last updated

Was this helpful?