Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.

Similar presentations


Presentation on theme: "Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory."— Presentation transcript:

1 Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory

2 Object-oriented programming and design 2 Identity “I’ll take the same thing he is having.” Objects have identity. Their state changes, but their identity doesn’t change. You can change the value of an object, but a 3 is always 3.

3 Object-oriented programming and design 3 Object Protocol Operations understood by all objects: == anObjectidentity - same object = anObjectequality - same value ~~ anObjectdifferent objects ~= anObjectdifferent values

4 Object-oriented programming and design 4 Equality vs. Identity Equality is user defined. = anObject ^self == anObject Identity is system defined. == anObject

5 Object-oriented programming and design 5 Equality (Date newDay: 40 year: 1995) == (Date newDay: 40 year: 1995) is false, because they are physically two distinct objects. However, they represent the same value, so they are equal.

6 Object-oriented programming and design 6 Implementation of Identity Each object has its own region of memory. “Object ID” is essentially a pointer. Variable contains object ID, not the space of an object. “Passing an object as an argument” means passing the object ID. -- tests for pointers being equal.

7 Object-oriented programming and design 7 New Objects A new object is different from any existing object. X new == X new is almost always false. “Rectangle new = Rectangle new” is true.

8 Object-oriented programming and design 8 Sharing Invoice name “Ann Jones” Oct. 3, 2005 Oct. 4, 2005 “Ann Jones” “Joe Smith” name date name date

9 Object-oriented programming and design 9 Sharing Sharing saves space. Sharing makes changes to one object visible to another. Sharing is always safe when objects are immutable.

10 Object-oriented programming and design 10 What is the value of? (Point x: 3 y: 17) == (Point x: 3 y: 17) (Point x: 3 y: 17) = (Point x: 3 y: 17) 'this is a string' == 'this is a string' #aSymbol == #aSymbol

11 Object-oriented programming and design 11 Value Objects Some objects (numbers, symbols, Military ranks) never change state. A value object is an object that is used like a value. l Initialize variables when it is created and never change them afterwards. l Define =

12 Object-oriented programming and design 12 Value Objects Classes that represent special values in your domain (SocialSecurityIdentifier, Address, MusicalNote, DayOfWeek, Money) The only methods that assign to instance variables are initialization methods. Only instance creation methods should call initialization methods.

13 Object-oriented programming and design 13 Why Value Objects? Reveal intention Check consistency (dynamic type check) Better printing / input Disadvantages More classes

14 Object-oriented programming and design 14 Information Hiding An employee’s transactions are entirely hidden from clients. To add a transaction, use postTransaction: There is no way to access transactions outside Employee.

15 Object-oriented programming and design 15 Information Hiding Suppose you want to iterate over transactions of an employee. Alternative 1 1) Add #transactions method 2) anEmployee transactions do:

16 Object-oriented programming and design 16 Violating Information Hiding An accessing method discloses information. anEmployee transactions add: (Paycheck new) anEmployee transactions remove Very dangerous!

17 Object-oriented programming and design 17 Information Hiding Alternative 2 transactionsDo: aBlock transaction do: aBlock Alternative 3 transactions ^transactions copy

18 Object-oriented programming and design 18 Copying "shallow copy" -- copy object, but not contents of object "deep copy" -- copy object and contents of object, recursively (usually VERY selectively) Copying is usually shallow copying.

19 Object-oriented programming and design 19 Copying In class Object: copy ^self shallowCopy postCopy Template Method pattern! Redefine postCopy to change the way to copy variables, not copy.

20 Object-oriented programming and design 20 OCollection transactions OCollection Timecard Employee PaycheckTimecard transactions

21 Object-oriented programming and design 21 | t s | t := Point x: 1 y: 17. s := t copy. s x: 5. s = t | t s | t := Point x: 1 y: 17. s := t. s x: 5. s = t | t s | t := Point x: 1 y: 17. s := t copy. s == t 2 3 1

22 Object-oriented programming and design 22 Classic bug aSet do: [:each | each isBold ifTrue: [aSet remove: each]] Avoid modifying a collection when you are iterating over it. aSet copy do: [:each | each isBold ifTrue: [aSet remove: each]]

