【java設計模式】【行為模式Behavioral Pattern】迭代器模式Iterator Pattern

來源:互聯網
上載者:User

標籤:nbsp   迭代器模式   sys   main   public   ack   dex   rgs   turn   

 1 package com.tn.pattern; 2  3 public class Client { 4     public static void main(String[] args) { 5         Object[] objs={"飛","雪","連","天","射","白","鹿","笑","書","神"}; 6         Aggregate aggregate=new ConcreteAggregate(objs); 7          8         Iterator it=aggregate.iterator(); 9         it.first();10         it.next();11         System.out.println(it.isDone());12         it.currentItem();13         14         it.first();15         System.out.println("----------------------------------------");16         while(!it.isDone()){17             it.currentItem();18             it.next();19         }20     }21 }22 23 interface Iterator{24     void first();25     void next();26     boolean isDone();27     void currentItem();28 }29 30 interface Aggregate{31     Iterator iterator();32 }33 34 class ConcreteAggregate implements Aggregate{35     private Object[] datas=new Object[10];36     37     public ConcreteAggregate(Object[] objs){38         this.datas=objs;39     }40     41     public Iterator iterator(){42         return new ConcreteIterator();43     }44     45     class ConcreteIterator implements Iterator{46         private int index;47         @Override48         public void first() {49             index=0;50         }51         @Override52         public void next() {53             index++;54         }55         @Override56         public boolean isDone() {57             return index>=datas.length;58         }59         @Override60         public void currentItem() {61             if(index<datas.length)62                 System.out.println(datas[index]);63         }64     }65 }

 

【java設計模式】【行為模式Behavioral Pattern】迭代器模式Iterator Pattern

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.