이 실습에서는 Builder와 Prototype 패턴을 활용하여 복잡한 객체 생성 문제를 해결하는 다양한 기법을 직접 구현합니다.
실습 목표
- Builder 패턴의 다양한 구현 방식 학습
- Prototype 패턴의 깊은 복사와 얕은 복사 이해
- 불변 객체와 Builder 패턴 조합
- 성능 최적화된 객체 생성 전략
실습 1: HTTP 클라이언트 Builder
요구사항
복잡한 HTTP 요청 설정을 간편하게 생성할 수 있는 Builder 구현
코드 템플릿
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
30
31
32
33
34
35
36
37
| public class HttpRequest {
private final String url;
private final String method;
private final Map<String, String> headers;
private final String body;
private final int timeout;
// TODO 1: private 생성자 구현
// TODO 2: Builder 내부 클래스 구현
public static class Builder {
// TODO: 필수 필드와 선택적 필드 구분
// TODO: 체이닝 메서드들 구현
// TODO: 검증 로직 포함한 build() 메서드
}
// TODO 3: 정적 팩토리 메서드
public static Builder builder() {
return new Builder();
}
}
// TODO 4: 테스트 코드
public class HttpRequestTest {
@Test
public void testBuilder() {
HttpRequest request = HttpRequest.builder()
.url("https://api.example.com")
.method("POST")
.header("Content-Type", "application/json")
.body("{\"name\":\"test\"}")
.timeout(5000)
.build();
// TODO: 검증 로직
}
}
|
실습 2: 게임 캐릭터 Prototype
요구사항
게임 캐릭터의 효율적인 복제 시스템 구현
코드 템플릿
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
30
31
32
| public class GameCharacter implements Cloneable {
private String name;
private int level;
private Stats stats;
private List<Item> inventory;
private Equipment equipment;
// TODO 1: 깊은 복사 구현
@Override
public GameCharacter clone() throws CloneNotSupportedException {
// TODO: 참조 타입 필드들의 깊은 복사 구현
return null;
}
// TODO 2: 복사 생성자 구현
public GameCharacter(GameCharacter other) {
// TODO: 다른 방식의 복사 구현
}
// TODO 3: 빌더와 결합
public Builder toBuilder() {
// TODO: 기존 객체를 바탕으로 Builder 생성
return null;
}
}
// TODO 4: 캐릭터 프로토타입 팩토리
public class CharacterPrototypeFactory {
private final Map<String, GameCharacter> prototypes = new HashMap<>();
// TODO: 프로토타입 등록 및 생성 메서드 구현
}
|
실습 3: 설정 객체 Builder + Prototype
코드 템플릿
1
2
3
4
5
| public class ServerConfig implements Cloneable {
// TODO 1: 불변 필드들과 Builder 패턴 조합
// TODO 2: 환경별 설정 복제 (dev, staging, prod)
// TODO 3: 설정 변경 시 새 인스턴스 생성하는 with* 메서드들
}
|
체크리스트
Builder 패턴
Prototype 패턴
통합 구현
추가 도전
- Type-Safe Builder: 컴파일 타임 검증
- Lens 패턴: 함수형 객체 변형
- Copy-on-Write: 지연 복사 최적화
- Fluent Interface: 자연어에 가까운 API
실무 적용
Builder 패턴 활용
- DTO/VO 객체 생성
- 설정 객체 관리
- 테스트 데이터 빌더
Prototype 패턴 활용
- 객체 풀 관리
- 설정 템플릿 시스템
- 성능 크리티컬한 객체 생성
핵심 포인트: Builder는 복잡한 객체 생성을, Prototype은 효율적인 객체 복제를 담당합니다. 두 패턴의 조합으로 강력한 객체 생성 전략을 구축할 수 있습니다.