23 Object-oriented programming and design 23 Keeping Track of Time Bug: 1. Forget to enter timecard 2. Raise salary 3. Notice that timecard is missing and enter it.

24 Object-oriented programming and design 24 Keeping Track of Time Problem: how do you ensure that each transaction is processed with rules in effect at the time it took place?

25 Object-oriented programming and design 25 Keeping Track of Time Things that are hard with current payroll design: Make a graph of salary paid per month for each person. Make a graph of vacation time taken per month for the entire company.

26 Object-oriented programming and design 26 ValueWithHistory I represent a (probably numerical) value that changes over time. I can answer my value at any point in time (usually a date). I can update my value at any point in time, and also add a number to my value from any point in time on into the future. My value does not change continuously, but changes at discrete points in time.

27 Object-oriented programming and design 27 ValueWithHistory Instead of storing a value in a variable, store it in a ValueWithHistory. earnings := ValueWithHistory zero. earnings at: today add: 50. earnings starting: endOfYear become: 0.

28 Object-oriented programming and design 28 ValueWithHistory protocol at: aDate- return value at aDate at: aDate add: anAmount- add anAmount to value at aDate and at all times in the future starting: aDate become: anAmount- set value from aDate to the next specificed time to anAmount

29 Object-oriented programming and design 29 ValueWithHistory variables date value The i'th element of value matches the i'th element of date. The date collection indicates when the value takes on the next element of the value collection.

30 Object-oriented programming and design 30 ValueWithHistory initialize date := SortedSequence new. value := OrderedCollection new.

31 Object-oriented programming and design 31 ValueWithHistory at: aDate "Return value at the date" | index | index := date indexOfStartOfIntervalContaining: aDate. index = 0 ifTrue: [self error: 'date is too early']. ^value at: index

32 Object-oriented programming and design 32 SortedSequence SortedSequence is a subclass of SortedCollection with one method: indexOfStartOfIntervalContaining: anElement "Return the index of anElement or, if it is not present, of the index of the largest element smaller than it."

33 Object-oriented programming and design 33 SortedSequence indexOfStartOfIntervalContaining: anElement self isEmpty ifTrue: [^0]. ^(self indexForInserting: anElement) - firstIndex

34 Object-oriented programming and design 34 ValueWithHistory at: aDate add: anAmount| start | start := (self indexForAccessing: aDate). start to: value size do: [:each | value at: each put: (value at: each) + anAmount]

35 Object-oriented programming and design 35 ValueWithHistory indexForAccessing: aDate "Return index of slot for aDate, creating it if necessary." | index |index := date indexOfStartOfIntervalContaining: aDate. index = 0 ifTrue: [date add: aDate. value addFirst: 0. ^1]. (date at: index) = aDate ifTrue: [^index].

36 Object-oriented programming and design 36 (continued) date add: aDate. value add: (value at: index) beforeIndex: index + 1. ^index + 1

37 Object-oriented programming and design 37 Collection methods add: anElement adds to end of OrderedCollection, to position in sort-order of SortedCollection, to random position in Set

38 Object-oriented programming and design 38 OrderedCollection methods addFirst: anElement adds to beginning of OrderedCollection add: anElement beforeIndex: anInteger insert element at location, moving what is there

39 Object-oriented programming and design 39 ValueWithHistory starting: aDate become: aValue "Change value so that it becomes aValue at aDate." | index | index := self indexForAccessing: aDate. value at: index put: aValue

40 Object-oriented programming and design 40 Moral It is often best not to make instance variable be simple object like number or string. 1) Make it be a domain object. (Money instead of Number) This usually is a “value object”. 2) Make it be a holder. (ValueWithHistory instead of Number or Money)

41 Next time "A Laboratory For Teaching Object-Oriented Thinking"-by Beck, K, Cunningham, W. http://c2.com/doc/oopsla89/paper.html Object-oriented programming and design 41


Download ppt "Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory."

Similar presentations


Ads by Google