java8-patterns
DESCRIPTION
- 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/TRANSCRIPT
![Page 1: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/1.jpg)
#JCConf
Java 8 Patterns
![Page 2: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/2.jpg)
#JCConf
Agenda
• Fluent Decorator
• In, Out, In, Out, Shake It All About
• The Lonely Override
• Monad
• Functional Reactive?
2
![Page 3: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/3.jpg)
#JCConf
Fluent Decorator
3
![Page 4: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/4.jpg)
#JCConf
Comparator
• Building a Comparator before Java 8
• Java 8
Wait! you’re cheating!! It‘s not the reverse order.
4
![Page 5: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/5.jpg)
#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
![Page 6: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/6.jpg)
#JCConf
• Order nulls before non-null elements.
• Decorator Pattern
6
![Page 7: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/7.jpg)
#JCConf
• Can you figure out this?
Refactor it to nullsFirst(byStringLength())?
7
![Page 8: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/8.jpg)
#JCConf
• This would be better!
• last name, and then first name, zip code …
8
![Page 9: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/9.jpg)
#JCConf
• Refactor the Decorator pattern itself
9
![Page 10: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/10.jpg)
#JCConf
In, Out, In, Out, Shake It All About
《Java 8 Lambdas》
10
![Page 11: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/11.jpg)
#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
![Page 12: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/12.jpg)
#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
![Page 13: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/13.jpg)
#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
![Page 14: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/14.jpg)
#JCConf
The Lonely Override 《Java 8 Lambdas》
14
![Page 15: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/15.jpg)
#JCConf
ThreadLocal
• Subclass solely to override a single method
• What you really want is providing a block of code to the method!
15
![Page 16: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/16.jpg)
#JCConf
• This wasn't an antipattern before Java 8—it was the idiomatic way of writing this code
• Source code of withInitial
16
![Page 17: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/17.jpg)
#JCConf
Monad Patterns
17
![Page 18: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/18.jpg)
#JCConf
Optional
• Null sucks, right?
• Optional is trendy now! Optional<Order>
WET
18
![Page 19: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/19.jpg)
#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
![Page 20: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/20.jpg)
#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
![Page 21: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/21.jpg)
#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
![Page 22: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/22.jpg)
#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
![Page 23: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/23.jpg)
#JCConf 23
![Page 24: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/24.jpg)
#JCConf
Stream
• for loop hell ... XD
• Call back hell ... XD
24
![Page 25: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/25.jpg)
#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
![Page 26: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/26.jpg)
#JCConf
• The flatMap operation
4 1
1 2 3 4 5 6
flatMap(List<Integer> to Stream<Integer>)
26
![Page 27: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/27.jpg)
#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
![Page 28: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/28.jpg)
#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
![Page 29: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/29.jpg)
#JCConf
• CompletableFuture Monad
flatMap
CompletableFuture<String>
CompletableFuture<String>
29
![Page 30: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/30.jpg)
#JCConf
• Composable Future
solo
birthdays
30
![Page 31: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/31.jpg)
#JCConf
Functional Reactive?
31
![Page 32: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/32.jpg)
#JCConf
Functional Reactive Programming
• Reactive
• Data flow
• Propagation of change
• Observer
• Functional
• Asynchronous
• …
WTF?
32
![Page 33: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/33.jpg)
#JCConf
Reactive Programming
• A programming paradigm oriented around data flows and the propagation of change
33
![Page 34: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/34.jpg)
#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
![Page 35: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/35.jpg)
#JCConf
Observer
• In MVC, Model-View could be implemented by the Observer pattern.
• Maybe, we can ...
(10, 15, 25)
(20, 25, 35)
35
![Page 36: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/36.jpg)
#JCConf
songs
• When things become complex ...
names
albums 1900s
solo artists songs songs songs
songs lengths
nationalities birthdays
36
![Page 37: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/37.jpg)
#JCConf
• RxJava Observable
• Look up artist information
• filter、flatMap? map? Stream?
Observable<Artist>
37
![Page 38: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/38.jpg)
#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
![Page 39: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/39.jpg)
#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
![Page 40: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/40.jpg)
#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
![Page 41: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/41.jpg)
#JCConf
• Make a mountain out of a molehill … XD
41
![Page 42: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/42.jpg)
#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
![Page 43: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/43.jpg)
#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
![Page 44: java8-patterns](https://reader033.vdocuments.us/reader033/viewer/2022060121/559447661a28ab0f0d8b4606/html5/thumbnails/44.jpg)
#JCConf
Thanks!!
44