-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNullObject.java
More file actions
57 lines (47 loc) · 1.25 KB
/
NullObject.java
File metadata and controls
57 lines (47 loc) · 1.25 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
interface Log {
// max # of elements in the log
int getRecordLimit();
// number of elements already in the log
int getRecordCount();
// expected to increment record count
void logInfo(String message);
}
class Account {
private Log log;
public Account(Log log) {
this.log = log;
}
public void someOperation()
throws Exception {
int c = log.getRecordCount();
log.logInfo("Performing an operation");
if (c + 1 != log.getRecordCount())
throw new Exception();
if (log.getRecordCount() >= log.getRecordLimit())
throw new Exception();
}
}
// NOTES: this class provides minimal implementation when we don't really this
// class to used
class NullLog implements Log {
int count = 0;
@Override
public int getRecordLimit() {
return count + 1;
}
@Override
public int getRecordCount() {
return count++;
}
@Override
public void logInfo(String message) {
}
}
class DemoNullObject {
public static void main(String[] args)
throws Exception {
NullLog log = new NullLog();
Account a = new Account(log);
a.someOperation();
}
}