430 likes | 628 Views
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.
E N D
Object Identity = vs. == copying objects Value Object ValueWithHistory Object-oriented programming and design
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
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
Equality vs. Identity • Equality is user defined. • = anObject • ^self == anObject • Identity is system defined. • == anObject • <primitive: 110> Object-oriented programming and design
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
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
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
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
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
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
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
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
Why Value Objects? • Reveal intention • Check consistency (dynamic type check) • Better printing / input • Disadvantages • More classes Object-oriented programming and design
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
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
Violating Information Hiding • An accessing method discloses information. • anEmployee transactions add: (Paycheck new) • anEmployee transactions remove • Very dangerous! Object-oriented programming and design
Information Hiding • Alternative 2 • transactionsDo: aBlock • transaction do: aBlock • Alternative 3 • transactions • ^transactions copy Object-oriented programming and design
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
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
transactions Employee transactions OCollection OCollection Timecard Paycheck Timecard Object-oriented programming and design
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
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
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
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
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
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
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
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
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
ValueWithHistory • initialize • date := SortedSequence new. • value := OrderedCollection new. Object-oriented programming and design
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
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
SortedSequence • indexOfStartOfIntervalContaining: anElement • self isEmpty ifTrue: [^0]. • ^(self indexForInserting: anElement) - firstIndex Object-oriented programming and design
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
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
(continued) • date add: aDate. • value add: (value at: index) beforeIndex: index + 1. • ^index + 1 Object-oriented programming and design
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
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
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
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
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