-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNullObject.java
More file actions
57 lines (48 loc) · 1.05 KB
/
NullObject.java
File metadata and controls
57 lines (48 loc) · 1.05 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
abstract class AbstractCustomer {
String name;
abstract boolean ifNull();
}
class RealCustomer extends AbstractCustomer {
public RealCustomer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean ifNull() {
return false;
}
}
class NilCustomer extends AbstractCustomer {
public boolean isNull() {
return true;
}
public String getName() {
return "No name can be returned!";
}
}
class CustomerFactory {
Set<String> nameSet = new HashSet<String>();
public CustomerFactory(List<String> names) {
for (String name : names) {
nameSet.add(name);
}
}
public AbstractCustomer createCustomer(String name) {
if(nameSet.contains(name)) {
return new RealCustomer(name);
}
else {
return new NilCustomer();
}
}
}
class Test {
public static void main(String[] args) {
List<String> nameList = new ArrayList<String>();
nameList.add("Tom");
CustomerFactory cf = new CustomerFactory(nameList);
AbstractCustomer c1 = cf.createCustomer("Tom");
AbstractCustomer c2 = cf.createCustomer("John");
}
}