2 분 소요

💡💡 객체 지향 프로그래밍 입문 시리즈

인프런의 객채 지향 프로그래밍 입문을 기반으로 작성한 게시물 입니다.
(본 시리즈의 모든 예시는 JavaScript(TypeScript)로 변환하여 작성하였습니다.)
객체 지향 프로그래밍 입문_1
객체 지향 프로그래밍 입문_2
객체 지향 프로그래밍 입문_3
객체 지향 프로그래밍 입문_4
객체 지향 프로그래밍 입문_5
객체 지향 프로그래밍 입문_6
객체 지향 프로그래밍 입문_7


“캡슐화 연습”

캡슐화 예제 1

// 캡슐화 전
public authenricate(id: string, pw: string) {
    const member: Member = findOne(id);
    if(member === null) throw new UnauthorizedError
    if(member.getVerificationEmailStatus() !== 2) {
        throw new UnauthorizedError
    }
    return;
}

// 캡슐화 후
public class Member{
    private verificationEmailStatus: number;
    public isEmailVerified() {
        return this.vrificationEmailStatus === 2;
    }
}

public authenticate(id: string, pw: string) {
    const member = await memberRepository.findOne({id});
    if(member.isNull()) {
        throw new UnauthorizedError()
    }	
	
    if(member.isEmailVerified()) {
        throw new UnauthorizedError()
    }
    return;
}

캡슐화 예제 2

// 캡슐화 전
public class Rental {
    private _movie Movie;
    private _daysRented: number;

    public getFrequentRenterPoints(): number {
        if(this.movie.getPriceCode() === Movie.NEW_RELEASE && this.daysRented > 1) {
            return 2;
        }
        return 1;
    }
}

public class Movie {
    public static REGULAR: number = 0;
    public static NEW_RELEASE: number =1;
    private priceCode: number;

    public getPriceCode(): number {
        return this.priceCode;
    }
}

// 캡슐화 후
public class Rental {
    private movie: Movie;
    private daysRented: number;

    public getFrequentRenterPoints() {
        return this.movie.getFrequentRenterPoints(this.daysRented);
}

public class Movie {
    public static REGULAR: number = 0;
    public static NEW_RELEASE: number =1;
    private priceCode: number;
	
    public getFrequentRenterPoints(daysRented: number): number {
        if(this.priceCode === NEW_RELEASE && daysRented > 1) return 2;
        return 1;
    }
}

캡슐화 예제 3

// 캡슐화 전
public class Timer {
    public startTime: Date;
    public stopTime: Date;
}
const t: Timer = new Timer();
t.startTime = Date.now();
...
t.stopTime = Date.now();

const elaspedTime: Date = t.stopTime.getTime() - t.startTime.getTime();

// 캡슐화 후
public class Timer {
    public startTime: Date;
    public stopTime: Date;

    public start(): void {
        this.startTime = Date.now();
    }
    public stop(): void {
        this.stopTime = Date.now();
    }
    public elaspedTime(): Date {
        return this.stopTime.getTime() - this.startTime.getTime();
    }
}

const t: Timer = new Timer;
t.start();
...
t.stop();
const elaspedTime: Date = t.elaspedTime();

캡슐화 예제 4

// 캡슐화 전 코드
public verifyEmail(token: string): void {
    const member: Member = findByToken(token);
    if(member === null) throw new UnauthorizedError();
    if(member.getVerificationEmailStatus() == 2) {
        throw new UnauthorizedError();
    } else {
    member.setVerificationEmailStatus(2);
    }
    // 수정 사항 DB 반영 코드
}

// 캡슐화 후 코드
public verifyEmail(token: string): void {
    const member: Member = findByToken(token);
    if(member === null) throw new UnauthorizedError();
    member.verifyEmail();
    ...
}

public class Member {
    private verificationEmailStatus: number;

    public verifyEmail(): void {
        if(this.isEmailVerified()) throw new ConflictError();
        this.verificationEmailStatus = 2;
    }

    public isEmailVerified(): boolean {
        return this.verificationStatus = 2;
    }
}

댓글남기기