6-9. 여러 함수를 클래스로 묶기

적용 시점

  • 공통 데이터를 중심으로 긴밀하게 엮여 작동하는 함수들이 존재하는 경우

  • 새로 만든 클래스와 관련하여 놓친 연산을 찾은 경우

  • 울타리로 묶을 함수들 중 외부에 공개할 함수가 여러 개인 경우

절차

  1. 함수들이 공유하는 공통 데이터 레코드를 캡슐화

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

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

예시

❌Before

const countProduct = (productName: string) => { // 비즈니스 로직 }
const getDiscountProduct = (productName: string) => { // 비즈니스 로직 }
const getProductInfo = (productName: string) => { // 비즈니스 로직 }

⭕After

const Product {
	private productName: string;
	constructor(data) {
		this.productName = data;
	}
	countProduct () { // this.productName을 쓴다 }
	getDiscontProduct () { // this.productName을 쓴다 }
	getProductInfo() { // this.productName을 쓴다 }
}

Last updated