1 / 41

Object Identity

Object Identity. = vs. == copying objects Value Object ValueWithHistory. 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. Object Protocol.

Download Presentation

Object Identity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Identity = vs. == copying objects Value Object ValueWithHistory 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. Object-oriented programming and design

  3. Object Protocol • Operations understood by all objects: • == anObject identity - same object • = anObject equality - same value • ~~ anObject different objects • ~= anObject different values Object-oriented programming and design

  4. Equality vs. Identity • Equality is user defined. • = anObject • ^self == anObject • Identity is system defined. • == anObject • <primitive: 110> 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. 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. 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. Object-oriented programming and design

  8. Sharing name “Joe Smith” Invoice date Oct. 4, 2005 date Invoice “Ann Jones” name date Oct. 3, 2005 Invoice name “Ann Jones” name Invoice Oct. 3, 2005 date 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. 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 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. • Initialize variables when it is created and never change them afterwards. • Define = 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. Object-oriented programming and design

  13. Why Value Objects? • Reveal intention • Check consistency (dynamic type check) • Better printing / input • Disadvantages • More classes 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. 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: Object-oriented programming and design

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

  17. Information Hiding • Alternative 2 • transactionsDo: aBlock • transaction do: aBlock • Alternative 3 • transactions • ^transactions copy 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. 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. Object-oriented programming and design

  20. transactions Employee transactions OCollection OCollection Timecard Paycheck Timecard Object-oriented programming and design

  21. 1 2 | 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 x: 5. • s = t 3 | t s | t := Point x: 1 y: 17. s := t copy. s == t 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]] 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. 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? 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. 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. 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. 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 Object-oriented programming and design

  29. ValueWithHistory variables • date <SortedCollection of: Date> • value <OrderedCollection of: Number> • 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. Object-oriented programming and design

  30. ValueWithHistory • initialize • date := SortedSequence new. • value := OrderedCollection new. 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 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." Object-oriented programming and design

  33. SortedSequence • indexOfStartOfIntervalContaining: anElement • self isEmpty ifTrue: [^0]. • ^(self indexForInserting: anElement) - firstIndex 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] 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]. Object-oriented programming and design

  36. (continued) • date add: aDate. • value add: (value at: index) beforeIndex: index + 1. • ^index + 1 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 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 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 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) Object-oriented programming and design

  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

More Related