Successfully reported this slideshow.

java8-patterns

30

Share

Upcoming SlideShare
JDK8 Functional API
JDK8 Functional API
Loading in …3
×
1 of 44
1 of 44

java8-patterns

30

Share

Download to read offline

- 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/

- 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/

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

java8-patterns

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

×