๐Ÿ“Œํ”„๋ก ํŠธ์—”๋“œ ์˜ˆ์‹œ๋กœ ์‚ดํŽด๋ณด๋Š” SOLID ์›์น™ 2)๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™, ๋ฆฌ์Šค์ฝ”ํ”„ ์น˜ํ™˜ ์›์น™

maru5mangoยท2022๋…„ 5์›” 28์ผ
0

๋””์ž์ธํŒจํ„ด

๋ชฉ๋ก ๋ณด๊ธฐ
3/4

๋ฏธ์…˜

์ €๋ฒˆ ํฌ์ŠคํŒ… ์˜ˆ์‹œ๋กœ ๋Œ์•„๊ฐ€๋ณด์ž.
ํ”„๋ก ํŠธ์—”๋“œ ์˜ˆ์‹œ๋กœ ์‚ดํŽด๋ณด๋Š” SOLID์›์น™ 1)๋‹จ์ผ ์ฑ…์ž„ ์›์น™

์ด๋ฒˆ์—๋Š” ๊ณ ์–‘์ด์˜ ์ข…๋ฅ˜๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ํ•ด๋‹ฌ๋ผ๋Š” ์š”๊ตฌ๊ฐ€ ๋“ค์–ด์™”๋‹ค.

๊ทธ๋ƒฅ ๊ณ ์–‘์ด ์™ธ์—๋„ ๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ, ์ฝ”๋ฆฌ์•ˆ ์ˆํ—ค์–ด, ๋ฒต๊ฐˆ ๊ณ ์–‘์ด, ๋…ธ๋ฅด์›จ์ด ์ˆฒ ์ด๋ ‡๊ฒŒ 4๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๊ณ ์–‘์ด๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ์ข‹๊ฒ ์–ด์š”. ๋‹ค๋ฅธ ์ข…๋ฅ˜ ๊ณ ์–‘์ด๋Š” ์•„์ง ๊ฒฐ์ •์ด ์•ˆ๋‚˜์„œ ๋‚˜์ค‘์— ์ถ”๊ฐ€ ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
๊ทธ๋ฆฌ๊ณ  ๊ณ ์–‘์ด๋ฅผ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ , ๋ถˆ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.
๊ณ ์–‘์ด ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  ๊ณ ์–‘์ด๋ฅผ ๋ถ€๋ฅด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ–‰๋™ํ•ฉ๋‹ˆ๋‹ค.

  • ์ผ๋ฐ˜ ๊ณ ์–‘์ด: ๋‚˜์—๊ฒŒ ๋‹ค๊ฐ€์˜จ๋‹ค,
  • ๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ: ์›์„ 2๋ฒˆ ๊ทธ๋ฆฌ๊ณ  ๋‚˜์—๊ฒŒ ์˜จ๋‹ค.,
  • ์ฝ”๋ฆฌ์•ˆ ์ˆํ—ค์–ด: ๋ฌผ๋„๋Ÿฌ๋ฏธ ๋ฐ”๋ผ๋งŒ ๋ณธ๋‹ค.,
  • ๋ฒต๊ฐˆ ๊ณ ์–‘์ด: 2๋ฒˆ ์ด์ƒ ๋ถˆ๋Ÿฌ์•ผ ๋‚˜์—๊ฒŒ ์˜จ๋‹ค.,
  • ๋…ธ๋ฅด์›จ์ด ์ˆฒ: ๋ฐ•์Šค ์†์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

STEP1. ์š”๊ตฌ์‚ฌํ•ญ ๋ถ„์„

์ €๋ฒˆ์ฒ˜๋Ÿผ ๋‹ค์‹œ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ถ„์„ํ•ด๋ณด์ž.

  • ๊ณ ์–‘์ด๋Š” ์ด 5๊ฐ€์ง€ ์ข…๋ฅ˜๋กœ ๋Š˜์–ด๋‚ฌ๋‹ค. (์ผ๋ฐ˜ ๊ณ ์–‘์ด, ๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ, ์ฝ”๋ฆฌ์•ˆ ์ˆํ—ค์–ด, ๋ฒต๊ฐˆ ๊ณ ์–‘์ด, ๋…ธ๋ฅด์›จ์ด ์ˆฒ)
  • ๊ณ ์–‘์ด ์ข…๋ฅ˜๋Š” ๋” ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ณ ์–‘์ด๋ฅผ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฒ„ํŠผ์ด ํ•„์š”ํ•˜๋‹ค.
  • ๊ณ ์–‘์ด ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  ๋ถ€๋ฅด๋Š” ๋ฒ„ํŠผ์ด ํ•„์š”ํ•˜๋‹ค.
  • ๊ณ ์–‘์ด๋งˆ๋‹ค ๋ถ€๋ฆ„์— ๋‹ตํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค.

