返回顶部

一缕半夏微光

温柔半两,从容一生

导航

软件设计18|迭代器模式

 (1)Java版本

效果如下:

类图如下:

工程目录如下:

代码如下:

Client.java

 1 package iterator;
 2 
 3 /**
 4  * 客户端
 5  * @author Administrator
 6  *
 7  */
 8 public class Client {
 9     public static void main(String[] args) {
10         Java_Iterator iterator=new Java_Iterator();
11         iterator.display();
12     }
13 }

Java_Iterator.java

  1 package iterator;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Collections;
  5 import java.util.Iterator;
  6 import java.util.List;
  7 
  8 /**
  9  * Java内置迭代器
 10  * @author Administrator
 11  *
 12  */
 13 public class Java_Iterator {
 14     List<Student_Large_To_Small> student=null;
 15     List<Student_Small_To_Large> student1=null;
 16     public Java_Iterator() {
 17         //从大到小
 18         Student_Large_To_Small[] stu=new Student_Large_To_Small[44];
 19         student=new ArrayList<Student_Large_To_Small>();
 20         stu[0]=new Student_Large_To_Small("e",20194144,19);
 21         stu[1]=new Student_Large_To_Small("a",20194136,18);
 22         stu[2]=new Student_Large_To_Small("b",20194138,17);
 23         stu[3]=new Student_Large_To_Small("c",20194140,19);
 24         stu[4]=new Student_Large_To_Small("d",20194142,19);
 25         stu[5]=new Student_Large_To_Small("au",20194134,19);
 26         stu[6]=new Student_Large_To_Small("f",20194132,19);
 27         stu[7]=new Student_Large_To_Small("g",20194130,19);
 28         stu[8]=new Student_Large_To_Small("h",20194128,19);
 29         stu[9]=new Student_Large_To_Small("i",20194126,20);
 30         stu[10]=new Student_Large_To_Small("j",20194124,20);
 31         stu[11]=new Student_Large_To_Small("k",20194122,19);
 32         stu[12]=new Student_Large_To_Small("l",20194120,21);
 33         stu[13]=new Student_Large_To_Small("m",20194118,21);
 34         stu[14]=new Student_Large_To_Small("n",20194116,21);
 35         stu[15]=new Student_Large_To_Small("o",20194114,21);
 36         stu[16]=new Student_Large_To_Small("p",20194112,19);
 37         stu[17]=new Student_Large_To_Small("q",20194110,19);
 38         stu[18]=new Student_Large_To_Small("r",20194108,19);
 39         stu[19]=new Student_Large_To_Small("s",20194106,20);
 40         stu[20]=new Student_Large_To_Small("t",20194104,19);
 41         stu[21]=new Student_Large_To_Small("u",20194102,19);
 42         stu[22]=new Student_Large_To_Small("v",20194101,18);
 43         stu[23]=new Student_Large_To_Small("w",20194103,19);
 44         stu[24]=new Student_Large_To_Small("x",20194105,22);
 45         stu[25]=new Student_Large_To_Small("y",20194107,19);
 46         stu[26]=new Student_Large_To_Small("z",20194109,21);
 47         stu[27]=new Student_Large_To_Small("hello",20194129,19);
 48         stu[28]=new Student_Large_To_Small("world",20194127,19);
 49         stu[29]=new Student_Large_To_Small("my",20194125,20);
 50         stu[30]=new Student_Large_To_Small("input",20194123,19);
 51         stu[31]=new Student_Large_To_Small("output",20194121,18);
 52         stu[32]=new Student_Large_To_Small("iterator",20194111,19);
 53         stu[33]=new Student_Large_To_Small("ab",20194113,19);
 54         stu[34]=new Student_Large_To_Small("cd",20194115,19);
 55         stu[35]=new Student_Large_To_Small("ed",20194117,19);
 56         stu[36]=new Student_Large_To_Small("rg",20194119,19);
 57         stu[37]=new Student_Large_To_Small("yh",20194131,19);
 58         stu[38]=new Student_Large_To_Small("ij",20194135,19);
 59         stu[39]=new Student_Large_To_Small("sx",20194133,19);
 60         stu[40]=new Student_Large_To_Small("dc",20194137,19);
 61         stu[41]=new Student_Large_To_Small("fv",20194139,19);
 62         stu[42]=new Student_Large_To_Small("gb",20194141,19);
 63         stu[43]=new Student_Large_To_Small("exit",20194143,19);
 64         
 65         for(int i=0;i<44;i++) {
 66             student.add(stu[i]);
 67         }
 68         
 69         //从小到大
 70         Student_Small_To_Large[] stu1=new Student_Small_To_Large[44];
 71         student1=new ArrayList<Student_Small_To_Large>();
 72         stu1[0]=new Student_Small_To_Large("e",20194144,19);
 73         stu1[1]=new Student_Small_To_Large("a",20194136,18);
 74         stu1[2]=new Student_Small_To_Large("b",20194138,17);
 75         stu1[3]=new Student_Small_To_Large("c",20194140,19);
 76         stu1[4]=new Student_Small_To_Large("d",20194142,19);
 77         stu1[5]=new Student_Small_To_Large("au",20194134,19);
 78         stu1[6]=new Student_Small_To_Large("f",20194132,19);
 79         stu1[7]=new Student_Small_To_Large("g",20194130,19);
 80         stu1[8]=new Student_Small_To_Large("h",20194128,19);
 81         stu1[9]=new Student_Small_To_Large("i",20194126,20);
 82         stu1[10]=new Student_Small_To_Large("j",20194124,20);
 83         stu1[11]=new Student_Small_To_Large("k",20194122,19);
 84         stu1[12]=new Student_Small_To_Large("l",20194120,21);
 85         stu1[13]=new Student_Small_To_Large("m",20194118,21);
 86         stu1[14]=new Student_Small_To_Large("n",20194116,21);
 87         stu1[15]=new Student_Small_To_Large("o",20194114,21);
 88         stu1[16]=new Student_Small_To_Large("p",20194112,19);
 89         stu1[17]=new Student_Small_To_Large("q",20194110,19);
 90         stu1[18]=new Student_Small_To_Large("r",20194108,19);
 91         stu1[19]=new Student_Small_To_Large("s",20194106,20);
 92         stu1[20]=new Student_Small_To_Large("t",20194104,19);
 93         stu1[21]=new Student_Small_To_Large("u",20194102,19);
 94         stu1[22]=new Student_Small_To_Large("v",20194101,18);
 95         stu1[23]=new Student_Small_To_Large("w",20194103,19);
 96         stu1[24]=new Student_Small_To_Large("x",20194105,22);
 97         stu1[25]=new Student_Small_To_Large("y",20194107,19);
 98         stu1[26]=new Student_Small_To_Large("z",20194109,21);
 99         stu1[27]=new Student_Small_To_Large("hello",20194129,19);
100         stu1[28]=new Student_Small_To_Large("world",20194127,19);
101         stu1[29]=new Student_Small_To_Large("my",20194125,20);
102         stu1[30]=new Student_Small_To_Large("input",20194123,19);
103         stu1[31]=new Student_Small_To_Large("output",20194121,18);
104         stu1[32]=new Student_Small_To_Large("iterator",20194111,19);
105         stu1[33]=new Student_Small_To_Large("ab",20194113,19);
106         stu1[34]=new Student_Small_To_Large("cd",20194115,19);
107         stu1[35]=new Student_Small_To_Large("ed",20194117,19);
108         stu1[36]=new Student_Small_To_Large("rg",20194119,19);
109         stu1[37]=new Student_Small_To_Large("yh",20194131,19);
110         stu1[38]=new Student_Small_To_Large("ij",20194135,19);
111         stu1[39]=new Student_Small_To_Large("sx",20194133,19);
112         stu1[40]=new Student_Small_To_Large("dc",20194137,19);
113         stu1[41]=new Student_Small_To_Large("fv",20194139,19);
114         stu1[42]=new Student_Small_To_Large("gb",20194141,19);
115         stu1[43]=new Student_Small_To_Large("exit",20194143,19);
116         
117         for(int i=0;i<44;i++) {
118             student1.add(stu1[i]);
119         }
120     }
121     
122     public void display() {
123         System.out.println("*****************************");
124         Iterator<Student_Large_To_Small> st=student.iterator();
125         System.out.println("遍历获得的原始数据:");
126         while(st.hasNext()) {
127             Student_Large_To_Small stud=st.next();
128             System.out.println("学号:"+stud.getNum()+",姓名:"+stud.getName()+",年龄:"+stud.getAge());
129         }
130         System.out.println("*****************************");
131         Collections.sort(student);
132         Iterator<Student_Large_To_Small> large=student.iterator();
133         System.out.println("按学号从大到小排序:");
134         while(large.hasNext()) {
135             Student_Large_To_Small stud=large.next();
136             System.out.println("学号:"+stud.getNum()+",姓名:"+stud.getName()+",年龄:"+stud.getAge());
137         }
138         System.out.println("*****************************");
139         Collections.sort(student1);
140         Iterator<Student_Small_To_Large> small=student1.iterator();
141         System.out.println("按学号从小到大排序:");
142         while(small.hasNext()) {
143             Student_Small_To_Large stud=small.next();
144             System.out.println("学号:"+stud.getNum()+",姓名:"+stud.getName()+",年龄:"+stud.getAge());
145         }
146     }
147 }

