-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNullRobot.java
More file actions
68 lines (55 loc) · 1.73 KB
/
NullRobot.java
File metadata and controls
68 lines (55 loc) · 1.73 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
66
67
68
package typeinfo;
import java.util.*;
import java.lang.reflect.*;
import net.mindview.util.*;
/**
* RUN:
* javac typeinfo/NullRobot.java && java typeinfo.NullRobot
* OUTPUT:
* Name: SnowBee
* Model: SnowBot Series 11
* SnowBee can remove snow
* SnowBee remove snow
* SnowBee can crush ice
* SnowBee crush ice
* SnowBee can clean roof
* SnowBee clean roof
* [Null Robot]
* Name: SnowRemovalRobot NullRobot
* Model: SnowRemovalRobot NullRobot
*/
public class NullRobot {
public static Robot newNullRobot(Class<? extends Robot> type) {
return (Robot)Proxy.newProxyInstance(
NullRobot.class.getClassLoader()
, new Class[] { Null.class, Robot.class }
, new NullRobotProxyHandler(type)
);
}
public static void main(String[] args) {
Robot[] bots = {
new SnowRemovalRobot("SnowBee")
, newNullRobot(SnowRemovalRobot.class)
};
for (Robot bot : bots) {
Robot.Test.test(bot);
}
}
}
class NullRobotProxyHandler implements InvocationHandler {
private String nullName;
private Robot proxied = new NRobot();
NullRobotProxyHandler(Class<? extends Robot> type) {
nullName = type.getSimpleName() + " NullRobot";
}
private class NRobot implements Null, Robot {
public String name() { return nullName; }
public String model() { return nullName; }
public List<Operation> operations() {
return Collections.emptyList();
}
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(proxied, args);
}
}