49
@aditlal Immutable Objects Less is more

Exploring immutability

Embed Size (px)

Citation preview

Page 1: Exploring immutability

@aditlal

Immutable ObjectsLess is more

Page 2: Exploring immutability

MutableMutability — the ability to create object whose initial value can be changed later

Cognitive Clouds | 2017

Immutability — the ability to create object whose initial value cannot be changed later

Immutable

Page 3: Exploring immutability

Cognitive Clouds | 2017

Objects can be altered

Problem ?

They are not thread safeMultiple state - not easy to maintain

Page 4: Exploring immutability

Cognitive Clouds | 2017

Prob 1

Page 5: Exploring immutability

Cognitive Clouds | 2017

Page 6: Exploring immutability

Cognitive Clouds | 2017

Page 7: Exploring immutability

Cognitive Clouds | 2017

Page 8: Exploring immutability

Cognitive Clouds | 2017

Page 9: Exploring immutability

Cognitive Clouds | 2017

Page 10: Exploring immutability

Cognitive Clouds | 2017

Page 11: Exploring immutability

Cognitive Clouds | 2017

Fix it ?

Page 12: Exploring immutability

Cognitive Clouds | 2017

Prob 2

Page 13: Exploring immutability

Cognitive Clouds | 2017

Page 14: Exploring immutability

Cognitive Clouds | 2017

Page 15: Exploring immutability

Cognitive Clouds | 2017

Prob 3

Page 16: Exploring immutability
Page 17: Exploring immutability
Page 18: Exploring immutability
Page 19: Exploring immutability
Page 20: Exploring immutability
Page 21: Exploring immutability
Page 22: Exploring immutability
Page 23: Exploring immutability
Page 24: Exploring immutability
Page 25: Exploring immutability

Cognitive Clouds | 2017

• immutable objects are simpler to construct, test• truly immutable objects are always thread-safe• their usage is side-effect free (no defensive copies)• identity mutability problem is avoided

• Less prone to errors.• they prevent NULL references, which are bad

Immutable

Page 26: Exploring immutability

We want Money to be Value Type

Cognitive Clouds | 2017

(*Immutable | Null Safe)

Page 27: Exploring immutability

public class Money { public Currency currency; public long amount; … }

Cognitive Clouds | 2017

Money POJO

Page 28: Exploring immutability

A value type is simply an immutable objects whose equality is based on

property values, as opposed to identity. Think of the Money object

Cognitive Clouds | 2017

Page 29: Exploring immutability

Plain Java Immutable

Cognitive Clouds | 2017

· The class cant be extended. Making the class final should do the job, this way you prevent malicious subclassing that can alter state.

· All fields are final. setting your fields as final you will need to pass all the values through constructor or create a builder pattern.

· Make fields private. I think it doesn’t need more explanation, if public you can still access and change fields values.

Page 30: Exploring immutability

public class Money { public Currency currency; public long amount; } // No modifiers

Cognitive Clouds | 2017

Value Type

Page 31: Exploring immutability

public final class Money { public Currency currency; public long amount;

}

Value Type

Cognitive Clouds | 2017

Page 32: Exploring immutability

public final class Money { private Currency currency; private long amount;

}

Value Type

Cognitive Clouds | 2017

Page 33: Exploring immutability

public final class Money { private Currency currency; private long amount;

public Money(Currency currency, long amount) { this.currency = currency; this.amount = amount; }

}

Value Type

Cognitive Clouds | 2017

Page 34: Exploring immutability

Money money1 = new Money(); // $ and 25 Money money2 = new Money(); // $ and 25

if(money1.equal(money2)) //true or false

Are the two objects equal?

Cognitive Clouds | 2017

Page 35: Exploring immutability

@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Money money = (Money) o; if (Double.compare(money.amount, amount) != 0) return false; return currency.equals(money.currency); } @Override public int hashCode() { int result; long temp; result = currency.hashCode(); result = 31 * result + (int) (amount ^ (amount >>> 32)); return result; }

Cognitive Clouds | 2017

Page 36: Exploring immutability

Cognitive Clouds | 2017

@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Money money = (Money) o; if (Double.compare(money.amount, amount) != 0) return false; return currency.equals(money.currency); } @Override public int hashCode() { int result; long temp; result = currency.hashCode(); result = 31 * result + (int) (amount ^ (amount >>> 32)); return result; } @Override public String toString() { return "Money{" + "currency=" + currency + ", amount=" + amount + '}'; }

Page 37: Exploring immutability

Cognitive Clouds | 2017

public final class Money { private Currency currency; private long amount; public Money(Currency currency, long amount) { this.currency = currency; this.amount = amount; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Money money = (Money) o; if (money.amount != amount) return false; return currency.equals(money.currency); } @Override public int hashCode() { int result; long temp; result = currency.hashCode(); result = 31 * result + (int) (amount ^ (amount >>> 32)); return result; } @Override public String toString() { return "Money{" + "currency=" + currency + ", amount=" + amount + '}'; }

Page 38: Exploring immutability

a new property is added?

What if

Cognitive Clouds | 2017

we rename a field?

Retest entire code , analyse , maintain it.

Page 39: Exploring immutability

Yikes !!!

Page 40: Exploring immutability

AutoValueRescue

Cognitive Clouds | 2017

Page 41: Exploring immutability

Cognitive Clouds | 2017

AutoValueannotation processor that generates all of the mundane value-type code for you

Full implementation automated during compile timeDebugger friendlyNo added runtime dependency

Page 42: Exploring immutability

Cognitive Clouds | 2017

dependencies { provided 'com.google.auto.value:auto-value:1.2' apt 'com.google.auto.value:auto-value:1.2' }

Page 43: Exploring immutability

@AutoValue public abstract class Money { public abstract Currency currency(); public abstract long amount(); }

Cognitive Clouds | 2017

Page 44: Exploring immutability

@AutoValue public abstract class Money { public abstract Currency currency(); public abstract long amount(); public static Money create(Currency currency, long amount) {

return new AutoValue_Money(currency, amount); } }

Cognitive Clouds | 2017

Page 45: Exploring immutability

@AutoValue public abstract class Money { public abstract Currency currency(); public abstract long amount(); public static Money create(Currency currency, long amount) {

return new AutoValue_Money(currency, amount); }

public String displayString() { return currency().symbol() + amount(); } }

Cognitive Clouds | 2017

Page 46: Exploring immutability

@AutoValue public abstract class Money { public abstract Currency currency(); public abstract long amount(); @AutoValue.Builder abstract static class Builder() {

abstract Builder setCurrency(Currency currency); abstract Builder setAmount(Long amount); abstract Money build(); }

… }

Cognitive Clouds | 2017

Builder pattern

Page 47: Exploring immutability

Class AutoValue_Money is created on compile time

Cognitive Clouds | 2017

implements all the required boilerplate code

Add or modify any property , compile time annotation will regenerate the code for you.

Generated class is private and final and subclasses your value type class

Good stuff

Page 48: Exploring immutability

AutoValue - Extensionsauto-value-parcel

auto-value-cursor

auto-value-redacted

auto-json

auto-value-gson

auto-value-with

https://gist.github.com/gabrielittner/2afc80cbd8e75dc89527

Cognitive Clouds | 2017

Page 49: Exploring immutability

Questions