STEP2. ํ•  ์ผ ๋‚˜๋ˆ„๊ธฐ

  • makeCat
    ๊ณ ์–‘์ด๋ฅผ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค.
    ๊ณ ์–‘์ด๋Š” ์ด๋ฆ„, ๋‚˜์ด, ์ข…๋ฅ˜(์žˆ์„ ๋•Œ๋งŒ)๋ฅผ ๊ฐ–๋Š”๋‹ค.

  • catCall
    input tag์— ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฆ„์˜ ๊ณ ์–‘์ด๋ฅผ ๋ถ€๋ฅธ๋‹ค.
    input์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

  • Cat Class
    Russian Blue, korean shorthair cat, Bengal cat, Norwegian forest cat์€ ์ผ๋ฐ˜ Cat Class๋ฅผ ์ƒ์†๋ฐ›์•„ ๋งŒ๋“ ๋‹ค. ๊ฐ ๊ณ ์–‘์ด์˜ ํŠน์ง•์€ ๊ฐ class method๋กœ ๋‚ด๋ถ€ ๊ตฌํ˜„ํ•œ๋‹ค.

๐Ÿ“Œ catCall ๊ตฌํ˜„์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด๋ณด์ž
1. switch๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ

function catCall(type){
  switch(type){
    case "๋ฒต๊ฐˆ":
      '์—ฌ๊ธฐ์— ํ–‰๋™์„ ์ž‘์„ฑ'
    case "๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ":
      '์—ฌ๊ธฐ์— ํ–‰๋™์„ ์ž‘์„ฑ'
    ....
  }
}
  1. ํŠน์ • method ํ˜ธ์ถœ
function catCall(catClass){
  catClass.come();
}

๐Ÿ“‘ SOLID์›์น™ 2. ๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™(OCP, Open-Closed Principle)
์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ์ฒด(ํด๋ž˜์Šค, ๋ชจ๋“ˆ, ํ•จ์ˆ˜ ๋“ฑ๋“ฑ)๋Š” ํ™•์žฅ์— ๋Œ€ํ•ด ์—ด๋ ค ์žˆ์–ด์•ผ ํ•˜๊ณ , ์ˆ˜์ •์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ซํ˜€ ์žˆ์–ด์•ผ ํ•œ๋‹ค

  • ์ฒซ๋ฒˆ์งธ catCall ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ, cat์˜ ์ข…๋ฅ˜๊ฐ€ 100๊ฐœ๋กœ ๋Š˜์–ด๋‚œ๋‹ค๋ฉด switch๋ฌธ ์‚ฌ์šฉ์ด ์–ด๋ ค์›Œ์งˆ ๊ฒƒ์ด๋‹ค. ์‹ค์ˆ˜๋กœ ํ•˜๋‚˜๋ผ๋„ ๋นผ๋จน๋Š”๋‹ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์€ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๊ฒƒ์ด๋‹ค. ( ํ™•์žฅ์— ์—ด๋ ค์žˆ์ง€ ์•Š๋‹ค.)
  • ์ฒซ๋ฒˆ์งธ catCall ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ, cat์ด ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค catCall์˜ ํ•จ์ˆ˜ ๋˜ํ•œ ์ˆ˜์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ( ์ˆ˜์ •์— ๋‹ซํ˜€์žˆ์ง€ ์•Š๋‹ค. )
  • ๋‘๋ฒˆ์งธ catCallํ•จ์ˆ˜๋Š” catClass๋ฅผ cat ์ข…๋ฅ˜๊ฐ€ 100๊ฐœ๊ฐ€ ๋˜์–ด๋„, ์ˆ˜์ •์ด ํ•„์š”์—†๋‹ค. ( ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™์„ ์ง€ํ‚จ ์ฝ”๋“œ๋‹ค. )

