Chan Chen Coding...

Twelve: Iterator Design Pattern

Iterator (aka Cursor) Overview

One object can traverse all of the elements of another object.




DvdListIterator.java - the Iterator Interface

package behavioral.interator.pattern;

public interface DvdListIterator {

    public void first();

    public void next();

    public boolean isDone();

    public String currentItem();
}

DvdList.java - the Concrete Aggregate (with a Concrete Iterator inner class)

package behavioral.interator.pattern;

public class DvdList {

    private String[]    titles;

    private int                titleCount;

    private int                arraySize;

    public DvdList() {
        titles = new String[3];
        titleCount = 0;
        arraySize = 3;
    }

    public int count() {
        return titleCount;
    }

    public void append(String titleIn) {
        if (titleCount >= arraySize) {
            String[] tempArray = new String[arraySize];
            for (int i = 0; i < arraySize; i++) {
                tempArray[i] = titles[i];
            }
            titles = null;
            arraySize = arraySize + 3;
            titles = new String[arraySize];
            for (int i = 0; i < (arraySize - 3); i++) {
                titles[i] = tempArray[i];
            }
        }
        titles[titleCount++] = titleIn;
    }

    public void delete(String titleIn) {
        boolean found = false;
        for (int i = 0; i < (titleCount - 1); i++) {
            if (found == false) {
                if (titles[i].equals(titleIn)) {
                    found = true;
                    titles[i] = titles[i + 1];
                }
            } else {
                if (i < (titleCount - 1)) {
                    titles[i] = titles[i + 1];
                } else {
                    titles[i] = null;
                }
            }
        }

        if (found == true) {
            --titleCount;
        }
    }

    public DvdListIterator createIterator() {
        return new InnerIterator();
    }

    private class InnerIterator implements DvdListIterator {
        private int    currentPosition    = 0;

        private InnerIterator() {
        }

        public void first() {
            currentPosition = 0;
        }

        public void next() {
            if (currentPosition < (titleCount)) {
                ++currentPosition;
            }
        }

        public boolean isDone() {
            if (currentPosition >= (titleCount)) {
                return true;
            } else {
                return false;
            }
        }

        public String currentItem() {
            return titles[currentPosition];
        }
    }
}


Try the Design Patterns Video Tutorial from SourceMaking

TestDvdIterator.java - testing the iterator

package behavioral.interator.pattern;

public class TestDvdIterator {
    public static void main(String[] args) {
        DvdList fiveShakespeareMovies = new DvdList();
        fiveShakespeareMovies.append("10 Things I Hate About You");
        fiveShakespeareMovies.append("Shakespeare In Love");
        fiveShakespeareMovies.append("O (2001)");
        fiveShakespeareMovies.append("American Pie 2");
        fiveShakespeareMovies.append("Scotland, PA.");
        fiveShakespeareMovies.append("Hamlet (2000)");

        DvdListIterator fiveShakespeareIterator = fiveShakespeareMovies.createIterator();
        while (!fiveShakespeareIterator.isDone()) {
            System.out.println(fiveShakespeareIterator.currentItem());
            fiveShakespeareIterator.next();
        }

        fiveShakespeareMovies.delete("American Pie 2");

        System.out.println(" ");
        fiveShakespeareIterator.first();
        while (!fiveShakespeareIterator.isDone()) {
            System.out.println(fiveShakespeareIterator.currentItem());
            fiveShakespeareIterator.next();
        }
    }
}

Test Results

10 Things I Hate About You
Shakespeare In Love
O (2001)
American Pie 2
Scotland, PA.
Hamlet (2000)
 
10 Things I Hate About You
Shakespeare In Love
O (2001)
Scotland, PA.
Hamlet (2000)

UML

UML for Iterator

References



-----------------------------------------------------
Silence, the way to avoid many problems;
Smile, the way to solve many problems;

posted on 2012-11-06 10:49 Chan Chen 阅读(258) 评论(0)  编辑  收藏 所属分类: Design Pattern


只有注册用户登录后才能发表评论。


网站导航: