Home > CS > 2024 > πŸ’Ύ [CS] μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄(Iterator pattern)

πŸ’Ύ [CS] μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄(Iterator pattern)
CS

πŸ’Ύ [CS] μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄(Iterator pattern).

1️⃣ μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄(Iterator pattern).

  • μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄(Iterator pattern)은 μ΄ν„°λ ˆμ΄ν„°(Iterator)λ₯Ό μ‚¬μš©ν•˜μ—¬ μ»¬λ ‰μ…˜(collection)의 μš”μ†Œλ“€μ— μ ‘κ·Όν•˜λŠ” λ””μžμΈ νŒ¨ν„΄μž…λ‹ˆλ‹€.
    • 이λ₯Ό 톡해 μˆœνšŒν•  수 μžˆλŠ” μ—¬λŸ¬ 가지 μžλ£Œν˜•μ˜ κ΅¬μ‘°μ™€λŠ” 상관없이 μ΄ν„°λ ˆμ΄ν„°λΌλŠ” ν•˜λ‚˜μ˜ μΈν„°νŽ˜μ΄μŠ€λ‘œ μˆœνšŒκ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€.

2️⃣ μ΄ν„°λ ˆμ΄ν„°(Iterator)

  • μ΄ν„°λ ˆμ΄ν„°(Iterator)λŠ” ν”„λ‘œκ·Έλž˜λ°μ—μ„œ μ»¬λ ‰μ…˜(예: λ² μ—΄, 리슀트, μ…‹ λ“±) λ‚΄μ˜ μš”μ†Œλ“€μ„ 순차적으둜 μ ‘κ·Όν•  수 있게 ν•΄μ£ΌλŠ” 객체λ₯Ό λ§ν•©λ‹ˆλ‹€.
  • μ΄ν„°λ ˆμ΄ν„°λŠ” 주둜 루프λ₯Ό 톡해 μ»¬λ ‰μ…˜μ˜ μš”μ†Œλ“€μ„ ν•˜λ‚˜μ”© 가져와 μ²˜λ¦¬ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€.

μ΄ν„°λ ˆμ΄ν„°μ˜ 핡심 κΈ°λŠ₯ 두 가지.

  • 1. next() : μ΄ν„°λ ˆμ΄ν„°μ˜ λ‹€μŒ μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. λ‹€μŒ μš”μ†Œκ°€ 없을 경우 μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚€κ±°λ‚˜ νŠΉμ • 값을 λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 2. hasNext() : λ‹€μŒμ— κ°€μ Έμ˜¬ μš”μ†Œκ°€ μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό ν™•μΈν•©λ‹ˆλ‹€. λ‹€μŒ μš”μ†Œκ°€ 있으면 trueλ₯Ό, μ—†μœΌλ©΄ falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ.

  • μžλ°”μ—μ„œμ˜ μ΄ν„°λ ˆμ΄ν„° μ‚¬μš© μ˜ˆμ‹œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        
        Iterator<String> iterator = list.iterator();
        
        while (iterator.hasNext()) {
            String element = iterator.next();
            System.out.println(element);
        }
    }
}
  • μœ„μ˜ μ½”λ“œμ—μ„œ list.iterator()λ₯Ό ν†΅ν•΄μ„œ 리슀트의 μ΄ν„°λ ˆμ΄ν„°λ₯Ό μ–»κ³ , while 루프λ₯Ό 톡해 hasNext()둜 λ‹€μŒ μš”μ†Œκ°€ μžˆλŠ”μ§€ ν™•μΈν•˜λ©΄μ„œ next()λ₯Ό μ‚¬μš©ν•΄ 각 μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© 좜λ ₯ν•©λ‹ˆλ‹€.

  • μ΄ν„°λ ˆμ΄ν„°λŠ” μ»¬λ ‰μ…˜μ˜ μš”μ†Œλ₯Ό 순차적으둜 탐색할 수 μžˆλŠ” ν‘œμ€€ν™”λœ 방법을 μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ—, μ»¬λ ‰μ…˜μ΄ 무엇이든 상관 없이 λ™μΌν•œ λ°©μ‹μœΌλ‘œ μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    • λ˜ν•œ, μ΄ν„°λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜λ©΄ μ»¬λ ‰μ…˜ λ‚΄λΆ€ κ΅¬ν˜„μ— 직접 μ ‘κ·Όν•˜μ§€ μ•Šκ³ λ„ μš”μ†Œλ“€μ„ 탐색할 수 있기 λ•Œλ¬Έμ— μ»¬λ ‰μ…˜μ˜ μ•ˆμ „ν•œ μ ‘κ·Όκ³Ό μˆ˜μ •μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.

