530 likes | 625 Views
Object-Orientation by Khoshafian and Abnous. Chap.4 Object Identity. 서울대학교 컴퓨터공학부 Internet Data Base 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
E N D
Object-Orientation by Khoshafian and Abnous Chap.4 Object Identity 서울대학교 컴퓨터공학부 Internet Data Base 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 • 6. Operations with Identity • 7. Summary
Introduction • Object Identity( Khoshafian & 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
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
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
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
Referencing Objects in PLs (2) • Pointers • Virtual memory addresses • Dynamic memory allocation • 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 • Low-level concept within high-level PL
Pointer 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!
C1 3.5 Real 2.7 C2 Free Space < Before dispose(C1) > < After dispose(C1) > 3.5 C1 2.7 Imaginary C2 Free Space
Pointer 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
Pointer 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 But now “CompareComplex(C1,C3)” is true < When record equality is not supported in a PL, a function to implement complex number object equality is needed >
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
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
Path Name Example (1) Items Computer TV Refrigerator Hard Soft Print Database Word-Proc SpreadSheet HP EPSON DeskJet 660 LaserJet 4L Items/Computer/Hard/Print/EPSON
Path Name 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 • unlink 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)
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
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
Database Key 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
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
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
Intuitive Example for the need of 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!!!
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
Equality and Object State • == 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
Object i22 Object i2 Object i4 Last Smith First John Last Brown First Jim Name Hardware Budget $1,000,000 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 Street# 1212 StName Main City Walnut Creek State CA Zip 94596 < objects in rectangular boxes >
Graphical representation of objects (1) 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 (2) spouse i1 i101 spouse address address name name i4 i104 children children i102 last first last first i3 * smith john smith mary < Object references with cycles >
Implementation techniques for O-ID • Main concept: persistence • Objects are saved and accessible between programs • Implementation strategies • Two dimensions to consider • Transient versus persistentobject spaces • Address versus indirectionstrategies • Surrogate method
Supporting 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.
Supporting 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
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
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
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
OID with Table Mapping • Index to one table row OID 1 2 k
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 (RecordIdentifier)/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)
RID O-ID through RID Example • <PAGE, SLOT>
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
Surrogate 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 >
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
Operations with Object Identity • Equality predicates • Identical predicate (identity equal test) • Shallow equality predicate (shallow-equal test) • Deep equality predicate (deep-equal test) • Copy operations • Shallow copy vs Deep copy • Merge and swap operations
Identity Predicate (Identity Equal Test) • Corresponds to the equality of reference or pointers in conventional PL • checks whether the OIDs are the same • “==“ ( in Simula & Smalltalk )
: 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 Identity-Equality Test 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)
Shallow-Equality Predicate (Shallow-equal Test) • Two objects are shallow-equal if their states or contents are identical • One-level deep equal • Symbol: “ = “
i5 i6 Name Name Age Age Tom Ann 5 10 Shallow-Equality Test 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)
Shallow-Equality Test 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)
Deep-Equality Predicate (Deep-Equal Test) • 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
Deep-Equality Test Example 3 2 4 6 3 2 4 6 3 2 3 2 4 6 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
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
Merging and Swapping • Merging operation • is very useful in statistical DBs when an attempt is made to merge information that was gathered by different sources ( record linking ) • Swapping Operation • two objects are interchanged • e.g. O1 becomes O2( in Smalltalk )
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 Merge Example merge(O3,O6)
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 SWAP Example (1) < Before Swapping >