이 실습에서는 Bridge 패턴으로 다중 플랫폼 시스템을, Flyweight 패턴으로 메모리 효율적인 시스템을 직접 구현합니다.
실습 목표
- Bridge 패턴으로 다중 플랫폼 파일 시스템 구현
- Flyweight 패턴으로 메모리 효율적인 텍스트 렌더링 구현
- 패턴 적용 전후 성능 비교 분석
과제 1: Bridge 패턴 - 파일 시스템
기본 구조
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // 추상화: 파일 매니저
public abstract class FileManager {
protected FileSystemImpl fileSystem;
public FileManager(FileSystemImpl fileSystem) {
this.fileSystem = fileSystem;
}
public abstract void copyFile(String source, String destination);
public abstract void moveFile(String source, String destination);
public abstract boolean fileExists(String path);
}
// 구현 인터페이스
public interface FileSystemImpl {
void createFile(String path, String content);
String readFile(String path);
void deleteFile(String path);
boolean exists(String path);
String getPathSeparator();
}
|
구현 과제
- WindowsFileSystem (경로 구분자: , 드라이브 문자 지원)
- LinuxFileSystem (경로 구분자: /, 권한 모드)
- MacFileSystem (확장 속성 지원)
- BasicFileManager, SecureFileManager 구현
과제 2: Flyweight 패턴 - 텍스트 렌더링
기본 구조
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // Flyweight 인터페이스
public interface CharacterFlyweight {
void render(RenderContext context, int x, int y);
int getWidth(RenderContext context);
int getHeight(RenderContext context);
}
// 팩토리
public class CharacterFlyweightFactory {
private final Map<String, CharacterFlyweight> flyweights = new ConcurrentHashMap<>();
public CharacterFlyweight getFlyweight(char character, String fontFamily,
int fontSize, boolean isBold, boolean isItalic) {
// TODO: 구현
return null;
}
}
|
구현 과제
- ConcreteCharacterFlyweight (내재적 상태 관리)
- TextDocument (외재적 상태 관리)
- 100만 개 문자 처리 성능 테스트
과제 3: 성능 비교
측정 항목
- Bridge: 직접 구현 vs 패턴 적용 시간 오버헤드
- Flyweight: 일반 구현 vs 패턴 적용 메모리 사용량
- 처리량과 응답 시간 비교
완성도 체크리스트
Bridge 패턴
Flyweight 패턴
성능 측정
추가 도전 과제
- Bridge + Strategy 결합으로 파일 압축 알고리즘 적용
- Flyweight + Observer 결합으로 문서 변경 알림
- JIT 최적화를 고려한 성능 개선
실습 팁
- 작은 단위로 구현하고 테스트
- 메모리 측정 도구(JProfiler, VisualVM) 활용
- JVM 워밍업 고려한 성능 측정
- 실제 사용 시나리오 기반 테스트 케이스 작성