Spring with Immutability

1,783 views

Published on

Speaker: Readers of Josh Bloch's "Effective Java" are sometimes perplexed when they reach Item #15: "Minimize Mutability." If we are to minimize mutability, then obviously we must maximize immutability. While all Java programmers utilize immutable objects every day (e.g. java.lang.String), when asked to create our own immutable classes, we often hesitate. However, if we push through this hesitation, we'll reap the benefits of simpler reasoning about program correctness, free thread safety, and other benefits.
One of the primary issues faced by enterprise Java programmers seeking to utilize immutable classes are framework issues. Enterprise frameworks from Spring to Hibernate have varying levels of support for immutability, ranging from decent to nonexistent. However, there several practical solutions available to the Spring developer, and this session will illuminate what's available.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,783
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
19
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Spring with Immutability

  1. 1. Spring with Immutability Matt Stine Community Engineer, Cloud Foundry mstine@gopivotal.com http://mattstine.com Twitter: @mstine © 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  2. 2. RELOADED
  3. 3. Item #15 Minimize mutability
  4. 4. Recipe • • • • • Don’t provide mutators Ensure the class can’t be extended Make all fields final Make all fields private Ensure exclusive access to any mutable components
  5. 5. Simple!
  6. 6. Inherently thread-safe!
  7. 7. Can be shared freely.
  8. 8. Can share internals.
  9. 9. Calling negate() BigInteger mag mag BigInteger signum signum signum BigInteger mag int[] = 100101 mag mag BigInteger signum mag BigInteger signum signum BigInteger
  10. 10. Make great building blocks for other objects.
  11. 11. Require separate object for each distinct value.
  12. 12. Assume immutability during design.
  13. 13. If mutability necessary, limit as much as possible.
  14. 14. Make every field final unless there is a compelling reason to do otherwise.
  15. 15. Thinking in Immutability
  16. 16. Forces New Style
  17. 17. Procedural Out Side Effects Arguments Procedure Possible Return Val
  18. 18. Functional In Arguments Function Return Value
  19. 19. Immutability is key to functional programming because it matches the goals of minimizing the parts that change, making it easier to reason about those parts. Neal Ford http://www.ibm.com/developerworks/library/j-ft4/
  20. 20. Ease into Programming with Immutability
  21. 21. Ease into Functional Programming
  22. 22. ? Big Questions
  23. 23. Big Questions • • • Performance Concerns Construction of Large Objects Framework Concerns
  24. 24. Performance Concerns
  25. 25. Revisiting negate() BigInteger mag mag BigInteger signum signum signum BigInteger mag int[] = 100101 mag mag BigInteger signum mag BigInteger signum signum BigInteger
  26. 26. What about large collections of objects?
  27. 27. Persistent Data Structures
  28. 28. Persistent List From Programming Concurrency on the JVM,Venkat Subramaniam
  29. 29. Persistent Hash Trie From Programming Concurrency on the JVM,Venkat Subramaniam
  30. 30. Persistent Hash Trie From Programming Concurrency on the JVM,Venkat Subramaniam
  31. 31. Library Support • TotallyLazy http://code.google.com/p/totallylazy/ • Functional Java http://functionaljava.org/ • CLJ-DS (Clojure’s data structures modified for use outside of Clojure) https://github.com/krukow/clj-ds
  32. 32. Code Examples = Spock http://spockframework.org
  33. 33. Construction of Large Objects
  34. 34. Telescoping Constructor Pattern
  35. 35. Doesn’t scale!
  36. 36. L JavaBean IPattern A F
  37. 37. The Builder Pattern http://en.wikipedia.org/wiki/File:Builder_UML_class_diagram.svg
  38. 38. Builder Pattern • • • Static member class of class it builds Simulates named optional parameters Can provide validation
  39. 39. Bob the Builder http://code.google.com/a/eclipselabs.org/p/bob-the-builder/ 1. Download Jar 2. Add to dropins folder Works with Helios, Indigo, and MyEclipse 10
  40. 40. Framework Concerns
  41. 41. Anything that... • • Requires a no-arg constructor Requires setter methods
  42. 42. Bridge Pattern Morrison Bridge, Portland, OR
  43. 43. Marick’s Restructuring Layer
  44. 44. Mutable World Restructuring Nugget Immutable Nugget Restructuring Nugget Mutable World
  45. 45. Dozer http://dozer.sourceforge.net/ https://github.com/DozerMapper/dozer
  46. 46. L JavaBean IPattern A F
  47. 47. Use the Builder! http://codeslut.blogspot.com/2010/05/mapping-immutable-value-objects-with.html
  48. 48. Frameworks Supporting Immutability
  49. 49. JIRM Java Immutable object Relational Mapper https://github.com/agentgt/jirm
  50. 50. Leverages Spring JDBC
  51. 51. Code Samples https://github.com/mstine/immutability-examples
  52. 52. Spring with Immutability Please fill out an evaluation! Matt Stine Community Engineer, Cloud Foundry mstine@gopivotal.com http://mattstine.com Twitter: @mstine
  53. 53. Image Credits • • • “Think,” sculpture by Nathan Sawaya, photo by Matt Stine • • • “Yellow,” sculpture by Nathan Sawaya, photo by Matt Stine “Blue Sky,” sculpture by Nathan Sawaya, photo by Matt Stine “Title Unknown,” sculpture by Nathan Sawaya, photo by Matt Stine “Morrison Bridge, Portland, OR,” photo by Matt Stine “CAT Dozer,” http://www.flickr.com/photos/ mkw87/4877121037

×