Post on 21-Jan-2018
@aditlal
Immutable ObjectsLess is more
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
Cognitive Clouds | 2017
Objects can be altered
Problem ?
They are not thread safeMultiple state - not easy to maintain
Cognitive Clouds | 2017
Prob 1
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Fix it ?
Cognitive Clouds | 2017
Prob 2
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Cognitive Clouds | 2017
Prob 3
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
We want Money to be Value Type
Cognitive Clouds | 2017
(*Immutable | Null Safe)
public class Money { public Currency currency; public long amount; … }
Cognitive Clouds | 2017
Money POJO
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
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.
public class Money { public Currency currency; public long amount; } // No modifiers
Cognitive Clouds | 2017
Value Type
public final class Money { public Currency currency; public long amount;
}
Value Type
Cognitive Clouds | 2017
public final class Money { private Currency currency; private long amount;
}
Value Type
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; }
}
Value Type
Cognitive Clouds | 2017
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
@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
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 + '}'; }
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 + '}'; }
a new property is added?
What if
Cognitive Clouds | 2017
we rename a field?
Retest entire code , analyse , maintain it.
Yikes !!!
AutoValueRescue
Cognitive Clouds | 2017
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
Cognitive Clouds | 2017
dependencies { provided 'com.google.auto.value:auto-value:1.2' apt 'com.google.auto.value:auto-value:1.2' }
@AutoValue public abstract class Money { public abstract Currency currency(); public abstract long amount(); }
Cognitive Clouds | 2017
@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
@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
@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
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
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
Questions