47
1 Chap.4 Object Identity Chap.4 Object Identity 서서서서서 서서서서서서 서서서서서서서서서서 snu oopsla lab 서서 서 서 서 Object-Orientation by Khoshafian and Abnous

Chap.4 Object Identity

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Chap.4   Object Identity

1

Chap.4 Object IdentityChap.4 Object Identity

서울대학교 컴퓨터공학부객체지향시스템연구실

snu oopsla lab

교수 김 형 주

Object-Orientation by Khoshafian and AbnousObject-Orientation by Khoshafian and Abnous

Page 2: Chap.4   Object Identity

2

ContentsContents

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

Page 3: Chap.4   Object Identity

3

IntroductionIntroduction

Object IdentityObject 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

Page 4: Chap.4   Object Identity

4

Ways of Accessing ObjectsWays 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

Page 5: Chap.4   Object Identity

5

Referencing Objects in PLsReferencing Objects in PLs

Referencing mechanisms Earliest PLs of the 1950s, FORTRAN, COBOL

variable name object sharing through the COMMON & EQUIVALENCE constr

ucts Algol 68, C, Pascal, etc.

pointer (object reference, ref constuct) heap management

Page 6: Chap.4   Object Identity

6

Referencing Objects in PLs (2)Referencing Objects in PLs (2)

Pointers Virtual memory addresses Dynamic memory allocation (low level computational c

ontruct 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

Page 7: Chap.4   Object Identity

7

Example in Pascal (1)Example in Pascal (1)

TYPE ComplexPtr = ^Complex Complex = RECORD

Real : 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!

Page 8: Chap.4   Object Identity

8

3.5

2.7 Imaginary

Free

Space

< After dispose(C1) >

3.5

2.7

RealC1

C2

< Before dispose(C1) >

Free

Space

C1C1

C2C2

Page 9: Chap.4   Object Identity

9

Example in Pascal (2)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

Page 10: Chap.4   Object Identity

10

Example in Pascal (3)Example in Pascal (3)FUNCTION CompareComplex (C1, C2 : Complex) : boolean;begin

if C1.Real = C2.Real and C1.Imaginary = C2.Imaginarythen

CompareComplex := TRUEelse

CompareComplex := FALSEend; {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 >

Page 11: Chap.4   Object Identity

11

Object Naming in OSObject 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

Page 12: Chap.4   Object Identity

12

Examples(1)Examples(1)

Items

Computer TV Refrigerator

Hard Soft

Print

HP

DeskJet 660 LaserJet 4L

EPSON

Database Word-Proc SpreadSheet

Items/Computer/Hard/Print/EPSON

Page 13: Chap.4   Object Identity

13

Example(2)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)

Page 14: Chap.4   Object Identity

14

Identity Through Identifier KeysIdentity 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

Page 15: Chap.4   Object Identity

15

ExampleExample

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

Page 16: Chap.4   Object Identity

16

Type/State/Identity TrichotomyType/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

Page 17: Chap.4   Object Identity

17

Intuitive Example for O-IDIntuitive Example for 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!!!

Page 18: Chap.4   Object Identity

18

Object Spaces with IdentityObject 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

Page 19: Chap.4   Object Identity

19

Example(1)Example(1)

== 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

Page 20: Chap.4   Object Identity

20

Example(2)Example(2)

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 >

Page 21: Chap.4   Object Identity

21

Example(3)Example(3)

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 >< graphical representation of objects >

Page 22: Chap.4   Object Identity

22

Example(4)Example(4)

Graphical representation of objects

*

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 >

Page 23: Chap.4   Object Identity

23

Implementation techniques for O-IDImplementation 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

Page 24: Chap.4   Object Identity

24

Persistence(1)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

Page 25: Chap.4   Object Identity

25

Persistence(2)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 transactio

ns persistency are related with atomicity and consistency

by the support of resiliency of data recover transaction/system/media failures

Page 26: Chap.4   Object Identity

26

Implementation considerations of O-ID(1)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

Page 27: Chap.4   Object Identity

27

Implementation consideration of O-ID(2)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 acce

ssing components of objects

Page 28: Chap.4   Object Identity

28

Indirection through Object TableIndirection through Object Table

Earlier SmallTalk (only for memory-resident objects) Involves an object table of starting addresses of obje

cts Mapping: object pointer --> memory address Disadvantage

extra memory access and processing overhead

Advantage flexibility of object movement

important in garbage collection

Page 29: Chap.4   Object Identity

29

OID with Table MappingOID with Table Mapping

Index to one table row

1

2

k

OID

Page 30: Chap.4   Object Identity

30

Identity through Address SchemesIdentity 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)

Page 31: Chap.4   Object Identity

31

RID ExampleRID Example

<PAGE, SLOT>

RID

Page 32: Chap.4   Object Identity

32

Surrogate MethodSurrogate 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 tog

ether with the object, the object can be freely moved, copied, replic

ated, fragmented, and so on!!!

Completely independent from object state or object space

More comprehensive than tuple identifier

Page 33: Chap.4   Object Identity

33

ExampleExample

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 >

Page 34: Chap.4   Object Identity

34

Operations with Object IdentityOperations 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

Page 35: Chap.4   Object Identity

35

Identity Predicate (Identity Equal)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 )

Page 36: Chap.4   Object Identity

36

ExampleExample

O1:O1:

i1i1

NameName

AgeAge

AddressAddress

NameName

AgeAge

Street #Street #

Street NameStreet Name

CityCity

StateState Zip

Zip

12121212

MainMain

Walnut CreekWalnut Creek

CACA

9459694596

i4i4

i101i101 : O2

: O2

JohnJohn

3232 Mary

Mary32

32

AddressAddress

NameName

AgeAge

Street #Street #

Street NameStreet Name

CityCity

StateState Zip

Zip

12121212

MainMain

Walnut CreekWalnut Creek

CACA

9459694596

i23i23

i21i21 : O3

: O3

JimJim

3232

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)

Page 37: Chap.4   Object Identity

37

Shallow-equality predicate (shallow-equal)Shallow-equality predicate (shallow-equal)

Two objects are shallow-equal if their states or contents are identical

One-level deep equal Symbol: “ = “

Page 38: Chap.4   Object Identity

38

Example(1)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)

Page 39: Chap.4   Object Identity

39

Example(2)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)

Page 40: Chap.4   Object Identity

40

Deep equality predicate (deep-equal)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 obj

ects

Page 41: Chap.4   Object Identity

41

ExampleExample

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 >

Page 42: Chap.4   Object Identity

42

Copy OperationsCopy 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

Page 43: Chap.4   Object Identity

43

Merging and SwappingMerging 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 )

Page 44: Chap.4   Object Identity

44

Example(1)Example(1)

Merge

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

Page 45: Chap.4   Object Identity

45

Example(2)Example(2)

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

Page 46: Chap.4   Object Identity

46

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

Page 47: Chap.4   Object Identity

47

SummarySummary

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 imple

mentation technique