Spring with Immutability
-
Upload
spring-io -
Category
Technology
-
view
557 -
download
1
description
Transcript of Spring with Immutability
![Page 1: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/1.jpg)
© 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
Spring with ImmutabilityMatt Stine
Community Engineer, Cloud [email protected]
http://mattstine.comTwitter: @mstine
![Page 2: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/2.jpg)
RELOADED
![Page 3: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/3.jpg)
Item #15Minimize mutability
![Page 4: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/4.jpg)
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
![Page 5: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/5.jpg)
![Page 6: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/6.jpg)
Simple!
![Page 7: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/7.jpg)
Inherently thread-safe!
![Page 8: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/8.jpg)
Can be shared freely.
![Page 9: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/9.jpg)
![Page 10: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/10.jpg)
Can share internals.
![Page 11: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/11.jpg)
BigInteger
signum mag
int[] = 100101
BigInteger
signum mag
BigInteger
signum mag
BigInteger
signummagBigInteger
signummag
BigInteger
signummag
Calling negate()
![Page 12: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/12.jpg)
Make great building blocks for other objects.
![Page 13: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/13.jpg)
Require separate object for each distinct value.
![Page 14: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/14.jpg)
Assume immutability during design.
![Page 15: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/15.jpg)
If mutability necessary, limit as much as possible.
![Page 16: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/16.jpg)
Make every field final unless there is a compelling reason to
do otherwise.
![Page 17: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/17.jpg)
Thinking in Immutability
![Page 18: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/18.jpg)
Forces New Style
![Page 19: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/19.jpg)
Procedure
Arguments
Possible Return Val
Side Effects
Procedural Out
![Page 20: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/20.jpg)
Function
Arguments
Return Value
Functional In
![Page 21: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/21.jpg)
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 Fordhttp://www.ibm.com/developerworks/library/j-ft4/
![Page 22: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/22.jpg)
Ease into Programming with Immutability
![Page 23: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/23.jpg)
Ease into Functional Programming
![Page 24: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/24.jpg)
?Big Questions
![Page 25: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/25.jpg)
Big Questions
• Performance Concerns
• Construction of Large Objects
• Framework Concerns
![Page 26: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/26.jpg)
Performance Concerns
![Page 27: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/27.jpg)
BigInteger
signum mag
int[] = 100101
BigInteger
signum mag
BigInteger
signum mag
BigInteger
signummagBigInteger
signummag
BigInteger
signummag
Revisiting negate()
![Page 28: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/28.jpg)
What about large collections of objects?
![Page 29: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/29.jpg)
Persistent Data Structures
![Page 30: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/30.jpg)
Persistent List
From Programming Concurrency on the JVM, Venkat Subramaniam
![Page 31: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/31.jpg)
Persistent Hash Trie
From Programming Concurrency on the JVM, Venkat Subramaniam
![Page 32: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/32.jpg)
Persistent Hash Trie
From Programming Concurrency on the JVM, Venkat Subramaniam
![Page 33: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/33.jpg)
Library Support
• TotallyLazyhttp://code.google.com/p/totallylazy/
• Functional Javahttp://functionaljava.org/
• CLJ-DS (Clojure’s data structures modified for use outside of Clojure)https://github.com/krukow/clj-ds
![Page 34: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/34.jpg)
Code Examples = Spockhttp://spockframework.org
![Page 35: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/35.jpg)
![Page 36: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/36.jpg)
![Page 37: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/37.jpg)
![Page 38: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/38.jpg)
![Page 39: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/39.jpg)
Construction of Large Objects
![Page 40: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/40.jpg)
Telescoping Constructor Pattern
![Page 41: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/41.jpg)
![Page 42: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/42.jpg)
Doesn’t scale!
![Page 43: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/43.jpg)
JavaBean PatternFAIL
![Page 44: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/44.jpg)
http://en.wikipedia.org/wiki/File:Builder_UML_class_diagram.svg
The Builder Pattern
![Page 45: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/45.jpg)
Builder Pattern
• Static member class of class it builds
• Simulates named optional parameters
• Can provide validation
![Page 46: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/46.jpg)
![Page 47: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/47.jpg)
![Page 48: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/48.jpg)
![Page 49: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/49.jpg)
Bob the Builderhttp://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
![Page 50: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/50.jpg)
![Page 51: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/51.jpg)
![Page 52: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/52.jpg)
![Page 53: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/53.jpg)
![Page 54: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/54.jpg)
![Page 55: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/55.jpg)
![Page 56: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/56.jpg)
![Page 57: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/57.jpg)
![Page 58: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/58.jpg)
![Page 59: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/59.jpg)
![Page 60: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/60.jpg)
![Page 61: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/61.jpg)
![Page 62: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/62.jpg)
Framework Concerns
![Page 63: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/63.jpg)
![Page 64: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/64.jpg)
Anything that...
• Requires a no-arg constructor
• Requires setter methods
![Page 65: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/65.jpg)
Bridge Pattern
Morrison Bridge, Portland, OR
![Page 66: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/66.jpg)
Marick’s Restructuring Layer
![Page 67: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/67.jpg)
Nugget
Nugget
ImmutableNugget
Mutable World
Mutable World
Restructuring
Restructuring
![Page 68: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/68.jpg)
Dozerhttp://dozer.sourceforge.net/https://github.com/DozerMapper/dozer
![Page 69: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/69.jpg)
JavaBean PatternFAIL
![Page 70: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/70.jpg)
Use the Builder!http://codeslut.blogspot.com/2010/05/mapping-immutable-value-objects-with.html
![Page 71: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/71.jpg)
![Page 72: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/72.jpg)
![Page 73: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/73.jpg)
![Page 74: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/74.jpg)
![Page 75: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/75.jpg)
![Page 76: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/76.jpg)
Frameworks Supporting Immutability
![Page 77: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/77.jpg)
JIRM
Java Immutable object Relational Mapperhttps://github.com/agentgt/jirm
![Page 78: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/78.jpg)
Leverages Spring JDBC
![Page 79: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/79.jpg)
![Page 80: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/80.jpg)
![Page 81: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/81.jpg)
![Page 82: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/82.jpg)
![Page 83: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/83.jpg)
![Page 84: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/84.jpg)
![Page 85: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/85.jpg)
Code Samples
https://github.com/mstine/immutability-examples
![Page 86: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/86.jpg)
Matt StineCommunity Engineer, Cloud Foundry
[email protected]://mattstine.com
Twitter: @mstinePlease fill out an evaluation!
Spring with
Immutability
![Page 87: Spring with Immutability](https://reader031.fdocuments.in/reader031/viewer/2022020115/554f56deb4c905b9508b5127/html5/thumbnails/87.jpg)
Image Credits
• “Think,” 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
• “Yellow,” 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