Upload
spring-io
View
557
Download
1
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
© 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
Spring with ImmutabilityMatt Stine
Community Engineer, Cloud [email protected]
http://mattstine.comTwitter: @mstine
RELOADED
Item #15Minimize mutability
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
Simple!
Inherently thread-safe!
Can be shared freely.
Can share internals.
BigInteger
signum mag
int[] = 100101
BigInteger
signum mag
BigInteger
signum mag
BigInteger
signummagBigInteger
signummag
BigInteger
signummag
Calling negate()
Make great building blocks for other objects.
Require separate object for each distinct value.
Assume immutability during design.
If mutability necessary, limit as much as possible.
Make every field final unless there is a compelling reason to
do otherwise.
Thinking in Immutability
Forces New Style
Procedure
Arguments
Possible Return Val
Side Effects
Procedural Out
Function
Arguments
Return Value
Functional In
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/
Ease into Programming with Immutability
Ease into Functional Programming
?Big Questions
Big Questions
• Performance Concerns
• Construction of Large Objects
• Framework Concerns
Performance Concerns
BigInteger
signum mag
int[] = 100101
BigInteger
signum mag
BigInteger
signum mag
BigInteger
signummagBigInteger
signummag
BigInteger
signummag
Revisiting negate()
What about large collections of objects?
Persistent Data Structures
Persistent List
From Programming Concurrency on the JVM, Venkat Subramaniam
Persistent Hash Trie
From Programming Concurrency on the JVM, Venkat Subramaniam
Persistent Hash Trie
From Programming Concurrency on the JVM, Venkat Subramaniam
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
Code Examples = Spockhttp://spockframework.org
Construction of Large Objects
Telescoping Constructor Pattern
Doesn’t scale!
JavaBean PatternFAIL
http://en.wikipedia.org/wiki/File:Builder_UML_class_diagram.svg
The Builder Pattern
Builder Pattern
• Static member class of class it builds
• Simulates named optional parameters
• Can provide validation
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
Framework Concerns
Anything that...
• Requires a no-arg constructor
• Requires setter methods
Bridge Pattern
Morrison Bridge, Portland, OR
Marick’s Restructuring Layer
Nugget
Nugget
ImmutableNugget
Mutable World
Mutable World
Restructuring
Restructuring
Dozerhttp://dozer.sourceforge.net/https://github.com/DozerMapper/dozer
JavaBean PatternFAIL
Use the Builder!http://codeslut.blogspot.com/2010/05/mapping-immutable-value-objects-with.html
Frameworks Supporting Immutability
JIRM
Java Immutable object Relational Mapperhttps://github.com/agentgt/jirm
Leverages Spring JDBC
Code Samples
https://github.com/mstine/immutability-examples
Matt StineCommunity Engineer, Cloud Foundry
[email protected]://mattstine.com
Twitter: @mstinePlease fill out an evaluation!
Spring with
Immutability
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