Upload
sara-casey
View
30
Download
0
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
1
Chap.4 Object Identity
서울대학교 컴퓨터공학부Internet Data Base Lab
교수 김 형 주
Object-Orientation by Khoshafian and AbnousObject-Orientation by Khoshafian and Abnous
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 IdentityObject 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
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
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
6
Referencing Objects in PLs
Referencing mechanisms Earliest PLs of the 1950s, FORTRAN, COBOL
variable name object sharing through the COMMON & EQUIVALENCE co
nstructs
Algol 68, C, Pascal, etc. pointer (object reference, ref constuct) heap management
7
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
8
Pointer Example in Pascal (1)
TYPE ComplexPtr = ^Complex
Complex = RECORDReal : real;Imaginary : real;
END
new(C1);C1^.Real := 3.5;C1^.Imaginary := 2.7;C2 := C1;
< 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!
** Dispose(C1) cannot resolve the dangling reference problem
** What is the meaning of “Dispose(C1)”?
** What if the user access C2!
9
3.5
2.7 Imaginary
Free
Space
< After dispose(C1) >
3.5
2.7
RealC1
C2
Free
Space
C1C1
C2C2
< Before dispose(C1) >
10
Pointer Example in Pascal (2)
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^
< TRUE equality predicates>
C1 = C3< FALSE equality predicates>
** The equality predicates: = in Pascal, == in C ** Pascal and C do not support record equality, but only pointer equality
** The equality predicates: = in Pascal, == in C ** Pascal and C do not support record equality, but only pointer equality
11
Pointer Example in Pascal (3)FUNCTION CompareComplex (C1, C2 : Complex) : boolean;begin
if C1.Real = C2.Real and C1.Imaginary = C2.Imaginarythen CompareComplex := TRUEelse 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 >
12
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
13
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
14
Path Name Example (1)
Items
Computer TV Refrigerator
Hard Soft
HP
DeskJet 660 LaserJet 4L
EPSON
Database Word-Proc SpreadSheet
Items/Computer/Hard/Print/EPSON
15
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)
16
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
17
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
18
Database Key Example
Last NameLast Name First NameFirst Name AgeAge AddressAddress
AdamsBrownRipperSilvermanSmithSmith
AdamsBrownRipperSilvermanSmithSmith
TimJimJackLeoJohnMary
TimJimJackLeoJohnMary
233270343232
233270343232
“12 Sutton …..”“43 Doloney ……”“1 London …….”“55 H Street ….”“1212 Main ……”“1212 Main ……”
“12 Sutton …..”“43 Doloney ……”“1 London …….”“55 H Street ….”“1212 Main ……”“1212 Main ……”
Identifier KeyIdentifier Key
Person tablePerson table
19
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
20
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
21
Intuitive Example for the need of O-ID
Name: [Last: “Smith , First: “John” ]Age: 32Address: [Street #: 1212
Street name: “Main” City: Walnut Creek
State: California Zip: 94596 ]
Name: [Last: “Smith , First: “John” ]Age: 32Address: [Street #: 1212
Street name: “Main” City: Walnut Creek
State: California Zip: 94596 ]
Name: [Last: “Smith, First: “Mary” ]Age: 32Address: [Street #: 1212
Street name: “Main” City: Walnut Creek
State: California Zip: 94596 ]
Name: [Last: “Smith, First: “Mary” ]Age: 32Address: [Street #: 1212
Street name: “Main” City: Walnut Creek
State: California Zip: 94596 ]
JohnSmith ChangeAddress: NewAddressJohnSmith ChangeAddress: NewAddress
Consistency problem Consistency problem
“John Smith”“John Smith”“Mary Smith”“Mary Smith”
** Replication-based solution & Key-based solution
** O-ID solution is more efficient and reasonable!!!
** Replication-based solution & Key-based solution
** O-ID solution is more efficient and reasonable!!!
22
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
23
Equality and Object State
== to check for identical objects (pointer equality)
= to check for equality of object states (content equality)
== to check for identical objects (pointer equality)
= to check for equality of object states (content equality)
(e.g. A == B , A = B )(e.g. A == B , A = B )
Object State Object State
AI1, …….. , Ain : instance variables of an object Oi1, ……….. , in : an object identifier or a base object
AI1, …….. , Ain : instance variables of an object Oi1, ……….. , in : an object identifier or a base object
The state of an object O : AI1 : i1 , ……….. , AIn : in The state of an object O : AI1 : i1 , ……….. , AIn : in
The state of a collection object: { i1 , ……….. , in } The state of a collection object: { i1 , ……….. , in }
ij : the identifier of an object in the collectionij : the identifier of an object in the collection
24
Object i1Object i1
Name i2Age 32Address i3Salary $32,000Department i4
Name i2Age 32Address i3Salary $32,000Department i4
Object i21Object i21
Name i22Age 32Address i23Salary $34,000Department i4
Name i22Age 32Address i23Salary $34,000Department i4
Object i2Object i2
Last SmithFirst John
Last SmithFirst John
Object i3Object i3
Street# 1212StName MainCity Walnut CreekState CAZip 94596
Street# 1212StName MainCity Walnut CreekState CAZip 94596
Object i22Object i22
Last BrownFirst Jim
Last BrownFirst Jim
Object i4Object i4
Name HardwareBudget $1,000,000
Name HardwareBudget $1,000,000
< objects in rectangular boxes >< objects in rectangular boxes >
25
i1i1 i21i21
i2i2
3232 i3i3
$32,000$32,000
LastLastFirstFirst
NameNameAgeAge
Add.Add. SalarySalary
SmithSmith JohnJohn
i4i4
NameName BudgetBudget
DepartmentDepartment
$34,000$34,000 i23i233232
BrownBrown JimJim
i32i32
HardwareHardware $1,000,000$1,000,000
Graphical representation of objects (1)Graphical representation of objects (1)
26
*
spouse
spouse
name
firstlast
smith mary
address address
children children
i101
i102
i3
i1
i4name
last first
smith john
< Object references with cycles >< Object references with cycles >
Graphical representation of objects (2)Graphical representation of objects (2)
i104
27
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
28
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 c
annot be made persistent Machine-address memory pointers are of no use outside th
e current execution of the program Persistent PLs: PS-Algol, Pascal-R, etc.
29
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
30
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
31
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
32
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
34
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)
36
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 st
ored 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
37
Surrogate Example
surrogatesurrogate
surrogatesurrogate Last String
Last String
FirstString
FirstString
AgeAge
surrogatesurrogate Street #Street # StreetName
StreetName
NameNameAddressAddress
……….……….S1S1S2S2 SmithSmith JohnJohn 3232 S4S4
12121212 MainMain
< Identifiers through surrogates >< Identifiers through surrogates >
38
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
39
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
40
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 )
41
Identity-Equality Test Example
O1:O1:i1i1
NameNameAgeAge
AddressAddressNameName
AgeAge
Street #Street #Street NameStreet Name
CityCityStateState ZipZip
12121212 MainMain
Walnut CreekWalnut CreekCACA 9459694596
i4i4
i101i101 : O2: O2
JohnJohn3232 MaryMary
3232
AddressAddressNameName
AgeAge
Street #Street #Street NameStreet Name
CityCityStateState ZipZip
12121212 MainMain
Walnut CreekWalnut CreekCACA 9459694596
i23i23
i21i21 : O3: O3
JimJim3232
O1.Address = = O2.Address (TRUE) O3.Address = = O1.Address (FALSE) O3.Address = = O2.Address (FALSE)
O1.Address = = O2.Address (TRUE) O3.Address = = O1.Address (FALSE) O3.Address = = O2.Address (FALSE)
42
Shallow-Equality Predicate (Shallow-equal Test)
Two objects are shallow-equal if their states or contents are identical
One-level deep equal Symbol: “ = “
43
Shallow-Equality Test Example(1)
O1:O1:i1i1
NameNameAgeAge
JohnJohn3232
ChildrenChildrenNameName AgeAge
i4i4
i2i2 : O2: O2
MaryMary3232
ChildrenChildren
i3i3
i5i5i6i6
TomTom1010 AnnAnn 55
NameName AgeAge NameNameAgeAge
O1.Children == O2.Children (FALSE: not identical)O1.Children = O2.Children (TRUE: shallow equal)
O1.Children == O2.Children (FALSE: not identical)O1.Children = O2.Children (TRUE: shallow equal)
44
Shallow-Equality Test Example (2)
i5i5i6i6
2244 88 66
XX YY XXYY
REC1 i1REC1 i1 REC2 i2REC2 i2
LowerLeftLowerLeftUpperRightUpperRight
LowerLeftLowerLeft UpperRIghtUpperRIght
REC1 = REC2 (TRUE: shallow equal)REC1.LowerLeft = = RECT2.LowerLeft (TRUE: identical equal)REC1.UpperRight = = REC2.UpperRight(TRUE: identical equal)
REC1 = REC2 (TRUE: shallow equal)REC1.LowerLeft = = RECT2.LowerLeft (TRUE: identical equal)REC1.UpperRight = = REC2.UpperRight(TRUE: identical equal)
45
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 obj
ect a stronger deep equality (iso-deep-equal)
check weak deep equality & the isomorphism of the graphs of the objects
46
Deep-Equality Test ExampleDeep-Equality Test Example
3 2 4 6
3 23 2
3 2 4 6
4 6
Deep equal arraysDeep equal arrays
ARRAY1 i1
ARRAY1 i1
ARRAY2 i4
ARRAY2 i4
ARRAY3 i7
ARRAY3 i7
i2i2i3i3
i5i5 i6i6
i8i8 i9i9i10i10
ARRAY1 deep-equal ARRAY2ARRAY2 deep-equal ARRAY3ARRAY1 deep-equal ARRAY3
ARRAY1 iso-deep-equal ARRAY2
< TRUE >
ARRAY1 iso-deep-equal ARRAY3ARRAY2 iso-deep-equal ARRAY3
< FALSE >
ARRAY1 deep-equal ARRAY2ARRAY2 deep-equal ARRAY3ARRAY1 deep-equal ARRAY3
ARRAY1 iso-deep-equal ARRAY2
< TRUE >
ARRAY1 iso-deep-equal ARRAY3ARRAY2 iso-deep-equal ARRAY3
< FALSE >
47
Copy Operations
Shallow copy create a new object that will have instance
variables with values identical 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
48
Merging and Swapping
Merging operation is very useful in statistical DBs when an attempt is m
ade to merge information that was gathered by different sources ( record linking )
Swapping Operation two objects are interchanged
e.g. O1 becomes O2 ( in Smalltalk )
49
Merge Example
O2O1
O3
a b c
d e
0
1 2
merge(O3,O6)
O5
d e
1 2
g
O4
f
O6
O5f g
O4O2O1
O3
a b c
d e
0
1 2
50
SWAP Example (1)
Object i1Object i1
Name i2Age 32Addressi3Salary $32,000Department i4
Name i2Age 32Addressi3Salary $32,000Department i4
Object i21Object i21
Name i22Age 32Addressi23Salary $34,000Department i4
Name i22Age 32Addressi23Salary $34,000Department i4
Object i2Object i2
Last SmithFirst John
Last SmithFirst John
Object i3Object i3
Street# 1212StName MainCity Walnut CreekState CAZip 94596
Street# 1212StName MainCity Walnut CreekState CAZip 94596
Object i22Object i22
Last BrownFirst Jim
Last BrownFirst Jim
< Before Swapping >< Before Swapping >
Object i23Object i23
Street# 3245StName Oak GroveCity ConcordState CAZip 94598
Street# 3245StName Oak GroveCity ConcordState CAZip 94598
51
Object i1Object i1
Name i2Age 32Addressi3Salary $32,000Department i4
Name i2Age 32Addressi3Salary $32,000Department i4
Object i21Object i21
Name i22Age 32Addressi23Salary $34,000Department i4
Name i22Age 32Addressi23Salary $34,000Department i4
Object i2Object i2
Last SmithFirst John
Last SmithFirst John
Object i23Object i23
Street# 1212StName MainCity Walnut CreekState CAZip 94596
Street# 1212StName MainCity Walnut CreekState CAZip 94596
Object i22Object i22
Last BrownFirst Jim
Last BrownFirst Jim
< After Swapping : John’s Address becomes Jim’s Address >< After Swapping : John’s Address becomes Jim’s Address >
Object i3Object i3
Street# 3245StName Oak GroveCity ConcordState CAZip 94598
Street# 3245StName Oak GroveCity ConcordState CAZip 94598
SWAP Example (2)
52
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