3️⃣ μžλ°”μ—μ„œμ˜ μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄.

  • μžλ°”μ—μ„œ μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄(Iterator Pattern)은 μ»¬λ ‰μ…˜ λ‚΄λΆ€ ꡬ쑰λ₯Ό λ…ΈμΆœν•˜μ§€ μ•Šκ³ λ„ κ·Έ μš”μ†Œλ“€μ— 순차적으둜 μ ‘κ·Όν•  수 μžˆλ„λ‘ ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄μž…λ‹ˆλ‹€.
  • 이 νŒ¨ν„΄μ€ java.util.Iterator μΈν„°νŽ˜μ΄μŠ€λ₯Ό 톡해 μžλ°”μ˜ ν‘œμ€€ λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ 널리 μ‚¬μš©λ©λ‹ˆλ‹€.

1. κΈ°λ³Έ κ°œλ….

  • μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄μ€ μ»¬λ ‰μ…˜μ˜ λ‚΄λΆ€ ꡬ쑰λ₯Ό μˆ¨κΈ°λ©΄μ„œ μš”μ†Œλ“€μ— μ ‘κ·Όν•  수 있게 ν•΄μ€λ‹ˆλ‹€.
  • 이 νŒ¨ν„΄μ€ λ°˜λ³΅μžκ°€ μ»¬λ ‰μ…˜ μš”μ†Œλ“€μ„ 순차적으둜 탐색할 수 μžˆλŠ” λ©”μ„œλ“œλ“€μ„ μ •μ˜ν•©λ‹ˆλ‹€.

2. Iterator μΈν„°νŽ˜μ΄μŠ€.

  • μžλ°”μ˜ Iterator μΈν„°νŽ˜μ΄μŠ€λŠ” μ„Έ 가지 μ£Όμš” λ©”μ„œλ“œλ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€.
    • boolean hasNext() : λ‹€μŒμ— μ½μ–΄μ˜¬ μš”μ†Œκ°€ μžˆλŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€. 있으면 true, μ—†μœΌλ©΄ falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
    • E next() : λ‹€μŒ μš”μ†Œλ₯Ό λ°˜ν™˜ν•˜κ³ , μ΄ν„°λ ˆμ΄ν„°λ₯Ό λ‹€μŒ μœ„μΉ˜λ‘œ μ΄λ™μ‹œν‚΅λ‹ˆλ‹€.
    • void remove() : μ΄ν„°λ ˆμ΄ν„°κ°€ λ§ˆμ§€λ§‰μœΌλ‘œ λ°˜ν™˜ν•œ μš”μ†Œλ₯Ό μ»¬λ ‰μ…˜μ—μ„œ μ œκ±°ν•©λ‹ˆλ‹€.(이 λ©”μ„œλ“œλŠ” μ„ νƒμ μœΌλ‘œ κ΅¬ν˜„λ  수 μžˆμŠ΅λ‹ˆλ‹€.)

