이터레이터 패턴은 이터레이터(Iterator)를 사용하여 컬렉션(Collection)의 요소들에 접근하는 디자인 패턴입니다.
이를 통해 순회할 수 있는 여러 가지 자료형의 구조와는 상관없이 이터레이터라는 하나의 인터페이스로 순회가 가능합니다.
이터레이터 패턴은 주로 다음과 같은 구성 요소로 구성됩니다.
- Iterator(반복자): 검색된 요소를 순회하는 역할을 담당합니다. 다음 요소로 이동하거나 현재 요소에 접근하는 방법을 제공합니다.
- ConcreteIterator(구체적인 반복자): Iterator 인터페이스를 구현하여 실제로 모음을 순회하는 로직을 구현합니다.
- Aggregate(집합체): 객체들의 집합체를 인터페이스입니다. 이 인터페이스를 구현하는 컬렉션 클래스는 iterator() 메서드를 제공하여 ConcreteIterator 객체를 생성합니다.
- ConcreteAggregate(구체적인 집적체): Aggregate 인터페이스를 구현하여 실제로 집합을 관리하고 iterator() 메서드를 통해 ConcreteIterator 객체를 생성합니다.
Iterator예제 코드
- Iterator(반복자)
// Aggregate 인터페이스 => 집합체 객체 (컬렉션)
interface Aggregate {
Iterator<Integer> iterator();
}
- ConcreteIterator(구체적인 반복자)
// ConcreteAggregate 클래스
class ArrayCollection implements Aggregate {
// 데이터 집합 (컬렉션)
private int[] collection;
public ArrayCollection(int[] collection) {
this.collection = collection;
}
//내부 컬렉션을 인자로 넣어 이터레이터 구현체를 클라이언트에 반환
@Override
public Iterator<Integer> iterator() {
return new ArrayIterator(collection);
}
}
- Aggregate(집합체)
// 반복체 객체
interface Iterator<T> {
boolean hasNext();
T next();
}
- ConcreteAggregate(구체적인 집적체)
// ConcreteIterator 클래스
class ArrayIterator implements Iterator<Integer> {
private int[] collection;
private int position; // 커서 (for문의 i 변수 역할)
// 생성자로 순회할 컬렉션을 받아 필드에 참조 시킴
public ArrayIterator(int[] collection) {
this.collection = collection;
this.position = 0;
}
// 순회할 다음 요소가 있는지 true / false
@Override
public boolean hasNext() {
return position < collection.length;
}
// 다음 요소를 반환하고 커서를 증가시켜 다음 요소를 바라보도록 한다.
@Override
public Integer next() {
return collection[position++];
}
}
- Main코드
// 클라이언트 코드
public class Main {
public static void main(String[] args) {
// 1. 집합체 생성
int[] arr = {1, 2, 3, 4, 5};
ArrayCollection collection = new ArrayCollection(arr);
// 2. 집합체에서 이터레이터 객체 반환
Iterator<Integer> iterator = collection.iterator();
// 3. 이터레이터 내부 커서를 통해 순회
while (iterator.hasNext()) {
int element = iterator.next();
System.out.println(element);
}
}
}
이터레이터 패턴의 장점
- 일관된 이터레이터 인터페이스를 사용해 여러 형태의 컬렉션에 대해 동일한 순회 방법을 제공한다.
- 컬렉션의 내부 구조 및 순회 방식을 알지 않아도 된다.
- 집합체의 구현과 접근하는 처리 부분을 반복자 객체로 분리해 결합도를 줄 일 수 있다.
이터레이터 패턴의 단점
- 클래스가 늘어나고 복잡도가 증가한다.
- 구현 방법에 따라 캡슐화를 위배할 수 있다.
'Development > CS' 카테고리의 다른 글
PDU란? (Protocol Data Unit) (0) | 2023.07.10 |
---|---|
TCP/IP 4계층 모델 (0) | 2023.07.09 |
HTTPS란? (0) | 2023.07.07 |
프록시 서버란? (Proxy Server) (0) | 2023.07.02 |
프록시 패턴이란? (Proxy Pattern) (0) | 2023.07.02 |