๐Ÿ“‘ SOLID์›์น™ 3. ๋ฆฌ์Šค์ฝ”ํ”„ ์น˜ํ™˜ ์›์น™์ด๋ž€ (Liskov Substitution Principle)
์ƒ์œ„ ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ํ•˜์œ„ ํƒ€์ž…์˜ ๊ฐ์ฒด๋กœ ์น˜ํ™˜ํ•ด๋„ ๋™์ž‘์— ๋ฌธ์ œ๊ฐ€ ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ๋Š” CatClass๋ฅผ ์ƒ์†๋ฐ›์•„ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ผ๋ฐ˜ ๊ณ ์–‘์ด๋ฅผ RussianBlueCat์œผ๋กœ ์ „๋ถ€ ์น˜ํ™˜ํ•˜๋”๋ผ๋„ ํ”„๋กœ๊ทธ๋žจ์€ ๋ฌธ์ œ์—†์ด ๋Œ์•„๊ฐ€์•ผ ํ•œ๋‹ค.

STEP3. ๊ตฌํ˜„ํ•˜๊ธฐ

main

function main() {
  new InfoToolTip();
  new StateToolTip();
  const makeCatClass = new makeCatButton();
  const callCatFunc = callCat(makeCatClass);
  new callCatBtn(callCatFunc);

  makeCatClass.click();
}

makeCat

function makeCat() {
  const $parent = document.getElementById("playground");
  const randomAge = getRandomNumber(1, 14);
  const randomName = CAT_NAME_LIST[randomAge % 6];
  const idx = getRandomNumber(0, 4);
  const catType = [
    Cat,
    RussianBlue,
    koreanShorthairCat,
    BengalCat,
    NorwegianForestCat,
  ];
  return new catType[idx]($parent, {
    name: `${randomName}${new Date().getTime()}`,
    age: randomAge,
  });
}

์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š” Cat (ex. ๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ)

class RussianBlue extends Cat {
  constructor($parent, data) {
    super($parent, {
      ...data,
      type: "๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ",
    });
    this._dataController;
  }

  /*
   * @override
   */
  create() {
    this._domController.createDom(
      `<div class="russianBlueCat">๋Ÿฌ์‹œ์•ˆ ๋ธ”๋ฃจ</div>`
    );
    this._domController.setEvent("russianBlueCat");
  }

  /*
   * @override
   */
  call() {
    console.log("์›์„ 2๋ฒˆ ๊ทธ๋ฆฌ๊ณ  ๋‚˜์—๊ฒŒ ์˜จ๋‹ค");
  }
}

makeCat

class makeCatButton {
  #result;
  #maxNum;

  constructor() {
    this.$el = document.getElementById("makeCatBtn");
    this.#result = [];
    this.#maxNum = 6;
    this.#setEvent();
  }

  getCatBox() {
    return this.#result.map((catClass) => {
      return catClass.getName();
    });
  }

  getCatClass(name) {
    const catNameList = this.getCatBox();
    const idx = catNameList.findIndex((catName) => catName === name);
    return this.#result[idx];
  }

  #setEvent() {
    this.$el.addEventListener("click", () => {
      if (this.#result.length === this.#maxNum) {
        alert("๊ณ ์–‘์ด๋ฅผ ๋” ์ด์ƒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์–ด์š”!!!");
      } else {
        this.#result.push(makeCat());
      }
    });
  }

  click() {
    this.$el.click();
  }
}

catCall

function callCat(makeCatClass) {
  const $input = document.getElementById("catCallName");

  return function () {
    const name = $input.value;
    if (name === "") return alert("์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
    const nameList = makeCatClass.getCatBox();
    if (!nameList.includes(name))
      return alert("๊ณ ์–‘์ด ์ด๋ฆ„์„ ๋‹ค์‹œ ํ™•์ธํ•ด์ฃผ์„ธ์š”.");
    const catClass = makeCatClass.getCatClass(name);
    catClass?.call();
  };
}

์ฝ”๋“œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ > github

0๊ฐœ์˜ ๋Œ“๊ธ€