feat: 迭代器模式
This commit is contained in:
parent
b90a5bef13
commit
9d0baa8d71
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
|
@ -1098,7 +1098,7 @@ public class Adapter3 {
|
|||
|
||||
---
|
||||
|
||||
## 组合模式---结构型
|
||||
## 组合模式---结构型1
|
||||
|
||||
---
|
||||
|
||||
|
@ -1106,7 +1106,7 @@ public class Adapter3 {
|
|||
|
||||
---
|
||||
|
||||
## 代理模式---结构型
|
||||
## 代理模式---结构型1
|
||||
|
||||
---
|
||||
|
||||
|
@ -1604,7 +1604,7 @@ public class BakeDemo {
|
|||
|
||||
---
|
||||
|
||||
## 状态模式---行为型
|
||||
## 状态模式---行为型1
|
||||
|
||||
---
|
||||
|
||||
|
@ -1817,6 +1817,97 @@ public class Memo {
|
|||
|
||||
## 迭代器模式---行为型
|
||||
|
||||
**定义:**提供一种方法顺序访问一个聚合对象中的哥哥元素,而不是暴露其内部的表示。
|
||||
|
||||
![image-20250203173419155](./images/设计模式-v2/image-20250203173419155.png)
|
||||
|
||||
需要角色:抽象迭代器、具体迭代器、抽象聚合类、具体聚合类。
|
||||
|
||||
### 简单示例
|
||||
|
||||
#### 示例代码
|
||||
|
||||
```java
|
||||
public class Iterator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ListContainer listContainer = new ListContainer();
|
||||
listContainer.add("序号1");
|
||||
listContainer.add("序号2");
|
||||
listContainer.add("序号3");
|
||||
listContainer.add("序号4");
|
||||
|
||||
MyIterator iterator = listContainer.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
// 迭代器接口
|
||||
interface MyIterator {
|
||||
|
||||
boolean hasNext();
|
||||
|
||||
Object next();
|
||||
}
|
||||
|
||||
// 定义容器
|
||||
interface MyContainer {
|
||||
void add(Object o);
|
||||
|
||||
void remove(Object o);
|
||||
|
||||
MyIterator iterator();
|
||||
}
|
||||
|
||||
// 列表操作容器
|
||||
static class ListContainer implements MyContainer {
|
||||
private final List<Object> list = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void add(Object o) {
|
||||
list.add(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Object o) {
|
||||
list.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyIterator iterator() {
|
||||
return new MyIteratorImpl();
|
||||
}
|
||||
|
||||
class MyIteratorImpl implements MyIterator {
|
||||
|
||||
private int index = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index < list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object next() {
|
||||
Object object = list.get(index);
|
||||
index++;
|
||||
return object;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行结果:
|
||||
|
||||
```java
|
||||
序号1
|
||||
序号2
|
||||
序号3
|
||||
序号4
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 访问者模式---行为型
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
package cn.bunny.pattern13;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Iterator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ListContainer listContainer = new ListContainer();
|
||||
listContainer.add("序号1");
|
||||
listContainer.add("序号2");
|
||||
listContainer.add("序号3");
|
||||
listContainer.add("序号4");
|
||||
|
||||
MyIterator iterator = listContainer.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
// 迭代器接口
|
||||
interface MyIterator {
|
||||
|
||||
boolean hasNext();
|
||||
|
||||
Object next();
|
||||
}
|
||||
|
||||
// 定义容器
|
||||
interface MyContainer {
|
||||
void add(Object o);
|
||||
|
||||
void remove(Object o);
|
||||
|
||||
MyIterator iterator();
|
||||
}
|
||||
|
||||
// 列表操作容器
|
||||
static class ListContainer implements MyContainer {
|
||||
private final List<Object> list = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void add(Object o) {
|
||||
list.add(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Object o) {
|
||||
list.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyIterator iterator() {
|
||||
return new MyIteratorImpl();
|
||||
}
|
||||
|
||||
class MyIteratorImpl implements MyIterator {
|
||||
|
||||
private int index = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index < list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object next() {
|
||||
Object object = list.get(index);
|
||||
index++;
|
||||
return object;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue