Skip to content

Commit 0ac38a4

Browse files
committed
增加了对遍历器模式的解析讲解
1 parent 86eb0ac commit 0ac38a4

File tree

14 files changed

+272
-0
lines changed

14 files changed

+272
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
public class Boss {
4+
public static void main(String[] args) {
5+
CompanyDevelop develop = new CompanyDevelop();
6+
check(develop.iterator());
7+
8+
CompanyTester tester = new CompanyTester();
9+
check(tester.iterator());
10+
11+
}
12+
13+
14+
private static void check(Iterator iterator) {
15+
while (iterator.hasNext()) {
16+
System.out.println(iterator.next().toString());
17+
}
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
//容器类接口,主要是定义一个可以返回容器迭代器的方法
4+
public interface Company {
5+
Iterator<Employee> iterator();
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
7+
//开发对应的容器类
8+
public class CompanyDevelop implements Company {
9+
10+
List<Employee> list = new ArrayList<>();
11+
12+
public CompanyDevelop() {
13+
list.add(new Employee("小明", 21, "男", "安卓开发"));
14+
list.add(new Employee("晨晨", 18, "女", "H5"));
15+
list.add(new Employee("朔风", 19, "男", "JS"));
16+
list.add(new Employee("糖心", 25, "男", "技术管理"));
17+
}
18+
19+
public List<Employee> getEmployees() {
20+
return list;
21+
}
22+
23+
@Override
24+
public Iterator<Employee> iterator() {
25+
return new DevelopIterator(list);
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
4+
public class CompanyTester implements Company {
5+
private Employee[] array = new Employee[3];
6+
7+
public CompanyTester() {
8+
array[0] = new Employee("辉哥", 12, "男", "初级测试");
9+
array[1] = new Employee("小红", 25, "女", "高级测试");
10+
array[0] = new Employee("辉总", 35, "男", "测试管理");
11+
}
12+
13+
@Override
14+
public Iterator<Employee> iterator() {
15+
return new TestIterator(array);
16+
}
17+
18+
public Employee[] getEmployees() {
19+
return array;
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
4+
import java.util.List;
5+
6+
public class DevelopIterator implements Iterator {
7+
private List<Employee> list;
8+
private int position;
9+
10+
public DevelopIterator(List<Employee> list) {
11+
this.list = list;
12+
}
13+
14+
@Override
15+
public boolean hasNext() {
16+
return !(position > list.size() - 1 || list.get(position) == null);
17+
}
18+
19+
@Override
20+
public Object next() {
21+
Employee employee = list.get(position);
22+
position++;
23+
return employee;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
public class Employee {
4+
private String name;
5+
private int age;
6+
private String sex;
7+
private String position;
8+
9+
public Employee(String name, int age, String sex, String position) {
10+
this.name = name;
11+
this.age = age;
12+
this.sex = sex;
13+
this.position = position;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Employee{" +
19+
"name='" + name + '\'' +
20+
", age=" + age +
21+
", sex='" + sex + '\'' +
22+
", position='" + position + '\'' +
23+
'}';
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
public interface Iterator<T> {
4+
boolean hasNext();
5+
6+
T next();
7+
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.company.iteratorPattern.demo;
2+
3+
public class TestIterator implements Iterator {
4+
5+
private Employee[] array;
6+
private int position;
7+
8+
public TestIterator(Employee[] array) {
9+
this.array = array;
10+
}
11+
12+
@Override
13+
public boolean hasNext() {
14+
return !(position > array.length || array[position] == null);
15+
}
16+
17+
@Override
18+
public Object next() {
19+
Employee employee = array[position];
20+
position++;
21+
return employee;
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.company.iteratorPattern.model;
2+
3+
4+
//容器接口
5+
public interface Aggregate<T> {
6+
7+
void add(T obj);
8+
9+
void remove(T obj);
10+
11+
Iterator<T> iterator();//获取迭代器对象
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.company.iteratorPattern.model;
2+
3+
4+
//客户类
5+
public class Client {
6+
public static void main(String[] args) {
7+
Aggregate<String> a = new ConcreteAggregate<>();
8+
a.add("Aige");
9+
a.add("Studio\n");
10+
a.add("SM");
11+
a.add("brother");
12+
13+
Iterator<String> i = a.iterator();
14+
while (i.hasNext()) {
15+
System.out.println(i.next());
16+
}
17+
18+
}
19+
}

0 commit comments

Comments
 (0)