Java Reference
In-Depth Information
public class IsNumeric implements ValidationStrategy {
public boolean execute(String s){
return s.matches("\\d+");
}
}
You can then use these different validation strategies in your program:
Using lambda expressions
By now you should recognize that ValidationStrategy is a functional interface (in addition, it has
the same function descriptor as Predicate<String>). This means that instead of declaring new
classes to implement different strategies, you can pass lambda expressions directly, which are
more concise:
As you can see, lambda expressions remove the boilerplate code inherent to the strategy design
pattern. If you think about it, lambda expressions encapsulate a piece of code (or strategy),
which is what the strategy design pattern was created for, so we recommend that you use
lambda expressions instead for similar problems.
 
Search WWH ::




Custom Search