Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Encapsulation CSC 422 Dr. Spiegel.

Similar presentations


Presentation on theme: "Object Encapsulation CSC 422 Dr. Spiegel."— Presentation transcript:

1 Object Encapsulation CSC 422 Dr. Spiegel

2 Outline Object Encapsulation Ownership Types

3 Encapsulation Level O raw lines of code Level 1 the procedural module
class/object structure

4 Higher Levels of Encapsulation
Packages and components (groupings of classes with only some of their interfaces visible externally Level -4 Compositions Design trade offs become more complex as encapsulation becomes more complex

5 ~ ~ Object Encapsulation
Consider a Set object s implemented using a Vector object v s o ~ ~ v Local reasoning about s is possible: If objects outside s do not access v v is encapsulated within s

6 Encapsulation In general, all objects that s depends on must be encapsulated within s s depends on x if mutations of x affect behavior of s

7 Iterators and Encapsulation
Iterators require access to representation i s v Okay if violations of encapsulation limited to the same module

8 Ownership Types Goal is to enforce encapsulation statically
v Programmer can declare s owns v System ensures v is encapsulated in s

9 Ownership Types Every object has an owner
world Every object has an owner Owner can be another object or world Who owns main? Ownership relation forms a tree Owner of an object cannot change

10 Ownership Types for Encapsulation
If an object owns objects it depends on the type system enforces encapsulation If v is inside s and o is outside, o cannot access v s o ~ ~ v

11 Ownership Types world

12 TStack Example (No Owners)
class TStack { TNode head; void push(T value) {…} T pop() {…} } class TNode { TNode next; T value; class T {…} TStack head value next value next value next TNode T Can Tstack access Tnodes? Can a Tnode access T’s data?

13 TStack Example (With Owners)
class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T Who owns the nodes? Classes are parameterized with owners

14 TStack Example class TStackstackOwner, TOwner {
TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T First owner owns the “this” object

15 TStack Example class TStackstackOwner, TOwner {
TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T TStack owns the “head” TNode

16 TStack Example class TStackstackOwner, TOwner {
TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T The “next” TNode has the same owner as the “this” TNode All TNodes have the same owner

17 TStack Example class TStackstackOwner, TOwner {
TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; Client TStack TNode T s1 is an encapsulated stack with encapsulated elements

18 TStack Example class TStackstackOwner, TOwner { world
TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; world Client TStack TNode T s2 is an encapsulated stack with public elements

19 TStack Example class TStackstackOwner, TOwner { world
TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; world TStack TNode T s3 is a public stack with public elements

20 TStack Example class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; TStackworld, this s4; // illegal TStack TNode T Other owners must be same as or more public than first owner This constraint is necessary to enforce encapsulation with subtyping

21 Constraints on Owners class ClientcOwner, sOwner, tOwner where (sOwner <= tOwner) { TStacksOwner, tOwner head; } This is legal only if tOwner is same as or more public than sOwner

22 Iterators Consider an Iterator i over Stack s
If i is encapsulated within s Then i cannot be used outside s If i is not encapsulated within s Then i cannot access representation of s TStack TStackEnum

23 Solution Use inner classes Gives desired access to representation
Also satisfies modularity goal

24 Iterators TStack TStackEnum class TStackstackOwner, TOwner {
TNodethis, TOwner head; …. class TStackEnumenumOwner, TOwner implements TEnumenumOwner, TOwner { TNodeTStack.this, TOwner current = TStack.this.head; } Inner class objects can access rep of outer class objects TStack TStackEnum

25 Ownership Types for Encapsulation
If an object owns objects it depends on, then the type system enforces encapsulation If v is inside s and o is outside o cannot access v, unless o is an inner class object of s s o ~ ~ What must be true of o so we can declare an instance of o? v


Download ppt "Object Encapsulation CSC 422 Dr. Spiegel."

Similar presentations


Ads by Google