Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[오혜성] 챕터 7: 자바스크립트 디자인 패턴 (2/3) #56

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

hyesungoh
Copy link
Member

👍

Copy link
Member

@100Gyeon 100Gyeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏👏👏

Copy link
Contributor

@sangbooom sangbooom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻👍🏻👍🏻

Comment on lines +365 to +441
> 그래서 준비한 타입스크립트로 작성한 추상 데코레이터 패턴

```tsx
class MacBook {
// protected ??
// public = 어디서나 접근 가능
// private = 이 클래스 내부에서만 접근 가능
// protected = 이 클래스와 상속받은 클래스에서만 접근 가능
protected cost: number;
protected screenSize: number;

constructor() {
this.cost = 100;
this.screenSize = 13;
}

getCost(): number {
return this.cost;
}

getScreenSize(): number {
return this.screenSize;
}
}

// 데코레이터들이 구현해야 할 인터페이스 (헝가리안 표기법 ㅈㅅ)
interface IMacBook {
getCost(): number;
getScreenSize(): number;
}

// 추상 데코레이터 클래스
abstract class MacBookDecorator implements IMacBook {
protected macbook: IMacBook;

constructor(macbook: IMacBook) {
this.macbook = macbook;
}

getCost(): number {
return this.macbook.getCost();
}

getScreenSize(): number {
return this.macbook.getScreenSize();
}
}

// 구체적인 데코레이터 클래스들
class Memory extends MacBookDecorator {
getCost(): number {
return this.macbook.getCost() + 100; // 메모리 업그레이드 비용 추가
}
}

// abstract class??
// 추상 클래스라, 직접 인스턴스화 불가능 (new MackBookDecorator)
// 추상 메서드를 포함할 수 있음 (구현부가 없는 메서드)
// 상속받는 클래스에서 추상 메서드를 반드시 구현해야 함
//
// 사용하는 이유
// 1. 공통 기능 구현: 여러 클래스가 공유하는 기본 코드를 구현 (코드 재사용)
// 2. 표준화: 파생되는 클래스들이 반드시 구현하도록 강제
abstract class Shape {
abstract getArea(): number; // 모든 도형은 면적을 계산해야 함
}

class Circle extends Shape {
constructor(public radius: number) {
super();
}

getArea(): number {
return Math.PI * this.radius ** 2;
}
}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GOAT

@hyesungoh hyesungoh merged commit 03536c3 into main Nov 7, 2024
1 check passed
@hyesungoh hyesungoh deleted the 챕터7_2/오혜성 branch November 7, 2024 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants