-
Notifications
You must be signed in to change notification settings - Fork 0
/
java-patt.7.1.java
150 lines (143 loc) · 4.31 KB
/
java-patt.7.1.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
abstract class FruitD { }
class Peach extends FruitD {
public boolean equals(Object o) {
return o instanceof Peach;
}
}
class Apple extends FruitD {
public boolean equals(Object o) {
return o instanceof Apple;
}
}
class Pear extends FruitD {
public boolean equals(Object o) {
return o instanceof Pear;
}
}
class Lemon extends FruitD {
public boolean equals(Object o) {
return o instanceof Lemon;
}
}
class Fig extends FruitD {
public boolean equals(Object o) {
return o instanceof Fig;
}
}
interface TreeVisitorI {
Object forBud();
Object forFlat(FruitD f, TreeD t);
Object forSplit(TreeD l, TreeD r);
}
abstract class TreeD {
abstract Object accept(TreeVisitorI ask);
abstract public String toString();
}
class Bud extends TreeD {
Object accept(TreeVisitorI ask) {
return ask.forBud();
}
public String toString() {
return "Bud";
}
}
class Flat extends TreeD {
FruitD f;
TreeD t;
Flat (FruitD _f, TreeD _t) {
f = _f;
t = _t;
}
// ----------------------------------------
Object accept(TreeVisitorI ask) {
return ask.forFlat(f, t);
}
public String toString() {
return String.format( "(Flat %s %s)", f, t);
}
}
class Split extends TreeD {
TreeD l;
TreeD r;
Split (TreeD _l, TreeD _r) {
l = _l;
r = _r;
}
// ----------------------------------------
Object accept(TreeVisitorI ask) {
return ask.forSplit(l, r);
}
public String toString() {
return String.format( "(Split %s %s)", l, r);
}
}
class iHeightV implements TreeVisitorI {
public Integer forBud() {
return 0;
}
public Integer forFlat(FruitD f, TreeD t) {
return 1+(Integer) t.accept(this);
}
public Integer forSplit(TreeD l, TreeD r) {
return 1+Math.max((Integer) l.accept(this),
(Integer) r.accept(this));
}
}
class Test {
static void p(Object... objs) {
for(Object o:objs)
System.out.printf("%s ", o);
}
static private void test1() {
TreeD x = new Split ( new Split (new Bud(),
new Split(new Bud(),
new Bud())),
new Split (new Bud(),
new Split(new Bud(),
new Bud())));
p(x.accept(new iHeightV()), "\n");
}
static private void test2() {
TreeD x = new Split ( new Split (new Bud(),
new Split(new Bud(),
new Bud())),
new Split (new Bud(),
new Flat(new Fig(),
new Bud())));
p(x.accept(new iHeightV()), "\n");
}
static private void test3() {
TreeD x = new Split (new Bud(),
new Flat(new Fig(),
new Bud()));
TreeD x1 = new Flat(new Fig(),
new Bud());
TreeD x2 = new Bud();
p(x.accept(new iHeightV()), "\n");
p(x1.accept(new iHeightV()), "\n");
p(x2.accept(new iHeightV()), "\n");
}
static private void test4() {
TreeD x = new Split ( new Split (new Bud(),
new Split(new Bud(),
new Flat(new Fig(),
new Flat(new Fig(),
new Bud())))),
new Split (new Bud(),
new Split(new Bud(),
new Flat(new Fig(),
new Bud()))));
p(x.accept(new iHeightV()), "\n");
}
static public void main(String[] args) {
p("\n", "---test1", "\n");
test1();
p("\n", "---test2", "\n");
test2();
p("\n", "---test3", "\n");
test3();
p("\n", "---test4", "\n");
test4();
p ("\n", "--", "\n");
}
}