Skip to content

Commit 1b6e8e0

Browse files
committed
提交抽象工厂
1 parent eabba68 commit 1b6e8e0

7 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.juststarnew.designpattern.abstractfactory.iterator;
2+
3+
import java.util.Collection;
4+
import java.util.Map;
5+
6+
/**
7+
* 描述:具体工厂
8+
*
9+
* @author zhangcai at 2020/4/30 17:18
10+
* @version 1.0.0
11+
*/
12+
public class CollectionFactory<T> implements IFactory<T>{
13+
@Override
14+
public Iterator<T> genCollectionIterator(Collection<T> collection) {
15+
return new CollectionIterator<>(collection);
16+
}
17+
18+
@Override
19+
public Iterator<T> genMapIterator(Map<T,Object> map) {
20+
return new MapIterator<>(map);
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.juststarnew.designpattern.abstractfactory.iterator;
2+
3+
import java.util.Collection;
4+
5+
/**
6+
* 描述:具体产品,使用了代理模式
7+
*
8+
* @author zhangcai at 2020/4/30 17:19
9+
* @version 1.0.0
10+
*/
11+
public class CollectionIterator<T> implements Iterator<T>{
12+
private java.util.Iterator<T> iterator;
13+
14+
public CollectionIterator(Collection<T> collection) {
15+
this.iterator = collection.iterator();
16+
}
17+
18+
@Override
19+
public boolean hasNext() {
20+
return iterator.hasNext();
21+
}
22+
23+
@Override
24+
public T next() {
25+
return iterator.next();
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.juststarnew.designpattern.abstractfactory.iterator;
2+
3+
import java.util.Collection;
4+
import java.util.Map;
5+
6+
/**
7+
* 描述:定义工厂,定义多个产品群。这里即返回遍历Collection
8+
* 又返回Map的iterator
9+
*
10+
* @author zhangcai at 2020/4/30 17:13
11+
* @version 1.0.0
12+
*/
13+
public interface IFactory<T> {
14+
public Iterator<T> genCollectionIterator(Collection<T> collection);
15+
16+
public Iterator<T> genMapIterator(Map<T,Object> map);
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.juststarnew.designpattern.abstractfactory.iterator;
2+
3+
/**
4+
* 描述:定义抽象产品,可以用来遍历Collection和Map
5+
*
6+
* @author zhangcai at 2020/4/30 17:15
7+
* @version 1.0.0
8+
*/
9+
public interface Iterator<T> {
10+
boolean hasNext();
11+
Object next();
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.juststarnew.designpattern.abstractfactory.iterator;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* 描述:具体产品
7+
*
8+
* @author zhangcai at 2020/4/30 17:21
9+
* @version 1.0.0
10+
*/
11+
public class MapIterator<T> implements Iterator<T> {
12+
private java.util.Iterator<Map.Entry<T, Object>> iterator;
13+
14+
public MapIterator(Map<T,Object> map) {
15+
this.iterator = map.entrySet().iterator();
16+
}
17+
18+
@Override
19+
public boolean hasNext() {
20+
return iterator.hasNext();
21+
}
22+
23+
@Override
24+
public Object next() {
25+
return iterator.next().getValue();
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.juststarnew.designpattern.abstractfactory.iterator;
2+
3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
8+
/**
9+
* 描述:
10+
*
11+
* @author zhangcai at 2020/4/30 17:25
12+
* @version 1.0.0
13+
*/
14+
public class TestUse {
15+
16+
public static void main(String[] args) {
17+
Collection<Integer> collection = new HashSet<>();
18+
Map<Integer,Object> map = new HashMap<>();
19+
for (int i = 0; i < 10; i++) {
20+
collection.add(i);
21+
map.put(i, i+"哈哈");
22+
}
23+
CollectionFactory<Integer> collectionFactory = new CollectionFactory<>();
24+
Iterator<Integer> integerIterator = collectionFactory.genCollectionIterator(collection);
25+
Iterator<Integer> integerIterator1 = collectionFactory.genMapIterator(map);
26+
while (integerIterator.hasNext()) {
27+
System.out.println(integerIterator.next());
28+
}
29+
System.out.println("----------------");
30+
while (integerIterator1.hasNext()) {
31+
System.out.println(integerIterator1.next());
32+
}
33+
34+
35+
}
36+
37+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* 描述:抽象工厂iterator举例
3+
*
4+
* @author zhangcai at 2020/4/30 17:13
5+
* @version 1.0.0
6+
*/
7+
package com.juststarnew.designpattern.abstractfactory.iterator;

0 commit comments

Comments
 (0)