zoukankan      html  css  js  c++  java
  • 【设计模式】-行为型-8-迭代器模式

    主要角色

    1. 抽象聚合(Aggregate)角色:定义存储、添加、删除聚合对象以及创建迭代器对象的接口。
    2. 具体聚合(ConcreteAggregate)角色:实现抽象聚合类,返回一个具体迭代器的实例。
    3. 抽象迭代器(Iterator)角色:定义访问和遍历聚合元素的接口,通常包含 hasNext()、first()、next() 等方法。
    4. 具体迭代器(Concretelterator)角色:实现抽象迭代器接口中所定义的方法,完成对聚合对象的遍历,记录遍历的当前位置。

    代码示例

    package iterator;
    import java.util.*;
    public class IteratorPattern
    {
        public static void main(String[] args)
        {
            Aggregate ag=new ConcreteAggregate(); 
            ag.add("中山大学"); 
            ag.add("华南理工"); 
            ag.add("韶关学院");
            System.out.print("聚合的内容有:");
            Iterator it=ag.getIterator(); 
            while(it.hasNext())
            { 
                Object ob=it.next(); 
                System.out.print(ob.toString()+"	"); 
            }
            Object ob=it.first();
            System.out.println("
    First:"+ob.toString());
        }
    }
    //抽象聚合
    interface Aggregate
    { 
        public void add(Object obj); 
        public void remove(Object obj); 
        public Iterator getIterator(); 
    }
    //具体聚合
    class ConcreteAggregate implements Aggregate
    { 
        private List<Object> list=new ArrayList<Object>(); 
        public void add(Object obj)
        { 
            list.add(obj); 
        }
        public void remove(Object obj)
        { 
            list.remove(obj); 
        }
        public Iterator getIterator()
        { 
            return(new ConcreteIterator(list)); 
        }     
    }
    //抽象迭代器
    interface Iterator
    {
        Object first();
        Object next();
        boolean hasNext();
    }
    //具体迭代器
    class ConcreteIterator implements Iterator
    { 
        private List<Object> list=null; 
        private int index=-1; 
        public ConcreteIterator(List<Object> list)
        { 
            this.list=list; 
        } 
        public boolean hasNext()
        { 
            if(index<list.size()-1)
            { 
                return true;
            }
            else
            {
                return false;
            }
        }
        public Object first()
        {
            index=0;
            Object obj=list.get(index);;
            return obj;
        }
        public Object next()
        { 
            Object obj=null; 
            if(this.hasNext())
            { 
                obj=list.get(++index); 
            } 
            return obj; 
        }   
    }
    
  • 相关阅读:
    GoWeb-Gin 文件上载
    Node.JS + Mysql数据库
    Node.JS 项目打包 JXCore
    Express web框架 upload file
    hdoj 4430 Yukari's Birthday
    hdoj 4282 A very hard mathematic problem
    hdoj 3750 Guess Game
    hdoj 2199 Can you solve this equation?
    uva 11384 Help is needed for Dexter
    ios中fixed元素在滚动布局中的延时渲染问题
  • 原文地址:https://www.cnblogs.com/tuofan/p/12377501.html
Copyright © 2011-2022 走看看