Design Patterns - Iterator Pattern
Iterator Pattern
컬렉션 구현 방법을 노출시키지 않으면서도 그 집합체 안에 들어있는 모든 항목에 접급할 수 있게 하는 패턴
code
public interface Iterator {
boolean hasNext();
Object next();
void remove();
}
public class IteratorImpl implements Iterator {
Item[] list;
public IteratorImpl() {
...
}
public Object next() {
...
}
public boolean hasNext() {
...
}
public void remove() {
...
}
}
Pros
내부적인 구현 방법을 외부로 노출시키지 않으면서 집합체에 있는 모든 항목에 일일이 접근할 수 있음.
Composite Pattern
객체들을 트리 구조로 구성하여 부분과 전체를 나타내는 계층 구조로 만든 패턴. 클라이언트에서 개별 객체와 복합객체를 똑같은 방법으로 다룰 수 있음.
Component and Composite
Composite는 Component의 구현체이면서 동시에 Component를 구성요소로 가질 수 있기 때문에 계층구조를 만들 수 있다.
Composite Iterator(Iterator in Composite Pattern)
Composite 안에 있는 모든 node에 대해 반복작업을 할 수 있게 해주는 Iterator.
재귀적으로 childNode의 Iterator에 접근.
NullIterator
아무일도 하지 않는 Iterator.
public class NullIterator implements Iterator {
public Object next() {
return null;
}
public boolean hasNext() {
return false;
}
public void remove() {
throw new Exception();
}
}
Author And Source
이 문제에 관하여(Design Patterns - Iterator Pattern), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@versatile309/Design-Patterns-Iterator-Pattern저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)