• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Spring with Immutability
 

Spring with Immutability

on

  • 377 views

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

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.

Statistics

Views

Total Views
377
Views on SlideShare
375
Embed Views
2

Actions

Likes
1
Downloads
3
Comments
0

1 Embed 2

https://twitter.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Spring with Immutability Spring with Immutability Presentation Transcript

    • 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.
    • RELOADED
    • Item #15 Minimize 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.
    • Calling negate() BigInteger mag mag BigInteger signum signum signum BigInteger mag int[] = 100101 mag mag BigInteger signum mag BigInteger signum signum BigInteger
    • 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
    • Procedural Out Side Effects Arguments Procedure Possible Return Val
    • Functional In Arguments Function Return Value
    • 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/
    • Ease into Programming with Immutability
    • Ease into Functional Programming
    • ? Big Questions
    • Big Questions • • • Performance Concerns Construction of Large Objects Framework Concerns
    • Performance Concerns
    • Revisiting negate() BigInteger mag mag BigInteger signum signum signum BigInteger mag int[] = 100101 mag mag BigInteger signum mag BigInteger signum signum BigInteger
    • 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 • 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
    • Code Examples = Spock http://spockframework.org
    • Construction of Large Objects
    • Telescoping Constructor Pattern
    • Doesn’t scale!
    • L JavaBean IPattern A F
    • The Builder Pattern http://en.wikipedia.org/wiki/File:Builder_UML_class_diagram.svg
    • Builder Pattern • • • Static member class of class it builds Simulates named optional parameters Can provide validation
    • 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
    • Framework Concerns
    • Anything that... • • Requires a no-arg constructor Requires setter methods
    • Bridge Pattern Morrison Bridge, Portland, OR
    • Marick’s Restructuring Layer
    • Mutable World Restructuring Nugget Immutable Nugget Restructuring Nugget Mutable World
    • Dozer http://dozer.sourceforge.net/ https://github.com/DozerMapper/dozer
    • L JavaBean IPattern A F
    • Use the Builder! http://codeslut.blogspot.com/2010/05/mapping-immutable-value-objects-with.html
    • Frameworks Supporting Immutability
    • JIRM Java Immutable object Relational Mapper https://github.com/agentgt/jirm
    • Leverages Spring JDBC
    • Code Samples https://github.com/mstine/immutability-examples
    • Spring with Immutability Please fill out an evaluation! Matt Stine Community Engineer, Cloud Foundry mstine@gopivotal.com http://mattstine.com Twitter: @mstine
    • 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