7. 1
유연성 상실
• 애자일 개발 방법론
– 디자인과 개발을 병행
– 변화에 대한 유연한 대응 필요
8. 1. 유연성 상실
void f()
{
LinkedList list = new LinkedList();
//...
modify( list );
}
void modify( LinkedList list )
{
list.add( ... );
doSomethingWith( list );
}
9. 1. 유연성 상실
void f()
{
Collection list = new LinkedList();
//...
modify( list );
}
void modify( Collection list )
{
list.add( ... );
doSomethingWith( list );
}
10. 1. 유연성 상실
void f()
{
Collection c = new HashSet();
//...
examine( c );
}
void examine( Collection c )
{
for( Iterator i = c.iterator(); i.hasNext() ;)
//...
}
11. 1. 유연성 상실
void f()
{
Collection c = new HashSet();
//...
examine( c.iterator() );
}
void examine( Iterator i )
{
for(; i.hasNext() ; i.next() )
//...
}
12. 2
결합도 증가
• 결합도 높으면 유지보수 곤란
– 한 곳의 수정 나비효과
• getter/setter = public
13. 3
깨지기 쉬운 기반 클래스
• extends 사용시 파생클래스와
기반클래스의 강한 결합
– 기반클래스 수정 파생 클
래스오작동
14. 3. 약골 기반
class Stack extends ArrayList {
private int topOfStack = 0;
public void push( Object article ) {
add( topOfStack++, article );
}
public Object pop() {
return remove( --topOfStack );
}
public void pushMany( Object[] articles ) {
for( int i = 0; i < articles.length; ++i )
push( articles[i] );
} Stack aStack = new Stack();
} aStack.push("1");
aStack.push("2");
aStack.clear();
15. 3. 약골 기반
해결책 - 캡슐화
class Stack {
private int topOfStack = 0;
private ArrayList theData = new ArrayList();
public void push( Object article ) {
theData.add( topOfStack++, article );
}
public Object pop() {
return theData.remove( --topOfStack );
}
public void pushMany( Object[] articles ) {
for( int i = 0; i < articles.length; ++i )
push( articles[i] );
}
public int size() { // current stack size.
return theData.size();
}
}
16. 4
다중 상속
• 다중 상속이 유용한 경우
– 한 객체가 이것이면서
저것일 필요가 있을 때
17. 4. 다중 상속
interface Base {
void f();
static class Implementation implements Base {
public void f() { /*...*/ }
}
}
// Something과 Base.Implementation 를 동시에 상속
class Derived extends Something implements Base {
Base delegate = new Base.Implementation();
public void f() {
delegate.f();
}
}
18. 5
프레임워크
• 상속 기반의 아키텍처
– Template Method 패턴
– 깨지기도 쉽고
– 구현할 클래스가 너무 많다.
• out of the box
– 합성을 통해 그대로 가져다
쓸 수 있게 해야
19. Factory Method 패턴
• 자바 스윙의 예
– 가상의 요구 사항이라는 함정
– 유연성 위해 필요 이상으로 복잡
– 패턴의 광신도 지양하자.
• 구현상속으로 객체 생성 제어
– 기반 클래스 확장도 없어서
extends를 잘못 사용하는 예
20. Extends의 용도 - 다시
• 정규화
– 공통 연산 기반클래스에 집중
• 한 클래스가 다른 클래스에 약간
의 연산을 추가
• 상속 구조의 Keypoint
– 연산이 공유되는가?
• 컴파일 타임 타입 검사