Featured image of post [Design Patterns] 브리지와 플라이웨이트 패턴 실습 - 분리와 효율성

[Design Patterns] 브리지와 플라이웨이트 패턴 실습 - 분리와 효율성

Bridge와 Flyweight 패턴을 통해 추상화와 구현의 분리, 메모리 효율성을 실습합니다. GUI 컴포넌트 시스템, 게임 객체 최적화, 텍스트 에디터 등의 프로젝트를 통해 확장성과 성능 최적화를 동시에 달성하는 고급 설계 기법을 학습합니다.

이 실습에서는 Bridge 패턴으로 다중 플랫폼 시스템을, Flyweight 패턴으로 메모리 효율적인 시스템을 직접 구현합니다.

실습 목표

  1. Bridge 패턴으로 다중 플랫폼 파일 시스템 구현
  2. Flyweight 패턴으로 메모리 효율적인 텍스트 렌더링 구현
  3. 패턴 적용 전후 성능 비교 분석

과제 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 패턴

  • 내재적/외재적 상태 분리
  • 팩토리 정상 동작
  • 메모리 절약 확인
  • 대용량 처리 테스트

성능 측정

  • 오버헤드 측정
  • 메모리 절약률 계산
  • 결과 분석 및 문서화

추가 도전 과제

  1. Bridge + Strategy 결합으로 파일 압축 알고리즘 적용
  2. Flyweight + Observer 결합으로 문서 변경 알림
  3. JIT 최적화를 고려한 성능 개선

실습 팁

  • 작은 단위로 구현하고 테스트
  • 메모리 측정 도구(JProfiler, VisualVM) 활용
  • JVM 워밍업 고려한 성능 측정
  • 실제 사용 시나리오 기반 테스트 케이스 작성