다형성과 추상 타입
객체지향과 디자인 패턴(최범균 저) 다형성과 추상 타입 파트 정리한 내용입니다.
객체 지향이 주는 장점은 구현 변경의 유연함이다
콘크리트 클래스를 직접 사용해도 문제가 없는데, 왜 추상 타입을 사용하는 것일까?
public class FlowController {
private boolean useFile;
public FlowController(boolean useFile) {
this.useFile = useFile;
}
public void process() {
byte[] data = null;
if (useFile) {
FileDataReader fileReader = new FileDataReader();
data = fileReader.read();
} else {
SockerDataReader sockerReader = new SockerDataReader();
data = sockerReader.read();
}
...
} ByteSource의 종류가 FlowController가 바뀌지 않도록 하는 방법에는 다음의 두 가지가 존재한다.
인터페이스에 대고 프로그래밍하기
Last updated