Java Reference
In-Depth Information
f.registerObserver((String tweet) -> {
if(tweet != null && tweet.contains("money")){
System.out.println("Breaking news in NY! " + tweet);
}
});
f.registerObserver((String tweet) -> {
if(tweet != null && tweet.contains("queen")){
System.out.println("Yet another news in London... " + tweet);
}
});
Should you use lambda expressions all the time? The answer is no! In the example we described,
lambda expressions work great because the behavior to execute is simple, so they're helpful to
remove boilerplate code. But the observers may be more complex: they could have state, define
several methods, and the like. In those situations, you should stick with classes.
8.2.4. Chain of responsibility
The chain of responsibility pattern is a common solution to create a chain of processing objects
(such as a chain of operations). One processing object may do some work and pass the result to
another object, which then also does some work and passes it on to yet another processing
object, and so on.
Generally, this pattern is implemented by defining an abstract class representing a processing
object that defines a field to keep track of a successor. Once it has finished its work, the
processing object hands over its work to its successor. In code it looks like this:
public abstract class ProcessingObject<T> {
protected ProcessingObject<T> successor;
public void setSuccessor(ProcessingObject<T> successor){
this.successor = successor;
}
public T handle(T input){
T r = handleWork(input);
if(successor != null){
 
Search WWH ::




Custom Search