/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter9;
import java.util.function.Consumer;
/**
* Sample observable object where the Observer design pattern is applied using
* functional-style design.
*
* See Section 9.5.
*/
public class ObservableDeck extends Deck {
private Consumer<Card> aDrawHandler;
public ObservableDeck() {
aDrawHandler = pDrawHandler;
}
@Override
public Card draw() {
Card card = super.draw();
aDrawHandler.accept(card);
return card;
}
}
an interface with a single non static non default method
an interface with a single non static non default method
Because the abstract observer is a , client code can instantiate it with a lambda expression of a method reference, instead of creating an entire new class (anonymous or not). The resulting code is very concise, for example:
ObservableDeck deck = new ObservableDeck(System.out::println);
Consumer
is expected
to operate via side-effects.
Because the abstract observer is a , client code can instantiate it with a lambda expression of a method reference, instead of creating an entire new class (anonymous or not). The resulting code is very concise, for example:
ObservableDeck deck = new ObservableDeck(System.out::println);
Consumer
is expected
to operate via side-effects.
This is a functional interface
whose functional method is accept(Object)
.
Consumer
is expected
to operate via side-effects.
Consumer
is expected
to operate via side-effects.
This is a functional interface
whose functional method is accept(Object)
.