플라이웨이트 패턴을 사용해 동일한 (char, style) 조합에 대해 항상 같은 인스턴스를 반환하는 CharFlyweight 팩토리를 구현하세요.
많은 텍스트를 표시할 때 같은 문자/스타일을 반복 생성하면 메모리를 낭비합니다. 공유 가능한 내재 상태(intrinsic)를 캐시하여 메모리를 절약합니다.
export class Character {
constructor(public readonly char: string, public readonly style: string) {}
}
export class CharFlyweight {
forge(char: string, style: string): Character; // 같은 키 → 같은 인스턴스
count(): number; // 유니크 플라이웨이트 개수
}
const f = new CharFlyweight();
const a1 = f.forge('a', 'bold');
const a2 = f.forge('a', 'bold');
const a3 = f.forge('a', 'italic');
console.log(a1 === a2); // true — 같은 키 공유
console.log(a1 === a3); // false — 스타일이 다름
console.log(f.count()); // 2
===/==로 동일성 검증