BAEL-2755: Added design-patterns-2 module
This commit is contained in:
25
patterns/design-patterns-2/pom.xml
Normal file
25
patterns/design-patterns-2/pom.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>design-patterns-2</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>design-patterns-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class JmsRouter implements Router {
|
||||
|
||||
@Override
|
||||
public void route(Message msg) {
|
||||
System.out.println("Routing to a JMS queue. Msg: " + msg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class Message {
|
||||
|
||||
private String body;
|
||||
|
||||
private String priority;
|
||||
|
||||
public Message(String body, String priority) {
|
||||
this.body = body;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public String getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{body='" + body + '\'' +
|
||||
", priority='" + priority + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class NullRouter implements Router {
|
||||
|
||||
@Override
|
||||
public void route(Message msg) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public interface Router {
|
||||
|
||||
void route(Message msg);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class RouterFactory {
|
||||
|
||||
public static Router getRouterForMessage(Message msg) {
|
||||
|
||||
if (msg.getPriority() == null) {
|
||||
return new NullRouter();
|
||||
}
|
||||
|
||||
switch (msg.getPriority()) {
|
||||
case "high":
|
||||
return new SmsRouter();
|
||||
|
||||
case "medium":
|
||||
return new JmsRouter();
|
||||
|
||||
default:
|
||||
return new NullRouter();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class RoutingHandler {
|
||||
|
||||
public void handle(Iterable<Message> messages){
|
||||
for (Message msg : messages) {
|
||||
Router router = RouterFactory.getRouterForMessage(msg);
|
||||
router.route(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Message highPriorityMsg = new Message("Alert!", "high");
|
||||
Message mediumPriorityMsg = new Message("Warning!", "medium");
|
||||
Message lowPriorityMsg = new Message("Take a look!", "low");
|
||||
Message nullPriorityMsg = new Message("Take a look!", null);
|
||||
|
||||
List<Message> messages = Arrays.asList(highPriorityMsg,
|
||||
mediumPriorityMsg,
|
||||
lowPriorityMsg,
|
||||
nullPriorityMsg);
|
||||
|
||||
RoutingHandler routingHandler = new RoutingHandler();
|
||||
routingHandler.handle(messages);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class SmsRouter implements Router {
|
||||
|
||||
@Override
|
||||
public void route(Message msg) {
|
||||
System.out.println("Routing to a SMS gateway. Msg: " + msg);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user