Skip to content

Latest commit

 

History

History
106 lines (99 loc) · 2 KB

12.8 슈퍼클래스 추출하기.md

File metadata and controls

106 lines (99 loc) · 2 KB
  • 12.8 슈퍼클래스 추출하기

    1. before

      class Employee {
        #name;
        #id;
        #monthlyCost;
        constructor(name, id, monthlyCost) {
          this.#name = name;
          this.#id = id;
          this.#monthlyCost = monthlyCost;
        }
        get monthlyCost() {
          return this.#monthlyCost;
        }
        get name() {
          return this.#name;
        }
        get id() {
          return this.#id;
        }
        get annualCost() {
          return this.#monthlyCost * 12;
        }
      }
      
      class Department {
        #name;
        #staff;
        constructor(name, staff) {
          this.#name = name;
          this.#staff = staff;
        }
        get name() {
          return this.#name;
        }
        get staff() {
          return this.#staff;
        }
        get totalMonthlyCost() {
          return this.#staff
            .map((e) => e.monthlyCost)
            .reduce((sum, cost) => sum + cost, 0);
        }
        get headCount() {
          return this.staff.length;
        }
        get totalAnnualCost() {
          return this.totalMonthlyCost * 12;
        }
      }
    2. after

    class Party {
      _name;
      constructor(name) {
        this._name = name;
      }
      get name() {
        return this._name;
      }
      get annualCost() {
        return this.monthlyCost * 12;
      }
    }
    
    class Employee extends Party {
      #id;
      #monthlyCost;
      constructor(name, id, monthlyCost) {
        super(name);
        this.#id = id;
        this.#monthlyCost = monthlyCost;
      }
      get monthlyCost() {
        return this.#monthlyCost;
      }
      get id() {
        return this.#id;
      }
    }
    
    class Department extends Party {
      #staff;
      constructor(name, staff) {
        super(name);
        this.#staff = staff;
      }
      get staff() {
        return this.#staff;
      }
      get monthlyCost() {
        return this.#staff
          .map((e) => e.monthlyCost)
          .reduce((sum, cost) => sum + cost, 0);
      }
      get headCount() {
        return this.staff.length;
      }
    }