/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java",
* 2nd edition by Martin P. Robillard.
*
* Copyright (C) 2022 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*
*******************************************************************************/
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;
}
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)
.