DESIGN PATTERN/실용주의 디자인패턴

Chaper 2 - Interface VS Class

파란실버라이트 2012. 12. 18. 14:33
구현 상속보다는 인터페이스 상속이 훨씬 바람직하다.

 

인터페이스를 사용하면 뛰어난 유연성을 획득할 수 있다.

 

void f()

LinkedList list = new LinkedList();
//...
modify( list );

}


void modify( LinkedList list )

{

list.add( ... );
doSomethingWith( list );

}

 

Suppose that a new requirement for fast lookup has now emerged, so the LinkedList
isn't working. You need to replace it with a HashSet.

=>아래와 같이  Interface(Collection or Iterator)를 선언하여 다양성을 이용하자

 

 

f()
{
    Collection c = new HashSet();
    //...
    examine( c );
}
void examine( Collection c )
{

 for( Iterator i = c.iterator(); i.hasNext() ;)

 //...

}

 

to this more-generalized version:


void f()
{
    Collection c = new HashSet();
    //...
    examine( c.iterator() );
}


void examine( Iterator i )

for(; i.hasNext() ; i.next() )
//...

}

 

Holub on 실용주의 디자인패턴의 책 내용을 공부하며 정리한 내용입니다.

저자나 역자에게 문제를 일으킨다면 삭제하겠습니다.