How Immutability Helps in OOP

20
/20 @yegor256 1 How Immutability Helps in OOP? Yegor Bugayenko

Transcript of How Immutability Helps in OOP

Page 1: How Immutability Helps in OOP

/20@yegor256 1

How Immutability Helps in OOP?

Yegor Bugayenko

Page 2: How Immutability Helps in OOP

/20@yegor256 2

what is an object?

Page 3: How Immutability Helps in OOP

/20@yegor256 3

“Each object looks quite a bit like a little computer — it has a state, and it has operations that you can ask it to perform”- page 16

Page 4: How Immutability Helps in OOP

/20@yegor256 4

“An object is some memory that holds a value of some type”- page 40

Page 5: How Immutability Helps in OOP

/20@yegor256 5

“A class is a collection of data fields that hold values and methods that operate on those values”- page 98

Page 6: How Immutability Helps in OOP

/20@yegor256 6

“Objects may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods”

Page 7: How Immutability Helps in OOP

/20@yegor256 7

Page 8: How Immutability Helps in OOP

/20@yegor256 8

class Calculator { private int[] array; public void calculate(int a, int b) { this.array = new int[b - a]; for (int i = 0; i < this.array.length; ++i) { this.array[i] = a + i; } } public int[] numbers() { return this.array; } }

Page 9: How Immutability Helps in OOP

/20@yegor256 9

Calculator c = new Calculator(); c.calculate(5, 15); int[] x = c.numbers();

Page 10: How Immutability Helps in OOP

/20@yegor256 10

“who I am?” vs.

“what I do?”

Page 11: How Immutability Helps in OOP

/20@yegor256 11

class Range { private final int a; private final int b; Range(int start, int finish) { this.a = start; this.b = finish; } public int[] numbers() { int[] array = new int[this.b - this.a]; for (int i = 0; i < array.length; ++i) { array[i] = this.a + i; } return array; } }

Page 12: How Immutability Helps in OOP

/20@yegor256 12

Range r = new Range(5, 15); int[] x = r.numbers();

Page 13: How Immutability Helps in OOP

/20@yegor256 13

R range = new Range(5, 15); int[] x = range.numbers();

Calculator c = new Calculator(); c.calculate(5, 15); int[] x = c.numbers();

Page 14: How Immutability Helps in OOP

/20@yegor256 14

R range = new OddOnly( new Range(5, 15) ); int[] x = range.numbers();

Calculator c = new Calculator(); c.setOddOnly(true); c.calculate(5, 15); int[] x = c.numbers();

Page 15: How Immutability Helps in OOP

/20@yegor256 15

R range = new SquareAll( new OddOnly( new Range(5, 15) ) ); int[] x = range.numbers();

Calculator c = new Calculator(); c.setOddOnly(true); c.setSquareAll(true); c.calculate(5, 15); int[] x = c.numbers();

Page 16: How Immutability Helps in OOP

/20@yegor256 16

R range = new Incremented( new Squared( new OddOnly( new Range(5, 15) ) ), 100 ); int[] x = range.numbers();

Calculator c = new Calculator(); c.setOddOnly(true); c.setSquared(true); c.setIncremented(100); c.calculate(5, 15); int[] x = c.numbers();

Page 17: How Immutability Helps in OOP

/20@yegor256 17

R range = new Logged( new Incremented( new Squared( new OddOnly( new Range(5, 15) ) ), 100 ) ); int[] x = range.numbers();

Calculator c = new Calculator(); c.setOddOnly(true); c.setSquared(true); c.setIncremented(100); c.setLogged(true); c.calculate(5, 15); int[] x = c.numbers();

Page 18: How Immutability Helps in OOP

/20@yegor256 18

R range = new Logged( new Incremented( new Squared( new OddOnly( new RangeFromFile( new File(“a.txt”) ) ) ), 100 ) ); int[] x = range.numbers();

Calculator c = new Calculator(); c.setFromFile(“a.txt”); //??? c.setOddOnly(true); c.setSquared(true); c.setIncremented(100); c.setLogged(true); c.calculate(5, 15); int[] x = c.numbers();

Page 19: How Immutability Helps in OOP

/20@yegor256 19

what is an object?

Page 20: How Immutability Helps in OOP

/20@yegor256 20

www.yegor256.com