Unformatted text preview:

Object IdentityIdentityObject ProtocolEquality vs. IdentityEqualityImplementation of IdentityNew ObjectsSharingSlide 9What is the value of?Value ObjectsSlide 12Why Value Objects?Information HidingSlide 15Violating Information HidingSlide 17CopyingSlide 19PowerPoint PresentationSlide 21Classic bugKeeping Track of TimeSlide 24Slide 25ValueWithHistorySlide 27ValueWithHistory protocolValueWithHistory variablesSlide 30Slide 31SortedSequenceSlide 33Slide 34Slide 35(continued)Collection methodsOrderedCollection methodsSlide 39MoralObject-oriented programming and design1Object Identity= vs. ==copying objectscreating objectsObject-oriented programming and design2Identity“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 design3Object ProtocolOperations understood by all objects:== anObject identity - same object= anObject equality - same value~~ anObject different objects~= anObject different valuesObject-oriented programming and design4Equality vs. IdentityEquality is user defined.= anObject^self == anObjectIdentity is system defined.== anObject<primitive: 110>Object-oriented programming and design5Equality(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 design6Implementation of IdentityEach 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 design7New ObjectsA new object is different from any existing object.X new == X newis almost always false.“Rectangle new = Rectangle new” is true.Object-oriented programming and design8SharingInvoiceInvoiceInvoiceInvoicename“Ann Jones”Oct. 3, ‘75Oct. 3, ‘75Oct. 4, ‘75“Ann Jones”“Joe Smith”namenamedatenamedatedatedateObject-oriented programming and design9SharingSharing saves space.Sharing makes changes to one object visible to another.Sharing is always safe when objects are immutable.Object-oriented programming and design10What 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 == #aSymbolObject-oriented programming and design11Value ObjectsSome 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 design12Value ObjectsClasses 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 design13Why Value Objects?Reveal intentionCheck consistency (dynamic type check)Better printing / inputDisadvantagesMore classesObject-oriented programming and design14Information HidingAn 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 design15Information HidingSuppose you want to iterate over transactions of an employee.Alternative 11) Add #transactions method2) anEmployee transactions do:Object-oriented programming and design16Violating Information HidingAn accessing method discloses information.anEmployee transactions add: (Paycheck new)anEmployee transactions removeVery dangerous!Object-oriented programming and design17Information HidingAlternative 2transactionsDo: aBlocktransaction do: aBlockAlternative 3transactions^transactions copyObject-oriented programming and design18Copying"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 design19CopyingIn class Object:copy^self shallowCopy postCopyTemplate Method pattern!Redefine postCopy to change the way to copy variables, not copy.Object-oriented programming and design20OCollectionOCollectiontransactionstransactionsOCollectionOCollectionTimecardTimecardEmployeeEmployeePaycheckPaycheckTimecardTimecardtransactionstransactionsObject-oriented programming and design21| 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 == t231Object-oriented programming and design22Classic bugaSet 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 design23Keeping Track of TimeBug:1. Forget to enter timecard2. Raise salary3. Notice that timecard is missing and enter it.Object-oriented programming and design24Keeping Track of TimeProblem: how do you ensure that each transaction is processed with rules in effect at the time it took place?Object-oriented programming and design25Keeping Track of TimeThings 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 design26ValueWithHistoryI 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 design27ValueWithHistoryInstead 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 design28ValueWithHistory protocolat: aDate - return value at aDateat: aDate add: anAmount - add anAmount to value at aDate and at all times in the futurestarting: aDate become: anAmount - set value from aDate to the next


View Full Document

UIUC CS 598 - Object Identity

Download Object Identity
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Identity and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Identity 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?