-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathMixins.java
72 lines (57 loc) · 1.54 KB
/
Mixins.java
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
66
67
68
69
70
71
72
/**
* 组合模式
* 在 TaggableImpl 中实现打标签的逻辑
* Post 类和 TaggableImpl 类都实现 Taggable 接口
* Post 类中创建一个 TaggableImpl 实例并在实现 Taggable 时将相应方法调用委托过去。
*/
import java.util.List;
import java.util.ArrayList;
interface Entity {
public int getId();
public int getKind();
}
interface Taggable {
public void addTag(int tagId);
public List<Integer> getTags();
}
class TaggableImpl implements Taggable {
private Entity target;
public TaggableImpl(Entity target) {
this.target = target;
}
public void addTag(int tagId) {
int id = target.getId();
int kind = target.getKind();
System.out.println("insert into ... values "
+ id + ", "
+ kind + ", "
+ tagId + ")");
}
public ArrayList<Integer> getTags() {
// query from database
return new ArrayList<Integer>();
}
}
class Post implements Entity, Taggable {
public final static int KIND = 1001;
private Taggable taggable;
private int id;
private String title;
public Post(int id, String title) {
this.id = id;
this.title = title;
this.taggable = new TaggableImpl(this);
}
public int getId() {
return id;
}
public int getKind() {
return KIND;
}
public void addTag(int tagId) {
taggable.addTag(tagId); // delegate
}
public ArrayList<Integer> getTags() {
return taggable.getTags(); // delegate
}
}