- Fluent Decorator
- In, Out, In, Out, Shake It All About
- The Lonely Override
- Monad
- Functional Reactive?
你可以在以下找到中文說明:
http://www.codedata.com.tw/social-coding/java8-patterns/
Justin LinTechnology / Community Evangelist at Free lancer
5. #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
11. #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
12. #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
13. #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
19. #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
20. #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
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()
21
22. #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
25. #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
26. #JCConf
• The flatMap operation
4 1
1 2 3 4 5 6
flatMap(List<Integer> to Stream<Integer>)
26
27. #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
28. #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
34. #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
35. #JCConf
Observer
• In MVC, Model-View could be implemented
by the Observer pattern.
• Maybe, we can ...
(10, 15, 25)
(20, 25, 35)
35
36. #JCConf
songs
• When things become complex ...
names
albums 1900s
soloartists songs
songs songs
songs
lengths
nationalities
birthdays
36
38. #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
39. #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
40. #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
42. #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