
ocp 개방폐쇄 원칙
다형성-제우스
클래스는?
변화의 기본 단위가 클래스입니다.
lsp
상속관계가 잘 되어있는지
부모가 할 수 있는 일은 자식이 할 수 있어야 한다.
dip
<코드>는 사용자를 등록하고 이 사실을 이메일로 알리는 코드이다. <코드>에 대해 올바르게 설명한 것은?
<코드>
class UserRegistration {
public void register(String username, String email) {
// 사용자 등록 로직
sendConfirmation(email); //이메일로 사용자 등록 사실 전송
}
private void sendConfirmation(String email) {
//이메일 전송
}
}
하나를 선택하세요.
a. SRP와 OCP를 모두 만족한다.
b. SRP는 만족하지 않지만 OCP를 만족한다.
c. SRP는 만족하지만 OCP를 만족하지 않는다.
d. SRP와 OCP를 모두 만족하지 못한다. 정답

<코드>를 LSP 관점에서 평가할 때 문제를 가장 적절하게 지적한 것은?
<코드>
import java.util.*;
// 저장 실패 표현
class StorageException extends RuntimeException {
StorageException(String msg) { super(msg); }
}
// 계약을 지키도록 강제하는 템플릿
abstract class AbstractStore {
protected static final int GUARANTEED_MAX = 1024 * 1024; // 1MB
public final void save(byte[] d) {
Objects.requireNonNull(d, "data");
if (d.length <= GUARANTEED_MAX) {
// 반드시 성공해야 하는 경로
doSaveGuaranteed(d);
return;
}
// 미규정 영역: 구현마다 다르게 처리
doSaveUnspecified(d);
}
// 하위가 반드시 성공하도록 구현해야 하는 저장 훅
protected abstract void doSaveGuaranteed(byte[] d);
// 기본값은 거부. 확장 구현은 이 메서드에서 저장 허용 가능
protected abstract void doSaveUnspecified(byte[] d);
}
// 1MB 이하만 저장하는 기본 구현
final class StrictStore extends AbstractStore {
private final List<byte[]> data = new ArrayList<>();
@Override
protected void doSaveGuaranteed(byte[] d) {
// 반드시 성공해야 하는 경로
data.add(Arrays.copyOf(d, d.length));
}
// 기본값은 거부. 확장 구현은 이 메서드에서 저장 허용 가능
protected void doSaveUnspecified(byte[] d) {
throw new StorageException("size not supported: " + d.length);
}
}
final class ExtendedStore extends AbstractStore {
private final List<byte[]> data = new ArrayList<>();
private final int maxBytes;
ExtendedStore(int maxBytes) {
if (maxBytes < GUARANTEED_MAX) throw new IllegalArgumentException("maxBytes >= GUARANTEED_MAX");
this.maxBytes = maxBytes;
}
@Override
protected void doSaveGuaranteed(byte[] d) {
data.add(Arrays.copyOf(d, d.length));
}
@Override
protected void doSaveUnspecified(byte[] d) {
if (d.length > maxBytes) throw new StorageException("too big: " + d.length);
data.add(Arrays.copyOf(d, d.length));
}
}
public class Main {
static byte[] bytesOfSize(int n) { return new byte[n]; }
public static void main(String[] args) {
AbstractStore strict = new StrictStore();
AbstractStore extended = new ExtendedStore(10 * 1024 * 1024); // 10MB
strict.save(bytesOfSize(512 * 1024));
extended.save(bytesOfSize(512 * 1024));
try {
strict.save(bytesOfSize(5 * 1024 * 1024));
System.out.println("strict: saved 5MB");
} catch (StorageException ex) {
System.out.println("strict: rejected 5MB -> " + ex.getMessage());
}
extended.save(bytesOfSize(5 * 1024 * 1024));
System.out.println("extended: saved 5MB");
try {
extended.save(bytesOfSize(12 * 1024 * 1024));
} catch (StorageException ex) {
System.out.println("extended: rejected 12MB -> " + ex.getMessage());
}
}
}
하나를 선택하세요.
a. StrictStore, ExtendedStore 모두 LSP를 만족한다. 정답
b. StrictStore는 LSP를 만족하지만 ExtendedStor는 LSP를 만족하지 않는다.
c. StrictStore, ExtendedStore 모두 LSP를 만족하지 않는다.
d. StrictStore는 LSP를 만족하지 않지만 ExtendedStor는 LSP를 만족한다.
<클래스 다이어그램>과 <코드>에서 살펴볼 수 있는 설계는 ISP 설계 원칙을 준수하고 있다.

<클래스 다이어그램>
image (3).png
<코드>
class UseFoo1 {
public void doIt1(IFoo foo) {
foo.a1();
foo.a2(); }
}
class UseFoo2 {
public void doIt2(IFoo foo) {
foo.a3();
foo.a4();
foo.a5();
}
}
하나를 선택하세요.
참
거짓 정답
피드백
정답 : '거짓'