개요

비슷한 일을 수행하는 클래스가 보이면, 공통 부분을 추상화해 슈퍼클래스로 옮길 있다. 데이터는 필드 올리기, 동작은 메서드 올리기 리팩터링을 수행한다.

 

비슷한 대안으로 클래스 추출하기가 있다. 차이는 구성( 위임) 이용하냐 상속을 이용하냐 차이다.

 

예시

연간 비용, 월간 비용



class Employee{
    constructor(name, id, monthlyCost){
        this._id = id;
        this._name = name;
        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{
    constructor(name, staff){
        this._name = name;
        this._staff = staff;
    }
    get staff(){return this._staff.slice();}
    get name(){return this._name;}
    get totalMonthlyCost(){
        return this.staff
            .map(e=e.monthlyCost)
            .reduce((sum,cost)=>sum+cost);
    }
    get headCount(){
        return this.staff.length;
    }
    get totalAnnualCost(){
        return this.totalMonthlyCost * 12;
    }
}


class Party{} //일단 빈 슈퍼클래스를 만들고 확장한다.
class Employee extends Party{
    constructor(name, id, monthlyCost){
        super();
        this._id = id;
        this._name = name;
        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 extends Party{
    constructor(name, staff){
        super();
        this._name = name;
        this._staff = staff;
    }
    get staff(){return this._staff.slice();}
    get name(){return this._name;}
    get totalMonthlyCost(){
        return this.staff
            .map(e=e.monthlyCost)
            .reduce((sum,cost)=>sum+cost);
    }
    get headCount(){
        return this.staff.length;
    }
    get totalAnnualCost(){
        return this.totalMonthlyCost * 12;
    }
}



class Party{
    constructor(name){//데이터부터 옮기기(생성자)
        this._name = name;
    }
}
class Employee extends Party{
    constructor(name, id, monthlyCost){
        super(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 extends Party{
    constructor(name, staff){
        super(name);
        this._staff = staff;
    }
    get staff(){return this._staff.slice();}
    get name(){return this._name;}
    get totalMonthlyCost(){
        return this.staff
            .map(e=e.monthlyCost)
            .reduce((sum,cost)=>sum+cost);
    }
    get headCount(){
        return this.staff.length;
    }
    get totalAnnualCost(){
        return this.totalMonthlyCost * 12;
    }
}


class Party{
    constructor(name){
        this._name = name;
    }
    //옮긴 데이터 관련 메서드 올리기
    get name() {return this._name;}
}
class Employee extends Party{
    constructor(name, id, monthlyCost){
        super(name);
        this._id = id;
        this._monthlyCost = monthlyCost;
    }
    get monthlyCost() {return this._monthlyCost;}
    get id() {return this._id;}
    get annualCost(){ return this._monthlyCost * 12; }
}
class Department extends Party{
    constructor(name, staff){
        super(name);
        this._staff = staff;
    }
    get staff(){return this._staff.slice();}
    get totalMonthlyCost(){
        return this.staff
            .map(e=e.monthlyCost)
            .reduce((sum,cost)=>sum+cost);
    }
    get headCount(){
        return this.staff.length;
    }
    get totalAnnualCost(){
        return this.totalMonthlyCost * 12;
    }
}


class Department extends Party{
    constructor(name, staff){
        super(name);
        this._staff = staff;
    }
    get staff(){return this._staff.slice();}
    //비슷한 로직은 좀 더 추상화된 쪽 이름으로, 혹은 추상화시켜 함수선언바꾸기
    get monthlyCost(){
        return this.staff
            .map(e=e.monthlyCost)
            .reduce((sum,cost)=>sum+cost);
    }
    get headCount(){
        return this.staff.length;
    }
    get annualCost(){
        return this.monthlyCost * 12;
    }
}


class Party{
    constructor(name){
        this._name = name;
    }
    get name() {return this._name;}
    get annualCost(){
        return this.monthlyCost * 12;
    }
}
class Employee extends Party{
    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{
    constructor(name, staff){
        super(name);
        this._staff = staff;
    }
    get staff(){return this._staff.slice();}
    get monthlyCost(){
        return this.staff
            .map(e=e.monthlyCost)
            .reduce((sum,cost)=>sum+cost);
    }
    get headCount(){
        return this.staff.length;
    }
}

 

+ Recent posts