3. μ‚¬μš© μ˜ˆμ‹œ.

  • λ¨Όμ €, μ»¬λ ‰μ…˜ 클래슀(예: ArrayList, HashSet λ“±)의 μ΄ν„°λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ μš”μ†Œλ“€μ„ 반볡 μ²˜λ¦¬ν•˜λŠ” μ˜ˆμ‹œλ₯Ό λ³΄κ² μŠ΅λ‹ˆλ‹€.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorPatternExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        Iterator<String> iterator = names.iterator();

        while (iterator.hasNext()) {
            String name = iterator.next();
            System.out.println(name);
        }
    }
}

4. μ»€μŠ€ν…€ μ΄ν„°λ ˆμ΄ν„° κ΅¬ν˜„.

  • μžμ‹ λ§Œμ˜ μ»¬λ ‰μ…˜ ν΄λž˜μŠ€μ™€ 그에 λŒ€ν•œ μ΄ν„°λ ˆμ΄ν„°λ₯Ό 직접 κ΅¬ν˜„ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
    • 예λ₯Ό λ“€μ–΄, κ°„λ‹¨ν•œ Book μ»¬λ ‰μ…˜κ³Ό κ·Έ μ΄ν„°λ ˆμ΄ν„°λ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
import java.util.Iterator;
import java.util.NoSuchElementException;

class Book {
    private String title;
    
    public Book(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
}

class BookCollection implements Iterable<Book> {
    private Book[] books;
    private int index = 0;
    
    public BookCollection(int size) {
        books = new Book[size];
    }
    
    public void addBook(Book book) {
        if (index < books.length) {
            books[index++] = books;
        }
    }
    
    @Override
    public Iterator<Book> iterator() {
        return new BookIterator();
    }
    
    private class BookIterator implements Iterator<Book> {
        private int currentIndex = 0;
        
        @Override
        public boolean hasNext() {
            return currentIndex < book.length && books[currentIndex] != null;
        }
        
        @Override
        public Book next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return books[currentIntex++];
        }
        
        @Override
        public void remove() {
            throw new UnsupportedOperationException("Remove not supported");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        BookCollection bookCollection = new BookCollection(3);
        bookCollection.addBook(new Book("The Catcher in th Rye"));
        bookCollection.addBook(new Book("To Kill a Mockingbird"));
        bookCollection.addBook(new Book("1984"));
        
        for (Book book : bookCollection) {
            System.out.println(book.getTitle());
        }
    }
}

5. λ™μž‘ 원리.

  • BookCollection ν΄λž˜μŠ€λŠ” Iterable<Book>을 κ΅¬ν˜„ν•˜μ—¬ μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄μ„ λ”°λ¦…λ‹ˆλ‹€.
  • iterator() λ©”μ„œλ“œλŠ” BookIterator λΌλŠ” λ‚΄λΆ€ 클래슀λ₯Ό λ°˜ν™˜ν•˜λ©°, 이 ν΄λž˜μŠ€λŠ” Iterator<Book>을 κ΅¬ν˜„ν•©λ‹ˆλ‹€.
  • BookIteratorλŠ” hasNext(), next(), 그리고 remove() λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜μ—¬ μ»¬λ ‰μ…˜μ˜ μš”μ†Œλ“€μ„ 반볡 μ²˜λ¦¬ν•©λ‹ˆλ‹€.

6. 마무리.

  • μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄μ€ 이처럼 λ‚΄λΆ€ ꡬ쑰λ₯Ό 감좔고, ν‘œμ€€ν™”λœ λ°©λ²•μœΌλ‘œ μ»¬λ ‰μ…˜μ˜ μš”μ†Œλ“€μ„ 탐색할 수 있게 ν•΄μ€λ‹ˆλ‹€.
    • μ΄λŠ” μ½”λ“œμ˜ μž¬μ‚¬μš©μ„±κ³Ό μœ μ§€ λ³΄μˆ˜μ„±μ„ λ†’μ΄λŠ” 데 κΈ°μ—¬ν•©λ‹ˆλ‹€.