VOOZH about

URL: https://www.javacodegeeks.com/2015/06/strategy-pattern-in-java-8.html

⇱ Strategy Pattern in Java 8 - Java Code Geeks


These are two examples on how to implement a Strategy pattern design using Java 8 functional style together with Cyclops pattern matching and Hamcrest libraries.

PrintDependingOnInput method is a strategy that will System.println some message based on the log passed.

AddPrefix is another strategy that will add a prefix to a message based on the message content.

package com.marco.patternmatching; 

 

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.core.AllOf.allOf;
import java.util.ArrayList;
import java.util.List;
import com.aol.cyclops.matcher.builders.Matching; 

 

public class FunctionalStrategy {

 public static void main(String[] args) {
 List<String> toTest = new ArrayList<>();
 toTest.add("INFO everything is fine");
 toTest.add("WARN something weird happened");
 toTest.add("ERROR NullPointerException");
 toTest.add("ERROR IOException");
 
 toTest.stream().forEach(FunctionalStrategy::printDependingOnInput);

 System.out.println("--------------------");

 List<String> messages = new ArrayList<>();
 messages.add("everything is fine");
 messages.add("something weird happened");
 messages.add("NullPointerException");
 messages.add("IOException");

 messages.stream().map(FunctionalStrategy::addPrefix).forEach(System.out::println);
 }

 
 public static void printDependingOnInput(String log) {

 Matching
 .when().isMatch(startsWith("INFO"))
 .thenConsume(System.out::println)
 .when().isMatch(startsWith("WARN"))
 .thenConsume(message -> System.out.println("Found one warning : " + message))
 .when().isMatch(allOf(startsWith("ERROR"), containsString("NullPointerException")))
 .thenConsume(message -> System.err.println(message))
 .when().isMatch(allOf(startsWith("ERROR"), containsString("IOException")))
 .thenConsume(message -> System.err.println(message + " Retrying a couple of times"))
 .match(log);

 }

 public static String addPrefix(String log) {

 return Matching
 .when().isMatch(allOf(not(containsString("Exception")), not(containsString("weird"))))
 .thenApply(message -> "INFO " + message)
 .when().isMatch(containsString("weird"))
 .thenApply(message -> "WARN " + message)
 .when().isMatch(containsString("Exception"))
 .thenApply(message -> "ERROR " + message)
 .match(log).get();

 }
}

Nice and clean ;)

Reference: Strategy Pattern in Java 8 from our JCG partner Marco Castigliego at the Remove duplication and fix bad names blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Marco Castigliego
Marco Castigliego
June 30th, 2015Last Updated: June 28th, 2015
3 131 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Arndt
10 years ago

Sorry, but reread the strategy pattern… This is not a Strategy Pattern but a simple if-else-based output.

Using the strategy Pattern, you can β€œplug-in” a new strategy without having to change any existing code…

Cheers, Arndt

0
Reply
Arndt
10 years ago
Reply to  Arndt

Well, ok it is a strategy pattern with addPrefix being the strategy (my fault) – just forget my post

0
Reply
Manoj
10 years ago

This is not nice example, totally waste of time.

2
Reply
Back to top button
Close
wpDiscuz