-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFill2.java
More file actions
74 lines (55 loc) · 1.42 KB
/
Copy pathFill2.java
File metadata and controls
74 lines (55 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package chapter15.seventeen;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import chapter15.three.Coffee;
import chapter15.three.Generator;
import chapter15.three.Latte;
import chapter15.three.Mocha;
public class Fill2 {
public static <T> void fill(Addable<T> addable,Class<? extends T> classToken,int size){
for (int i = 0; i < size; i++) {
try {
addable.add(classToken.newInstance());
} catch (Exception e) {
System.out.println(e);
}
}
}
public static <T> void fill(Addable<T> addable,Generator<T> generator,int size){
for (int i = 0; i <size; i++) {
addable.add(generator.next());
}
}
}
/**
* 适配一个基类型Addable,需要使用composition(作文,作曲; 创作; 构图,布置; 妥协,和解;)
* @author admin
*
* @param <T>
*/
class AddableCollectionAdapter<T> implements Addable<T>{
private Collection<T> c;
public AddableCollectionAdapter(Collection<T> c) {
// TODO Auto-generated constructor stub
this.c=c;
}
public void add(T t) {
// TODO Auto-generated method stub
c.add(t);
}
}
class Adapter{
public static <T> Addable<T> collectionAdapter(Collection<T> c){
return new AddableCollectionAdapter<T>(c);
}
}
class AddableSimpleQueue<T> extends AddableCollectionAdapter<T> implements Addable<T>{
public AddableSimpleQueue(Collection<T> c) {
super(c);
// TODO Auto-generated constructor stub
}
public void add(T item){
super.add(item);
}
}