Share
Embed
In a Nutshell: Immutable Classes
In this valuable talk, Marcus will describe exactly what immutable classes are. He'll tell you about their advantages and disadvantages, and show you how to create them. He'll finish by giving you concrete advice on when to use immutable classes in your daily work.
Marcus Biel
October 30, 2017
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
63 slides
-
Copyright © 2017 Marcus Biel www.cleancodeacademy.com About me @MarcusBiel Clean
Code Evangelist JCP Member Senior Software Engineer -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Outline • What is
an Immutable Class? • Advantages of Immutable Classes • Disadvantages of Immutable Classes • How to create an Immutable Class? -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com What is an Immutable
Class? USS Enterprise (NCC-1701) ImmutableSpaceship enterprise = new ImmutableSpaceship("Enterprise"); enterprise.exploreGalaxy(); -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public ImmutableSpaceship exploreGalaxy() {
return new ImmutableSpaceship(name, Destination.OUTER_SPACE); } public final class ImmutableSpaceship { private final String name; private final Destination destination; public ImmutableSpaceship(String name) { this.name = name; this.destination = Destination.NONE; } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } What is an Immutable Class? -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Outline • What is
an Immutable Class? • Advantages of Immutable Classes • Disadvantages of Immutable Classes • How to create an Immutable Class? -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Advantages of Immutable Classes
• Are stable • Are fault tolerant (Failure Atomicity) • Can be shared freely • Work well as Map keys and Set elements -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects are stable
private void validate(long balance) { if (balance < 0) { throw new IllegalArgumentException("balance must not be negative:"+ balance); } } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public final class ImmutableAccount
{ private final long balance; public ImmutableAccount(long balance) { this.balance = balance; } public ImmutableAccount withdraw(long amount) {…} public ImmutableAccount payDebt(long amount) {…} public ImmutableAccount transferMoney(long amount) {…} […] } Immutable Objects are stable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public final class ImmutableAccount
{ private final long balance; public ImmutableAccount(long balance) { validate(balance); this.balance = balance; } public ImmutableAccount withdraw(long amount) {…} public ImmutableAccount payDebt(long amount) {…} public ImmutableAccount transferMoney(long amount) {…} […] } Immutable Objects are stable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Advantages of Immutable Classes
• Are stable • Are fault tolerant (Failure Atomicity) • Can be shared freely • Work well as Map keys and Set elements -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public final class ImmutableAccount
{ private final long balance; public ImmutableAccount(long balance) { validate(balance); this.balance = balance; } public ImmutableAccount withdraw(long amount) { long newBalance = newBalance(amount); return new ImmutableAccount(newBalance); } private long newBalance(long amount) { } […] } Immutable Objects are fault tolerant -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public final class ImmutableAccount
{ private final long balance; public ImmutableAccount(long balance) { validate(balance); this.balance = balance; } public ImmutableAccount withdraw(long amount) { long newBalance = newBalance(amount); return new ImmutableAccount(newBalance); } private long newBalance(long amount) { // error during balance calculation } […] } Immutable Objects are fault tolerant -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Advantages of Immutable Classes
• Are stable • Are fault tolerant (Failure Atomicity) • Can be shared freely • Work well as Map keys and Set elements -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects can be
shared freely Balance Account Account Holder Holder Balance -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects can be
shared freely Account Account Holder Holder Balance -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects can be
shared freely Account Balance Holder Account Holder Balance -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Advantages of Immutable Classes
• Are stable • Are fault tolerant (Failure Atomicity) • Can be shared freely • Work well as Map keys and Set elements -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Work well as Map
keys and Set elements java.util.HashMap -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Work well as Map
keys and Set elements java.util.HashMap -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com java.util.HashMap Work well as
Map keys and Set elements -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com java.util.HashMap Work well as
Map keys and Set elements -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Outline • What is
an Immutable Class? • Advantages of Immutable Classes • Disadvantages of Immutable Classes • How to create an Immutable Class? -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Disadvantages of Immutable Classes
• May lead to performance problems -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects may lead
to performance problems Object -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects may lead
to performance problems -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Immutable Objects may lead
to performance problems -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Outline • What is
an Immutable Class? • Advantages of Immutable Classes • Disadvantages of Immutable Classes • How to create an Immutable Class? -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com How to create an
Immutable Class 1. Make all attributes private and final. 2. Don’t provide any methods that modify the object’s state. 3. Ensure that the class can’t be extended. 4. Ensure exclusive access to any mutable attributes. How to create an Immutable Class -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com private final String name;
private final Destination destination; 1. Make all attributes private final public final class ImmutableSpaceship { public ImmutableSpaceship(String name) { this.name = name; this.destination = Destination.NONE; } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } public ImmutableSpaceship exploreGalaxy() { return new ImmutableSpaceship(name, Destination.OUTER_SPACE); } […] } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com How to create an
Immutable Class 1. Make all attributes private and final. 2. Don’t provide any methods that modify the object’s state. 3. Ensure that the class can’t be extended. 4. Ensure exclusive access to any mutable attributes. -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public ImmutableSpaceship exploreGalaxy() {
return new ImmutableSpaceship(name, Destination.OUTER_SPACE); } 2. Don’t provide any methods that modify the object’s state public final class ImmutableSpaceship { private final String name; private final Destination destination; public ImmutableSpaceship(String name) { this.name = name; this.destination = Destination.NONE; } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com How to create an
Immutable Class 1. Make all attributes private and final. 2. Don’t provide any methods that modify the object’s state. 3. Ensure that the class can’t be extended. 4. Ensure exclusive access to any mutable attributes. -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public class RomulanSpaceship extends
ImmutableSpaceship { 3. Ensure that the class can’t be extended […] } public RomulanSpaceship(String name) { super(name); } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public class RomulanSpaceship extends
ImmutableSpaceship { 3. Ensure that the class can’t be extended […] } public RomulanSpaceship(String name) { super(name); } private String evilName; private Destination evilDestination; -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com private String evilName; private
Destination evilDestination; public class RomulanSpaceship extends ImmutableSpaceship { 3. Ensure that the class can’t be extended […] } public RomulanSpaceship(String name) { super(name); } @Override public ImmutableSpaceship newDestination(Destination newDestination) { this.evilDestination = newDestination; return this; } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 3. Ensure that the
class can’t be extended public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public Destination currentDestination() { return destination; } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; public ImmutableSpaceship(String name) { this.name = name; this.destination = Destination.NONE; } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com How to create an
Immutable Class 1. Make all attributes private and final. 2. Don’t provide any methods that modify the object’s state. 3. Ensure that the class can’t be extended. 4. Ensure exclusive access to any mutable attributes. -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Name Spaceship Destination Name Immutable Mutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable Mutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable Mutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Mutable Mutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Mutable Spaceship Destination Name Immutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable Destination Mutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com 4. Ensure exclusive access
to any mutable attributes Spaceship Destination Name Immutable Destination Destination Mutable -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public ImmutableSpaceship(String name) {
this.name = name; this.destination = new Destination("NONE"); } 4. Ensure exclusive access to any mutable attributes public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public Destination currentDestination() { return destination; } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public Destination currentDestination() {
return destination; } 4. Ensure exclusive access to any mutable attributes public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public ImmutableSpaceship(String name) { this.name = name; this.destination = new Destination("NONE"); } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public Destination currentDestination() {
return destination; } 4. Ensure exclusive access to any mutable attributes public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public ImmutableSpaceship(String name) { this.name = name; this.destination = new Destination("NONE"); } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public Destination currentDestination() {
return new Destination(destination); } 4. Ensure exclusive access to any mutable attributes public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public ImmutableSpaceship(String name) { this.name = name; this.destination = new Destination("NONE"); } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com public ImmutableSpaceship newDestination(Destination newDestination)
{ return new ImmutableSpaceship(this.name, newDestination); } 4. Ensure exclusive access to any mutable attributes public Destination currentDestination() { return new Destination(destination); } public ImmutableSpaceship(String name) { this.name = name; this.destination = new Destination("NONE"); } private ImmutableSpaceship(String name, Destination destination) { this.name = name; this.destination = destination; } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com private ImmutableSpaceship(String name, Destination
destination) { this.name = name; this.destination = destination; } 4. Ensure exclusive access to any mutable attributes public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public Destination currentDestination() { return new Destination(destination); } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; public ImmutableSpaceship(String name) { this.name = name; this.destination = new Destination("NONE"); } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com private ImmutableSpaceship(String name, Destination
destination) { this.name = name; this.destination = new Destination(destination); } 4. Ensure exclusive access to any mutable attributes public ImmutableSpaceship newDestination(Destination newDestination) { return new ImmutableSpaceship(this.name, newDestination); } public Destination currentDestination() { return new Destination(destination); } […] } public final class ImmutableSpaceship { private final String name; private final Destination destination; public ImmutableSpaceship(String name) { this.name = name; this.destination = new Destination("NONE"); } -
Copyright © 2017 Marcus Biel www.cleancodeacademy.com Conclusion Immutable Classes… •
Enforce Encapsulation • Enforce Simplicity • Enforce Clean Architecture