Student_Large_To_Small.java

 1 package iterator;
 2 
 3 /**
 4  * 学生类
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public class Student_Large_To_Small implements Comparable<Student_Large_To_Small> {
10     private String name;// 姓名
11     private int num;// 学号
12     private int age;// 年龄
13 
14     public Student_Large_To_Small(String name, int num, int age) {
15         super();
16         this.name = name;
17         this.num = num;
18         this.age = age;
19     }
20 
21     public int compareTo(Student_Large_To_Small stu) {
22         //从大到小
23         if (this.num > stu.num) {
24             return -1;
25         } else if (this.num < stu.num) {
26             return 1;
27         }
28         return 0;
29     }
30 
31     public String getName() {
32         return name;
33     }
34 
35     public void setName(String name) {
36         this.name = name;
37     }
38 
39     public int getNum() {
40         return num;
41     }
42 
43     public void setNum(int num) {
44         this.num = num;
45     }
46 
47     public int getAge() {
48         return age;
49     }
50 
51     public void setAge(int age) {
52         this.age = age;
53     }
54 
55 }

Student_Small_To_Large.java

 1 package iterator;
 2 
 3 /**
 4  * 从小到大
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public class Student_Small_To_Large implements Comparable<Student_Small_To_Large> {
10 
11     private String name;// 姓名
12     private int num;// 学号
13     private int age;// 年龄
14 
15     public Student_Small_To_Large(String name, int num, int age) {
16         super();
17         this.name = name;
18         this.num = num;
19         this.age = age;
20     }
21 
22     @Override
23     public int compareTo(Student_Small_To_Large stu) {
24         // TODO Auto-generated method stub
25         // 从小到大
26         if (this.num < stu.num) {
27             return -1;
28         } else if (this.num > stu.num) {
29             return 1;
30         }
31         return 0;
32     }
33 
34     public String getName() {
35         return name;
36     }
37 
38     public void setName(String name) {
39         this.name = name;
40     }
41 
42     public int getNum() {
43         return num;
44     }
45 
46     public void setNum(int num) {
47         this.num = num;
48     }
49 
50     public int getAge() {
51         return age;
52     }
53 
54     public void setAge(int age) {
55         this.age = age;
56     }
57 
58 }

参考链接:设计模式练习(14)——迭代器模式

(2)C++版本

效果如下:

代码如下:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 class Node {
  5 private:
  6     int val1;
  7     double val2;
  8     Node* next;
  9     Node* pre;
 10 public:
 11     Node() {
 12         next = NULL;
 13         pre = NULL;
 14     };
 15     Node(int x1, double x2) {
 16         next = NULL;
 17         pre = NULL;
 18         val1 = x1;
 19         val2 = x2;
 20     }
 21     void SetNext(Node* n) {
 22         next = n;
 23     };
 24     Node* GetNext() {
 25         return next;
 26     }
 27     void SetPre(Node* n) {
 28         pre = n;
 29     }
 30     Node* GetPre() {
 31         return pre;
 32     }
 33     int getVal1() {
 34         return val1;
 35     }
 36     double getVal2() {
 37         return val2;
 38     }
 39 };
 40 
 41 template<class Node>
 42 class Iterator {
 43 public:
 44     Iterator() {};
 45     virtual ~Iterator() {};
 46     virtual Node* first() = 0;
 47     virtual Node* last() = 0;
 48     virtual Node* next() = 0;
 49     virtual Node* pre() = 0;
 50     virtual Node* nowPos() = 0;
 51     virtual int len() = 0;
 52     virtual void SetDirect(bool s) = 0;
 53 };
 54 
 55 template<class Node>
 56 class Aggregate {
 57 public:
 58     Aggregate() {};
 59     virtual ~Aggregate() {};
 60     virtual Iterator<Node>* createIterator() = 0;
 61     virtual Node* GetFirst() = 0;
 62     virtual Node* GetLast() = 0;
 63     virtual int GetSum() = 0;
 64     virtual void Insert(int a, double b) = 0;
 65     virtual void Init() = 0;
 66 };
 67 
 68 template<class Node>
 69 class ConcreteIterator : public Iterator<Node> {
 70 public:
 71     ConcreteIterator(Aggregate<Node>* a) :aggr(a) {
 72         cur = aggr->GetFirst()->GetNext();
 73     };
 74     ~ConcreteIterator() {};
 75     Node* first() {
 76         Node* p = new Node();
 77         p = aggr->GetFirst()->GetNext();
 78         return p;
 79     }
 80 
 81     Node* last() {
 82         Node* p = new Node();
 83         p = aggr->GetLast();
 84         return p;
 85     }
 86 
 87     Node* pre() {
 88         Node* p = cur->GetPre();
 89         cur = cur->GetPre();
 90         return p;
 91     }
 92 
 93     Node* nowPos() {
 94         return cur;
 95     }
 96 
 97     Node* next() {
 98         Node* p = cur->GetNext();
 99         cur = cur->GetNext();
100         return p;
101     }
102 
103     void SetDirect(bool s) {
104         if (s) {
105             cur = aggr->GetFirst()->GetNext();
106         }
107         else {
108             cur = aggr->GetLast();
109         }
110     }
111 
112     int len() {
113         return aggr->GetSum();
114     }
115 
116 private:
117     Aggregate<Node>* aggr;
118     Node* cur;
119 };
120 
121 template<class Node>
122 class LinkList : public Aggregate<Node> {
123 private:
124     Node* first;
125     Node* last;
126     int sum;
127 public:
128     Iterator<Node>* createIterator() {
129         return new ConcreteIterator<Node>(this);
130     }
131     LinkList() {
132         first = NULL;
133         last = NULL;
134         sum = 0;
135     }
136     Node* GetFirst() {
137         return first;
138     }
139     Node* GetLast() {
140         return last;
141     }
142     int GetSum() {
143         return sum;
144     }
145     void Init() {
146         first = new Node();
147     }
148     void Insert(int a, double b) {
149         Node* p = first;
150         Node* s = new Node(a, b);
151         for (int i = 0; i < sum; i++) {
152             p = p->GetNext();
153         }
154         s->SetNext(NULL);
155         s->SetPre(p);
156         p->SetNext(s);
157         last = s;
158         sum++;
159     }
160     ~LinkList() {}
161 };
162 
163 int main() {
164     Aggregate<Node>* aggr = new LinkList<Node>();
165     aggr->Init();
166     aggr->Insert(2017, 19);
167     aggr->Insert(2018, 18);
168     aggr->Insert(2019, 17);
169     aggr->Insert(2020, 20);
170     aggr->Insert(2021, 21);
171 
172     Iterator<Node>* it = aggr->createIterator();
173 
174     cout << "正序:" << endl;
175     it->SetDirect(true);
176     while (it->nowPos() != NULL) {
177         cout << it->nowPos()->getVal1() << "  " << it->nowPos()->getVal2() << endl;
178         it->next();
179     }
180     cout << "倒序:" << endl;
181     it->SetDirect(false);
182     do {
183         cout << it->nowPos()->getVal1() << "  " << it->nowPos()->getVal2() << endl;
184         it->pre();
185     } while (it->nowPos() != it->first()->GetPre());
186     return 0;
187 }

参考链接:迭代器模式C++实现

posted on 2021-11-22 21:31  一缕半夏微光  阅读(29)  评论(0编辑  收藏  举报