상세 컨텐츠

본문 제목

6.9. 여러 함수를 클래스로 묶기

🍜개발자라면/책을 읽자✍

by :Eundms 2021. 9. 26. 16:32

본문

- 여러 함수를 묶는 방법 

1) 클래스로 묶기

: 클라이언트가 객체의 핵심 데이터를 변경할 수 있다. 파생 객체들을 일관되게 관리할 수 있다.

 원본 데이터가 코드 안에서 갱신될 때 - 클래스로 묶기

2) 변환 함수로 묶기(6.10)

: 원본 데이터를 입력받아서 필요한 정보를 모두 도출한 뒤, 각각을 출력 데이터의 필드에 넣어 반환한다.

3) 중첩 함수 형태로 묶기

: 테스트하기 까다로울 수 있다.

4) 함수를 객체처럼 패턴 이용(클래스 지원X)

 


방법

1. 공통 데이터 레코드를 캡슐화한다.

2. 공통 레코드를 사용하는 함수 각각을 새 클래스로 옮긴다.

3. 데이터를 조작하는 로직들은 함수로 추출해서 새 클래스로 옮긴다.

 


  여러 함수를 클래스로 묶기  

어렵다...
// 레코드
const reading = {customer:"ivan", quantity:10, month:5, year:2017};

//레코드 캡슐화
class Reading {
    constructor(data){
        this._customer = data.customer;
        this._quantity = data.quantity;
        this._month = data.month;
        this._year = data.year;
    }
    get customer(){return this._customer;}
    get quantity(){return this._quantity;}
    get month(){return this._month;}
    get year(){return this._year;}
    
    get baseCharge(){
        return baseRate(this.month,this.year)*this.quantity;
    }
    get taxableCharge(){
        return Math.max(0, this.baseCharge-taxThreshold(this.year));
    }

}

 

 

 

 

관련글 더보기

댓글 영역