#JCConf
Java 8 Patterns
#JCConf
Agenda
• Fluent Decorator
• In, Out, In, Out, Shake It All About
• The Lonely Override
• Monad
• Functional Reactive?
2
#JCConf
Fluent Decorator
3
#JCConf
Comparator
• Building a Comparator before Java 8
• Java 8
Wait! you’re cheating!!
It‘s not the reverse order.
4
#JCConf
• Use Comparator.reverseOrder
• Use Comparator.comparing
• High order function
Anonymous inner classes were designed to make
it easier for Java programmers to pass
around code as data.
5
#JCConf
• Order nulls before non-null elements.
• Decorator Pattern
6
#JCConf
• Can you figure out this?
Refactor it to
nullsFirst(byStringLength())?
7
#JCConf
• This would be better!
• last name, and then first name, zip code …
8
#JCConf
• Refactor the Decorator pattern itself
9
#JCConf
In, Out, In, Out,
Shake It All About《Java 8 Lambdas》
10
#JCConf
Logger
• Invoke isLoggable only to check whether
calling a method on the Logger
• it uses information from the rental, but does
not use information from the customer
• the method is on the wrong object
11
#JCConf
Logger
• Invoke isLoggable only to check whether
calling a method on the Logger
• it uses information from Logger, but only to
push a value into it
• the block is on the wrong object
12
#JCConf
Map
• it uses information from the Map, but only to
push a value into it.
• the block is on the wrong object.
• computeIfPresent、compute、merge …
13
#JCConf
The Lonely Override《Java 8 Lambdas》
14
#JCConf
ThreadLocal
• Subclass solely to override a single method
• What you really want is providing a block of
code to the method!
15
#JCConf
• This wasn't an antipattern before Java 8—it
was the idiomatic way of writing this code
• Source code of withInitial
16
#JCConf
Monad Patterns
17
#JCConf
Optional
• Null sucks, right?
• Optional is trendy now! Optional<Order>
WET
18
#JCConf
• Behavioral WET(Write Everything Twice)
– You create Optional<T>
– invoke isPresent of Optional<T>
– if true, get and map T to Optional<U>
– if false, return Optional.empty()
• You uses information from Optional<T>,
but only to get a Optional<U> from it
• In, Out, In, Out, Shake It All About
19
#JCConf
• Let's flatMap repeatedly.
• Using method reference is much clearer.
• Monad? Why is it important?
– Enable me to talk about monad right now!!
20
#JCConf
• Seriously, a monad is a pattern
– You create M<T>
– do actions on M<T>
– get and map T value(s) to M<U>
– do actions on M<U> or else
• Optional Monad
– You create Optional<T>
– invoke isPresent of Optional<T>
– if true, get and map T to Optional<U>
– if false, return Optional.empty()
21
#JCConf
• Seriously, a monad is a pattern
– You create M<T>
– do actions on M<T>
– get and map T value(s) to M<U>
– do actions on M<U> or else
• Optional Monad
– You create Optional<T>
– invoke isPresent of Optional<T>
– if true, get and map T to Optional<U>
– if false, return Optional.empty()
Hide the boilerplate of
computational context
Hide the boilerplate of
"if non-null“ logic
22
#JCConf 23
#JCConf
Stream
• for loop hell ... XD
• Call back hell ... XD
24
#JCConf
• Stream Monad
– You create Stream<T>
– forEach T element
– map T to Stream<U>
• A simpler example
Hide the boilerplate of
nested loops
25
#JCConf
• The flatMap operation
4 1
1 2 3 4 5 6
flatMap(List<Integer> to Stream<Integer>)
26
#JCConf
CompletableFuture
• A monad is a pattern
– You create M<T>
– do actions on M<T>
– get and map T value(s) to M<U>
– do actions on M<U> or else
Hide the boilerplate of
computational context
27
#JCConf
• Lambda rocks!
• Lambda sucks!!
– You create Future<T>
– wait until T is ready (in another thread)
– use T and the supplied code to create Future<U>
Hide the boilerplate of thread
logic. You‘ve done that.
Future<String>
28
#JCConf
• CompletableFuture Monad
flatMap
CompletableFuture<String>
CompletableFuture<String>
29
#JCConf
• Composable Future
solo
birthdays
30
#JCConf
Functional Reactive?
31
#JCConf
Functional Reactive Programming
• Reactive
• Data flow
• Propagation of change
• Observer
• Functional
• Asynchronous
• …
WTF?
32
#JCConf
Reactive Programming
• A programming paradigm oriented around
data flows and the propagation of change
33
#JCConf
• If Java supports reactive programming
directly, it should ...
• Of course, it's impossible in the language
level for Java.
• Model-View-Controller architecture can
allow changes in the underlying model to
automatically be reflected in the view
(10, 15, 25)
(20, 25, 35)
34
#JCConf
Observer
• In MVC, Model-View could be implemented
by the Observer pattern.
• Maybe, we can ...
(10, 15, 25)
(20, 25, 35)
35
#JCConf
songs
• When things become complex ...
names
albums 1900s
soloartists songs
songs songs
songs
lengths
nationalities
birthdays
36
#JCConf
• RxJava Observable
• Look up artist information
• filter、flatMap? map? Stream?
Observable<Artist>
37
#JCConf
Functional Reactive Programming
• A programming paradigm for reactive
programming using the building blocks of
functional programming
• filter、map、reduce、flatMap are
patterns from functional programming
38
#JCConf
• Stream builds up computational workflows
over in-memory collections
• Observable composes and sequences
asynchronous and event-based systems.
Instead of pulling data out, it gets pushed in
• CompletableFuture is to a single value
• Observable is a sequence of values
39
#JCConf
Asynchronous
• "that executes in whole seconds is probably
doing something wrong…" - Brendan Eich
• it's 100 milliseconds from research
conducted by Robert Miller
40
#JCConf
• Make a mountain out of a molehill … XD
41
#JCConf
Summary
• Design patterns evolve more and more
exquisitely since Java 8.
• Lambda furthers advanced refactoring.
• As the language evolves, so do the idioms
that you use when programming.
• A monad is a pattern.
• Functional reactive programming is a mixture
of multi patterns and paradigms.
42
#JCConf
References
• http://books.gotop.com.tw/v_ACL042200
• http://www.slideshare.net/JustinSDK/jdk8-
functional-api
• http://www.slideshare.net/mariofusco/monadic-java
• http://shop.oreilly.com/product/0636920030713.do
• http://en.wikipedia.org/wiki/Reactive_programming
• https://github.com/ReactiveX/RxJava
• https://speakerdeck.com/benjchristensen/functional
-reactive-programming-with-rxjava-javaone-2013
• http://shop.oreilly.com/product/9780596802806.do
43
#JCConf
Thanks!!
44

java8-patterns