/*µü´úÆ÷Ä£Ê½
*
* ±éÀú¼¯ºÏµÄÖ°Ôð·ÖÀë³öÀ´£»
*/

/**°×Ïä¾ÛºÏ+Íâµü´ú×Ó*/

public interface Iterator
{
    public Object first();

    public Object next();

	//µÃµ½µ±Ç°¶ÔÏó
    public Object currentItem();

	//ÊÇ·ñµ½ÁË½áÎ²
    public boolean isDone();
}

//ÕýÐòµü´úÆ÷
public class ConcreteIterator implements Iterator
{
    private int currentIndex = 0;

	//¶¨ÒåÒ»¸ö¾ßÌå¼¯ºÏ¶ÔÏó
    private Aggregate aggregate = null;

    public ConcreteIterator(Aggregate aggregate)
    {
        this.aggregate = aggregate;
    }

	//ÖØÐ´¸¸Àà·½·¨
    @Override
    public Object first()
    {
        currentIndex = 0;
        return vector.get(currentIndex);
    }

    @Override
    public Object next()
    {
		if(currentIndex < aggregate.count())
			currentIndex++;
        return vector.get(currentIndex);
    }

    @Override
    public Object currentItem()
    {
        return aggregate.getAt(currentIndex);
    }

    @Override
    public boolean isDone()
    {
		return (currentIndex >= aggregate.count());
    }
}

/*¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª*/

//°×Ïä¾ÛºÏÒªÇó¼¯ºÏÀàÏòÍâ½çÌá¹©·ÃÎÊ×Ô¼ºÄÚ²¿ÔªËØµÄ½Ó¿Ú
public interface Aggregat
{
    public Iterator createIterator();

	//»ñÈ¡¼¯ºÏÄÚ²¿ÔªËØ×ÜÊý
	public int count();

	//»ñÈ¡Ö¸¶¨Î»ÖÃÔªËØ
	public Object getAt(int index);
}

/*¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª*/

//¾ßÌåµÄ¼¯ºÏÀà
public class ConcreteAggregat implements Aggregat
{
    private Vector vector = null;

    public Vector getVector()
    {
        return vector;
    }

    public void setVector(final Vector vector)
    {
        this.vector = vector;
    }

    public ConcreteAggregat()
    {
        vector = new Vector();
        vector.add("item 1");
        vector.add("item 2");
    }

	//»ñÈ¡¼¯ºÏÄÚ²¿ÔªËØ×ÜÊý
	@Override
	public int count()
	{
		return vector.size();
	}

	//»ñÈ¡Ö¸¶¨Î»ÖÃÔªËØ
	@Override
	public Object getAt(int index)
	{
		if(0 <= index && index < vector.size())
			return vector[index];
		else
			return null;
	}

	//´´½¨Ò»¸ö¾ßÌåµü´úÆ÷¶ÔÏó£¬²¢°Ñ¸Ã¼¯ºÏ¶ÔÏó×ÔÉí½»¸ø¸Ãµü´úÆ÷
    @Override
    public Iterator createIterator()
    {
		//ÕâÀï¿ÉÒÔÊ¹ÓÃ¼òµ¥¹¤³§Ä£Ê½
        return new ConcreteIterator(this);
    }
}

/*¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª*/

public class Client
{
    public static void main(final String[] args)
    {
        Aggregat agg = new ConcreteAggregat();
        final Iterator iterator = agg.createIterator();

        System.out.println(iterator.first());

        while (!iterator.isDone())
        {
			//Item item = (Item)iterator.currentItem();
            System.out.println(iterator.next());
        }
    }
}


