[TYPESCRIPT] 타입스크립트 디자인 패턴 - Facade

Facade Pattern

The façade pattern is one of the structural software design patterns.

Structural: 객체간의 관계를 처리

이점

  1. 여러시스템을 통합하는 인터페이스가 필요한 경우 사용.

서브시스템1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// subsystem1.ts
export class Contract {
private contractTerminationDateexport class Contract {
private contractTerminationDate: Date;
constructor(terminationDate: Date) {
this.contractTerminationDate = terminationDate;
}
checkActiveContract(date: Date): boolean {
if (date < this.contractTerminationDate) {
return true;
} else {
return false;
}
}
}

서브시스템2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// subsystem2.ts
export class TeamFunds {
private totalFunds: number;
constructor(total: number) {
this.totalFunds = total;
}
checkFunds(transferFee: number): boolean {
if (transferFee < this.totalFunds) {
return true;
} else {
return false;
}
}
}

Facade 패턴으로 2개의 서브 시스템을 통합하여 기능제공

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// facade.ts
import { Contract } from './subsystem1';
import { TeamFunds } from './subsystem2';

export class Facade {
private contract: Contract = new Contract(new Date(new Date().setDate(new Date().getDate() + 10)));
private funds: TeamFunds = new TeamFunds(200000);

private playerContract: Date;
private playerPrice: number;

constructor(playerPrice: number) {
this.playerContract = new Date();
this.playerPrice = playerPrice;
}

buyPlayer(): void {
if (this.contract.checkActiveContract(this.playerContract)) {
console.log('Player has active contract');
} else {
console.log('Player has no active contract');
if (!this.funds.checkFunds(this.playerPrice)) {
console.log('Player is too expensive to buy.');
} else {
console.log('Player can be bought.');
}
}
}
}

사용시

1
2
3
4
5
// app.ts
import { Facade } from './facade';

const facade: Facade = new Facade(200001);
facade.buyPlayer();