1Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 212Object Identity= vs. ==copying objectscreating objectsObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 213Object ProtocolOperations understood by all objects:== anObject identity - same object= anObject equality - same value~~ anObject different objects~= anObject different values2Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 214Equality vs. IdentityEquality is user defined.= anObject^self == anObjectIdentity is system defined.== anObject<primitive: 110>Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 215Equality(Date newDay: 40 year: 1995) ==(Date newDay: 40 year: 1995)is false, because they are physically twodistinct objects. However, theyrepresent the same value, so they areequal.3Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 216Each object has its own region ofmemory.“Object ID” is essentially a pointer.Variable contains object ID, not the spaceof an object.“Passing an object as an argument”means passing the object ID.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 217New ObjectsA new object is different from any existingobject.X new == X newis almost always false.“Rectangle new = Rectangle new” is true.4Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 218SharingInvoiceInvoiceInvoiceInvoicename“Ann Jones”Oct. 3, ‘75Oct. 3, ‘75Oct. 4, ‘75“Ann Jones”“Joe Smith”namenamedatenamedatedatedateObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 219SharingSharing saves space.Sharing makes changes to one objectvisible to another.Sharing is always safe when objects areimmutable.5Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 220What 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 Design - Copyright 1998 by Ralph E. Johnson 221Information HidingAn employees transactions are entirelyhidden from clients.To add an transaction, usepostTransaction:There is no way to access transactionsoutside Employee.6Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 222Information HidingSuppose you want to iterate overtransactions of an employee.Alternative 11) Add #transactions method2) anEmployee transactions do:Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 223Violating Information HidingAn accessing method disclosesinformation.anEmployee transactions add:(Paycheck new)anEmployee transactions removeVery dangerous!7Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 224Information HidingAlternative 2transactionsDo: aBlocktransaction do: aBlockAlternative 3transactions^transactions copyObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 225Copying"shallow copy" -- copy object, but notcontents of object"deep copy" -- copy object andcontents of object, recursively(usually VERY selectively)Copying is usually shallow copying.8Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 226CopyingIn class Object:copy^self shallowCopy postCopyTemplate Method pattern!Redefine postCopy to change the way tocopy variables, not copy.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 227OCollectiontransactionsOCollectionTimecardEmployeePaycheckTimecardtransactions9Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 228| 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 Design - Copyright 1998 by Ralph E. Johnson 229StreamsStream protocolObject composition10Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 230Uses of StreamsExternal iteratorParsingFormatted outputDataflow computingFile I/OObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 231ReadStream ProtocolReadStream• next, atEnd• (do:, nextMatchFor:)#next is valid if #atEnd is false.11Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 232ReadStreamt := ReadStream on: ‘this is the input’.[t atEnd]whileFalse:[t next = $p ifTrue: [^t]]]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 233IteratorInternal Iterator - do:External Iterator - streamsInternal iterator is easier to use, but lesspowerful.External iterators needed forsimultaneous iteration.12Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 234Iterator“return true if streams equal”[stream1 atEnd | stream2 atEnd]whileFalse:[stream1 next = stream2 nextifFalse: [^false]].^stream1 atEnd & stream2 atEndObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 235Stream ProtocolWriteStreamnextPut:(nextPutAll:, cr, tab, space)13Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 236LineStreamTakes character stream and returns oneinput line at a time.aStream := LineStream on: (‘stuff’asFilename readStream).inputLine := aStream next.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 237Creating a LineStreamClass methodon: aStream^self basicNew on: aStreamInstance methodon: aStreaminputStream := aStream14next| outputStream |outputStream := WriteStream on: (String new: 20).inputStreamdo: [:eachChar | eachChar = CRifTrue: [^outputStream contents]ifFalse: [outputStream nextPut: eachChar]].^outputStream contentsObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 239LineStreamatEnd^inputStream atEndClass methodinitialize"LineStream initialize"CR := Character cr15Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 240AdaptersStream ofStringsStream ofCharactersTranscript show: (LineStream on: (‘stuff’ asFilename readStream)) nextClientObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 241Infinite StreamsIt is easy for streams to be infinite; they justalways return false for atEnd.IntegerStream is a class of streams thatreturn the positive integers in order. It hasone instance variable, value.16Object-oriented Programming and Design - Copyright 1998 by Ralph
View Full Document