1 / 47

Chap.4 Object Identity

Object-Orientation by Khoshafian and Abnous. Chap.4 Object Identity. 서울대학교 컴퓨터공학부 객체지향시스템연구실 snu oopsla lab 교수 김 형 주. Contents. 1. Introduction 2. Referencing Objects in PLs 3. Object Naming in OS 4. Identity through Identifier Keys in DB 5. The Type/State/Identity Trichotomy

Download Presentation

Chap.4 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-Orientation by Khoshafian and Abnous Chap.4 Object Identity 서울대학교 컴퓨터공학부 객체지향시스템연구실 snu oopsla lab 교수 김 형 주

  2. Contents • 1. Introduction • 2. Referencing Objects in PLs • 3. Object Naming in OS • 4. Identity through Identifier Keys in DB • 5. The Type/State/Identity Trichotomy • 6. Operations with Identity • 7. Summary

  3. Introduction • Object Identity( Khoshanfian & Copeland, 1986 ) • one of fundamental object orientation concepts • an efficient way to both create & dispose objects at run time • a natural way to reaccess objects in subsequent programs, so objects can even become persistent • a proper way to contain or refer to other objects • clarifies, enhances, and extends the notions of • pointers in conventional PL • foreign keys in DB • filenames in OS • a way to dynamically construct arbitrary graph-structured composite or complex objects

  4. Ways of Accessing Objects • Traditional methods • memory references/addresses in PLs • user-specified names in OS • identifier keys in collections in DB • Object-oriented method • use object identity

  5. Referencing Objects in PLs • Referencing mechanisms • Earliest PLs of the 1950s, FORTRAN, COBOL • variable name • object sharing through the COMMON & EQUIVALENCE constructs • Algol 68, C, Pascal, etc. • pointer (object reference, ref constuct) • heap management

  6. Referencing Objects in PLs (2) • Pointers • Virtual memory addresses • Dynamic memory allocation (low level computational contruct within a high-level PL) • Pointer-level equality is only supported • Semantic problems • semantics of dynamic memory allocation primitives are implementation-dependent • scoping rules are not always correctly applicable by users

  7. Example in Pascal (1) new(C1); C1^.Real := 3.5; C1^.Imaginary := 2.7; C2 := C1; TYPE ComplexPtr = ^Complex Complex = RECORD Real : real; Imaginary : real; END < Before dispose(C1) > Dispose(C1); < After dispose(C1) > ** Dispose(C1) cannot resolve the dangling reference problem **What is the meaning of “Dispose(C1)”? ** What if the user access C2!

  8. C1 3.5 Real 2.7 C2 Free Space < Before dispose(C1) > 3.5 C1 Imaginary 2.7 C2 Free Space < After dispose(C1) >

  9. Example in Pascal (2) < TRUE equality predicates> new(C1); C1^.Real := 3.5; C1^.Imaginary := 2.7; C2 := C1; new(c3); C3^.Real := 3.5; C3^.Imaginary := 2.7; • C1 = C2 • C1.Real = C3.Real • C1.Imaginary = C3.Imaginary • C1^ = C3^ < FALSE equality predicates> • C1 = C3 ** The equality predicates: = in Pascal, == in C ** Pascal and C do not support record equality, but only pointer equality

  10. Example in Pascal (3) FUNCTION CompareComplex (C1, C2 : Complex) : boolean; begin if C1.Real = C2.Real and C1.Imaginary = C2.Imaginary then CompareComplex := TRUE else CompareComplex := FALSE end; {CompareComplex} “C1 = C3” was false “CompareComplex(C1,C3)” is true < When record equality is not supported in a PL, a function to implement complex number object equality is needed >

  11. Object Naming in OS • Commonly used method for identifying objects in OS is user-defined names for objects • Ex: Path names in OS • Hierarchical directory structure • Path is a concatenation of directory names • Disadvantages • Path name is long • Replication of files is enforced (hard to maintain consistency) • No way to test whether two files refer to the same object

  12. Examples(1) Items Computer TV Refrigerator Hard Soft Print Database Word-Proc SpreadSheet HP EPSON DeskJet 660 LaserJet 4L Items/Computer/Hard/Print/EPSON

  13. Example(2) • To create a link between an HP LaserJet 4L printer file & • PRINT in SYST1 link Items/Computer/Hard/Print/HP/LJ4L Items/Integ/SYST1/PRINT • To Unlink • link path • To check if two files are same information (content). • cmp file1 file2 • (But, no way to check whether two files refer to the same object)

  14. Identity Through Identifier Keys • Commonly used in RDBMS • Unique key (Primary key, 2nd-ary key) • some subset of the attributes of an object • Main problems • cannot modify identifier key value • nonuniformity (various key types) • excessive use of “unnatural” join operations

  15. Example Identifier Key Last Name First Name Age Address Adams Brown Ripper Silverman Smith Smith Tim Jim Jack Leo John Mary 23 32 70 34 32 32 “12 Sutton …..” “43 Doloney ……” “1 London …….” “55 H Street ….” “1212 Main ……” “1212 Main ……” Person table

  16. Type/State/Identity Trichotomy • Known Facts • A class implements a type • An object is an instance of a class • Objects have internal states expressed by their instance variables • Claim • Each object has a built-in Identity, which is independent of class or state • The state of an object can be changed, but the object identity remains the same

  17. Intuitive Example for O-ID • Consistency problem Name: [Last: “Smith , First: “John” ] Age: 32 Address: [Street #: 1212 Street name: “Main” City: Walnut Creek State: California Zip: 94596 ] Name: [Last: “Smith, First: “Mary” ] Age: 32 Address: [Street #: 1212 Street name: “Main” City: Walnut Creek State: California Zip: 94596 ] “John Smith” “Mary Smith” JohnSmith ChangeAddress: NewAddress ** Replication-based solution & Key-based solution ** O-ID solution is more efficient and reasonable!!!

  18. Object Spaces with Identity • Identifier • associated with every nonbase object • associated with the object at creation time • associated with only one object • Object • an object is an instance of a class (type) • an object has an identity • an object has a state • In comparison with pointers, object-identity is a semantic concept associated with objects, not a low level representation

  19. Example(1) • == to check for identical objects (pointer equality) • = to check for equality of object states (content equality) (e.g. A == B , A = B ) • Object State • The state of an object O : • AI1 : i1 , ……….. , AIn : in AI1, …….. , Ain : instance variables of an object O i1, ……….. , in : an object identifier or a base object • The state of a collection object • { i1 , ……….. , in } ij : the identifier of an object in the collection

  20. Object i22 Object i2 Object i4 Last Smith First John Last Brown First Jim Name Hardware Budget $1,000,000 Object i1 Object i21 Object i3 Name i2 Age 32 Address i3 Salary $32,000 Department i4 Name i22 Age 32 Address i23 Salary $34,000 Department i4 Street# 1212 StName Main City Walnut Creek State CA Zip 94596 Example(2) < objects in rectangular boxes >

  21. Example(3) i21 i1 Name Department Age Salary Add. i32 i2 i3 i4 i23 32 $34,000 32 $32,000 Last First Budget Name Smith John Hardware $1,000,000 Brown Jim < graphical representation of objects >

  22. Example(4) • Graphical representation of objects spouse i1 i101 spouse address address name name i4 i102 last first children children last first i3 * smith john smith mary < Object references with cycles >

  23. Implementation techniques for O-ID • Main concept • persistence: Objects are saved and accessible between programs • Implementation strategies • two dimensions to consider • Transient versus persistent object spaces • Address versus indirection strategies • Surrogate method

  24. Persistence(1) • Persistence in PLs • By saving files • By storing images • save the entire execution environment • PLs with record structures that have pointer-valued fields cannot be made persistent • Machine-address memory pointers are of no use outside the current execution of the program • Persistent PLs: PS-Algol, Pascal-R, etc

  25. Persistence(2) • Persistence in database • by providing schema • schema and instances of the types described in the schema is persistent • by the support of transactions • persistent objects are created and manipulated under transactions • persistency are related with atomicity and consistency • by the support of resiliency of data • recover transaction/system/media failures

  26. Implementation considerations of O-ID(1) • Transient vs. Persistent object spaces • Most languages are based on transient object spaces • ADA, C++, Smalltalk supports only the transient object space • Reasons for supporting persistent object space • to support persistent DB or PL • to provide access to much larger object space

  27. Implementation consideration of O-ID(2) • Address vs. Indirection • address-based solutions • virtual memory address • secondary storage address • structured name in a distributed environment • indirection-based solutions • memory-resident table • index for secondary storage-resident objects • tradeoff • the ease & flexibility of object movements v.s. overhead of accessing components of objects

  28. Indirection through Object Table • Earlier SmallTalk (only for memory-resident objects) • Involves an object table of starting addresses of objects • Mapping: object pointer --> memory address • Disadvantage • extra memory access and processing overhead • Advantage • flexibility of object movement • important in garbage collection

  29. OID with Table Mapping • Index to one table row 1 2 k OID

  30. Identity through Address Schemes • Use disk address/virtual address as O-ID • dual representation (o-id in disk, o-id in memory) • object swizzling (check each pointer variable and replace) • secondary storage resident persistent object • Use RID (Record Identifier)/virtual address as O-ID • [disk page number, slot/line number] • partial indirection • Use structured-identifier as O-ID • is similar to the naming of conventions for paths in OS • is useful in distributed file system (LOCUS)

  31. RID RID Example • <PAGE, SLOT>

  32. Surrogate Method • Address schemes cannot provide location independence • Surrogate: System-generated globally unique ID • persistent • use indirection (mapping table) to be mapped to objects • location, data independence: as long as the surrogate is stored together with the object, the object can be freely moved, copied, replicated, fragmented, and so on!!! • Completely independent from object state or object space • More comprehensive than tuple identifier

  33. Example Name Age Address surrogate Last String First String Street Name surrogate Street # surrogate S4 John 32 1212 Main S1 ………. S2 Smith < Identifiers through surrogates >

  34. Operations with Object Identity • Equality predicates • Identical predicate(Identity equal) • Shallow equality predicate(shallow-equal) • Deep equality predicate(deep-equal) • Copy operations • shallow/deep copy • Merge and swap operations

  35. Identity Predicate (Identity Equal) • Corresponds to the equality of reference or pointers in conventional PL • checks whether the OIDs are the same • “==“ ( in Simula & Smalltalk )

  36. : O2 O1: i1 i101 Age Address Name Name Age i4 Mary 32 John 32 Zip City State Street # Street Name CA 94596 1212 Main Walnut Creek Example : O3 i21 Age Address Name i23 Jim 32 Zip City State Street # Street Name CA 94596 1212 Main Walnut Creek O1.Address = = O2.Address (TRUE) O3.Address = = O1.Address (FALSE) O3.Address = = O2.Address (FALSE)

  37. Shallow-equality predicate (shallow-equal) • Two objects are shallow-equal if their states or contents are identical • One-level deep equal • Symbol: “ = “

  38. i5 i6 Name Name Age Age Tom Ann 5 10 Example(1) : O2 O1: i2 i1 Age Children Children Name Name Age Mary 32 John 32 i3 i4 O1.Children == O2.Children (FALSE: not identical) O1.Children = O2.Children (TRUE: shallow equal)

  39. Example(2) REC2 i2 REC1 i1 UpperRight LowerLeft UpperRIght LowerLeft i5 i6 X Y Y X 2 4 8 6 REC1 = REC2 (TRUE: shallow equal) REC1.LowerLeft = = RECT2.LowerLeft (TRUE: identical equal) REC1.UpperRight = = REC2.UpperRight(TRUE: identical equal)

  40. Deep equality predicate (deep-equal) • Compares the contents of corresponding base objects • two flavors of deep equality • a weaker deep equality (deep-equal) • check only for equality of a corresponding base object • a stronger deep equality (iso-deep-equal) • check weak deep equality & the isomorphism of the graphs of the objects

  41. 3 2 4 6 3 2 4 6 3 2 3 2 4 6 Example ARRAY1 i1 ARRAY1 deep-equal ARRAY2 ARRAY2 deep-equal ARRAY3 ARRAY1 deep-equal ARRAY3 ARRAY1 iso-deep-equal ARRAY2 < TRUE > ARRAY1 iso-deep-equal ARRAY3 ARRAY2 iso-deep-equal ARRAY3 < FALSE > i2 i3 ARRAY2 i4 i6 i5 ARRAY3 i7 i8 i9 i10 Deep equal arrays

  42. Copy Operations • Shallow copy • create a new object that will have instance variables with valuesidentical to the instance variables of the target object • Deep copy • create a new that will have instance variables with entirely new values such that the new object is deep equal to the target object

  43. Merging and Swapping • Merging • is very useful in statistical DBs when an attempt is made to merge information that was gathered by different sources ( record linking ) • Swapping • two objects are interchanged • e.g. O1 becomes O2 ( in Smalltalk )

  44. O1 O2 O4 O5 O1 O2 O4 a b c O5 f a b c g f g O3 O3 0 O6 0 d e d e d e 1 2 1 2 1 2 Example(1) • Merge merge(O3,O6)

  45. Object i22 Object i2 Last Smith First John Last Brown First Jim Object i1 Object i21 Name i2 Age 32 Address i3 Salary $32,000 Department i4 Name i22 Age 32 Address i23 Salary $34,000 Department i4 Object i3 Object i23 Street# 1212 StName Main City Walnut Creek State CA Zip 94596 Street# 3245 StName Oak Grove City Concord State CA Zip 94598 Example(2) < Before Swapping >

  46. Object i22 Object i2 Last Smith First John Last Brown First Jim Object i1 Object i21 Name i2 Age 32 Address i3 Salary $32,000 Department i4 Name i22 Age 32 Address i23 Salary $34,000 Department i4 Object i23 Object i3 Street# 1212 StName Main City Walnut Creek State CA Zip 94596 Street# 3245 StName Oak Grove City Concord State CA Zip 94598 < After Swapping : John’s Address becomes Jim’s Address >

  47. Summary • Object Identity • Location and address independence • Users should be able to share objects referentially • One of most natural oo modeling primitives • The surrogate method is known as the best implementation technique

More Related