【java设计模式】【行为模式Behavioral Pattern】迭代器模式Iterator Pattern
生活随笔
收集整理的這篇文章主要介紹了
【java设计模式】【行为模式Behavioral Pattern】迭代器模式Iterator Pattern
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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 @Override
48 public void first() {
49 index=0;
50 }
51 @Override
52 public void next() {
53 index++;
54 }
55 @Override
56 public boolean isDone() {
57 return index>=datas.length;
58 }
59 @Override
60 public void currentItem() {
61 if(index<datas.length)
62 System.out.println(datas[index]);
63 }
64 }
65 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/xiongjiawei/p/6846128.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【java设计模式】【行为模式Behavioral Pattern】迭代器模式Iterator Pattern的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件实施工程师的经验之谈(适合新手,老鸟
- 下一篇: Win10自带无线投屏功能(含无安装失败