Skip to content

Commit b34cdbf

Browse files
authored
Merge pull request eugenp#6451 from kwoyke/BAEL-2755
BAEL-2755 - Introduction to the Null Object Pattern
2 parents 6658e92 + 23e3e6c commit b34cdbf

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.nullobject;
2+
3+
public class JmsRouter implements Router {
4+
5+
@Override
6+
public void route(Message msg) {
7+
System.out.println("Routing to a JMS queue. Msg: " + msg);
8+
}
9+
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.nullobject;
2+
3+
public class Message {
4+
5+
private String body;
6+
7+
private String priority;
8+
9+
public Message(String body, String priority) {
10+
this.body = body;
11+
this.priority = priority;
12+
}
13+
14+
public String getPriority() {
15+
return priority;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return "{body='" + body + '\'' +
21+
", priority='" + priority + '\'' +
22+
'}';
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.nullobject;
2+
3+
public class NullRouter implements Router {
4+
5+
@Override
6+
public void route(Message msg) {
7+
// do nothing
8+
}
9+
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.nullobject;
2+
3+
public interface Router {
4+
5+
void route(Message msg);
6+
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.nullobject;
2+
3+
public class RouterFactory {
4+
5+
public static Router getRouterForMessage(Message msg) {
6+
7+
if (msg.getPriority() == null) {
8+
return new NullRouter();
9+
}
10+
11+
switch (msg.getPriority()) {
12+
case "high":
13+
return new SmsRouter();
14+
15+
case "medium":
16+
return new JmsRouter();
17+
18+
default:
19+
return new NullRouter();
20+
}
21+
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.nullobject;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class RoutingHandler {
7+
8+
public void handle(Iterable<Message> messages){
9+
for (Message msg : messages) {
10+
Router router = RouterFactory.getRouterForMessage(msg);
11+
router.route(msg);
12+
}
13+
}
14+
15+
public static void main(String[] args) {
16+
Message highPriorityMsg = new Message("Alert!", "high");
17+
Message mediumPriorityMsg = new Message("Warning!", "medium");
18+
Message lowPriorityMsg = new Message("Take a look!", "low");
19+
Message nullPriorityMsg = new Message("Take a look!", null);
20+
21+
List<Message> messages = Arrays.asList(highPriorityMsg,
22+
mediumPriorityMsg,
23+
lowPriorityMsg,
24+
nullPriorityMsg);
25+
26+
RoutingHandler routingHandler = new RoutingHandler();
27+
routingHandler.handle(messages);
28+
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.nullobject;
2+
3+
public class SmsRouter implements Router {
4+
5+
@Override
6+
public void route(Message msg) {
7+
System.out.println("Routing to a SMS gateway. Msg: " + msg);
8+
}
9+
10+
}

0 commit comments

Comments
 (0)