Professional Documents
Culture Documents
with lambdas
Raoul-Gabriel Urma
Richard Warburton
Outline
3. Practical
4. Design Patterns
String twoLines =
processFile((BufferedReader br) ->
br.readLine() + br.readLine());
String mostCommonWord =
processFile((BufferedReader br) ->
findMostCommonWord(br));
Step 2: Use a functional interface
@FunctionalInterface
public interface BufferedReaderProcessor {
String process(BufferedReader b) throws IOException;
}
}
Step 3: Execute a behavior!
String oneLine =
processFile((BufferedReader br) -> br.readLine());
String twoLines =
processFile((BufferedReader br) -> br.readLine() +
br.readLine());
Refactoring Practical
● See:
○ Pass: com.java_8_training.problems.design.CacheInitializerTest
○ Code: com.java_8_training.problems.design.NumberCache
Deferred execution: Logging
Logging
if (logger.isLoggable(Log.FINER)){
logger.finer("Problem: " + generateDiagnostic());
}
● The state of the logger (what level it supports) is exposed in the client
code through the method isLoggable.
● Why should you have to query the state of the logger object every time
before you can log a message? It just clutters your code.
Logging: internal checking
defer construction of
logging message
Refactoring Practical
● Inspired by Junit 5
● See:
○ Test: com.java_8_training.problems.design.MiniAssertTest
○ Code: com.java_8_training.problems.design.MiniAssert
Design patterns
Lots of design patterns
● Command
● Strategy
● Observer
● Template
● Factory
● Builder
● Processing Pipeline
Command Pattern
● Using an object to encapsulate information about a
method call.
void save();
void open();
void close();
}
public interface Action {
void perform();
}
public class Save implements Action {
this.editor = editor;
editor.save();
}
public class Macro {
actions.add(action);
actions.forEach(Action::perform);
}
Macro macro = new Macro();
macro.record(new Save(editor));
macro.record(new Close(editor));
macro.run();
The Command Object is a Function
Macro macro = new Macro();
macro.record(() -> editor.save());
macro.record(() -> editor.close());
macro.run();
The Command Object is a Function
Macro macro = new Macro();
macro.record(editor::save);
macro.record(editor::close);
macro.run();
Strategy: in a nutshell
boolean b2 = lowerCaseValidator.validate(numberField);
Refactoring practical
Look at:
com.java_8_training.problems.design.Compressor
interface Subject {
void registerObserver(Observer o);
void notifyObservers(String tweet);
}
f.notifyObservers(
"The queen said she loves French wine!");
Observer: with lambdas
f.registerObserver(
(String tweet) -> {
if(tweet.contains("trump")) {
System.out.println("Breaking news in NY! " + tweet);
}
});
f.registerObserver(
(String tweet) -> {
if(tweet.contains("queen")) {
System.out.println("Yet another news in London... " +
tweet);
}
});
Template: in a nutshell
loanMoney(c, amount);
}
}
loanMoney.accept(customer, amount);
}
}
Add some interfaces
@FunctionalInterface
interface CreditCheck {
boolean test(Customer customer, int amount);
}
@FunctionalInterface
interface Transfer {
void accept(Customer customer, int amount);
}
Template: with lambdas and names
loanMoney.accept(customer, amount);
}
}
Factory Method: in a nutshell
static {
map.put(LOAN, Loan::new);
map.put(STOCK, Stock::new);
map.put(BOND, Bond::new);
}
Message msg
= Message.builder()
.sender(new Sender("hello@world.com"))
.title("Hello")
.content("World")
.build();
Problems
● Incomplete Initialisation
● Boilerplate
Builder: add some interfaces
interface SenderBuilder {
TitleBuilder sender(Sender sender);
}
interface TitleBuilder {
ContentBuilder title(String title);
}
interface ContentBuilder {
Message content(String content);
}
Builder: with lambdas
public class Message {
private Sender sender;
private String title;
private String content;
Message msg
= Message.builder()
.sender(new Sender("hello@world.com"))
.title("Hello")
.content("World");
// No .build() call
Processing Pipeline: in a nutshell
UnaryOperator<String> spellCheckerProcessing =
(String text) -> text.replaceAll("labda", "lambda");
safety
structure
● Existing Design Patterns are useful, but they evolve with the language
com.java_8_training.examples.design.factory_class.FactoryC
lassExampleMain