Title: A Seminar on Encapsulation http:www'cs'tau'ac'ilmsagivcoursesencapsulate'html
1A Seminar on Encapsulationhttp//www.cs.tau.ac.il
/msagiv/courses/encapsulate.html
- Mooly Sagiv
- Schriber 317
- msagiv_at_post
- Office Hours Wed. 14-15
-
Noam Rinetzky
2Outline
- General information
- Seminar subject
- How to give a presentation
3General Information
- Prerequisites
- Compilers Program Analysis Program
VerificationSemantics Principles of OO - Requirements
- Select a topic (Monday 7/11)
- Read introductory material
- Participate in 10 seminar talks (2 lectures)
- Present a paper
4Tentative Schedule
- November 3 Intro
- November 10 Separation Logic
- Student lectures
- February 2 Summary
5What is encapsulation?
6Common Answer(1)
- Encapsulation is the ability of an object to
place a boundary around its properties (ie. data)
and methods (ie. operations) - Programs written in older languages suffered
from side effects where variables sometimes had
their contents changed or reused in unexpected
ways. Some older languages even allowed branching
into procedures from external points - This led to 'spaghetti' code that was difficult
to unravel, understand and maintain - Encapsulation is one of three basic principles
within object oriented programming languages
7Common Answer(2)
- Object variables can be hidden completely from
external access - These private variables can only be seen or
modified by use of object accessor and mutator
methods - Access to other object variables can be allowed
but with tight control on how it is done - Methods can also be completely hidden from
external use - Those that are made visible externally can only
be called by using the object's front door (ie.
there is no 'goto' branching concept).
8Is it that simple?
- Does private guarantee encapsulation?
9Encapsulation by Example
- main()
- Square sq new Square(2)
- assert( 0lt sq.circumference())
-
- main()
- Square sq new Square(2)
- sq.len -2
- assert( 0 lt sq.circumference())
- class Square
- int len
- Square(int l)
- assert(0 lt l)
- len l
-
- int circumference()
- return 4len
-
OK
oops
10Importance of Encapsulation (Information Hiding)
- main()
- Square sq new Square(2)
- assert( 0lt sq.circumference())
-
- main()
- Square sq new Square(2)
- sq.len -2
- assert( 0 lt sq.circumference())
- class Square
- private int len
- Square(int l)
- assert(0ltl)
- len l
-
- int circumference()
- return 4len
-
OK
Oh, no!
11Importance of Encapsulation (Information Hiding
Restricted Mutations)
- main()
- Square sq new Square(2)
- assert( 0lt sq.circumference())
-
- main()
- Square sq new Square(2)
- sq.set(3)
- assert( 0 lt sq.circumference())
-
- main()
- Square sq new Square(2)
- sq.set(-3)
- assert( 0 lt sq.circumference())
-
- class Square
- private int len
- Square(int l)
- assert(0 lt l)
- len l
-
- int circumference()
- return 4len
-
- set(int l)
- assert(0ltl)
- len l
-
OK
OK
Oh, no!
12Importance of Encapsulation (Representation
Independence)
- main()
- Square sq new Square(2)
- assert(0 lt sq.circumference())
-
- main()
- Square sq new Square(2)
- assert(0 lt sq.len())
-
- class Square
- private int circu
- Square(int l)
- assert(0 lt l)
- circu 4 l
-
- int circumference()
- return circu
-
- int setLen(int l)
- assert( 0 lt l) circu l4
-
- Int len() return circu / 4
OK
OK
13Encapsulation of objects
- Example class frame
- A Frame is comprised of two squares
- An outer square
- An inner square
-
14Encapsulation of (sub)objects
- Main()
- Square small new Square(1)
- Square big new Square(3)
-
- Frame frm new Frame(small, big)
-
- assert(0 lt frm.size())
- small.setLen(5)
-
- assert(0 lt frm.size())
- class Frame
- private Square outer, inner
- Frame(Square i, Square o)
- assert(i.len() lto.len())
- outer o
- inner i
-
- int size()
- return outer.len() outer.len()
- inner.len() inner.len()
-
OK
oops
15Deep heaps
- Class IntList
- int size
- IntElem h
- List()
- this.size 0
- this.h null
-
- void insert(IntElem e)
- this.size
- if (this.h null)
- this.h e
- else
- this.h.add(e)
-
-
- Class IntElem
- int data
- IntElem n
- IntElem(int d)
- this.data d
-
- void add(IntElem e)
- if (this.n null)
- this.n e
- else
- this.n.add(e)
-
16Deep heaps
- main()
- List x new List()
- List y new List()
-
- IntElem x1 new IntElem(1)
- x.add(x1)
- IntElem y1 new IntElem(2)
- y.add(y1)
- IntElem z new IntElem(3)
- x.add(z)
- y.add(z)
- IntElem w new IntElem(4)
- y.add(w)
size0
size1
size2
size0
size1
size2
size3
17Modification via Iterators
- class List
- Elem h
- List()
- this.h null
-
-
- void add (Object d)
- Elem e new Elem(d)
- if (this.h null)
- this.h elem
- else
- this.h.add(d)
-
-
- Iterator iterator()
- return new Iterator(this)
-
-
- class Elem
- Object o
- Elem n
-
-
- Class Iterator
- List c
- Elem e
-
- Iterator(List lst)
- this.c lst
- this.e lst.h
-
18Modification via Iterators
- Main()
- List list new List()
- list.add(new Square(1))
- list.add(new Square(2))
- list.add(new Square(3))
- Iterator itr1 list.iterator()
- Iterator itr2 list.iterator()
- itr1.remove()
- Square s (Square) itr2.next()
19Modification via Iterators
- Main()
- List list new List()
- list.add(new Square(1))
- list.add(new Square(2))
- list.add(new Square(3))
- Iterator itr1 list.iterator()
- Iterator itr2 list.iterator()
- itr1.remove()
- Square s (Square) itr2.next()
itr2
itr1
c
c
e
e
h
n
n
o
3
h
20Encapsulation Goals
- Develop a programming language
- Expressive and convenient
- Allow modular reasoning
- Controlled side-effects
- Allow library updates
- Representation independence
- Secure
- Easier program optimization
- Easier concurrent programming
Is it possible?
Semantic properties
21Modular Assume/GuaranteeReasoning
Class
- Verifier
- Correct API usage
- Correct implementation
Specification
22Idea 1 Ownership
- Define a binary relation on objects
- a owns b
- Only methods of the owner can access fields
- Ownership can be enforced by a type system
- Restricts programming model
- Restricts aliasing/sharing
23Ownership (Clarke Drossopoulo)
class Main ltgt Listltthis, worldgt list
Main() writes this list new Listltthis,
worldgt void populate() writes under
(this.1) list add(new Dataltworldgt)
list.add(new Dataltworldgt) static void main()
writes under world Main main new
Mainltgt main.populate()
class List ltowner, datagt Link ltthis,datagt
head void add(Data ltdatagt d) writes
under(this) head new Linkltthis,
datagt(d, head)
24Object Graph
World
25Reasoning about programs using ownership
Listltp,worldgt list1 Listltq,worldgt list2 for
(i 0 i lt 10 i) list1.add(new
Dataltworldgt(i)) // exp1 for (i
0 i lt 10 i) list2.add(new
Dataltworldgt(i)) // exp2
- Questions
- Are list1 and list2 aliases?
- Do exp1 and exp2 interfere?
- Can the loops be fused?
26Other concepts
- Other ownership models
- SPEC C
- Object oriented effect system
- Based on uniquness
- Alias types
- Islands
- Separation logic (next week)
27Summary
- Everybody agrees that encapsulation is desired
- But no agreement on the exact definition
28Related Issues
- Sharing
- Aliasing
- Class (object) invariant
- Modularity
- Confinment
29Giving a presentation
30How to give a presentation
- What to say and how to say it
- Getting through the audience
- Visual aids
31What to say and how to say it
- Communicate the Key Ideas
- Dont get bogged down in Details
- The best talk make you read the paper
- Structure your talk
- Use Top-Down approach
- Introduction
- Body
- Technicalities
- The Conclusion
Use Examples
32Introduction
- Define the problem
- Motivate the audience
- Introduce terminology
- Discuss earlier work
- Emphasize the contributions
- Provide a road map
Use Examples
33The body
- Abstract the major results
- Explain the significance of the results
- Explain the main techniques
- Use enlightening examples
- Demonstrations are welcome
34Technicalities
- Expert only part
- Show something really interesting beyond the
paper/tool
35The Conclusion
- Hindsight is clearer than Foresight
- Give open problems/further work
- Indicate that your talk is over
36Know your audience
37Getting through the Audience
- Use Repetitions
- Remind, dont assume
- Dont over-run
- Maintain Eye Contact
- Control your voice
- Control your motion
- Take care of your appearance
38Visual Aids
- PowerPoint transparencies
- Dont overload transparencies
- Dont use too many transparencies
- Use Overlays Properly
- Use Color Effectively
- Use Pictures and Tables
- The blackboard can be used too
39Dont overload transparencies
- The input of the program can be arbitrary.
- Let x be a prime number, i.e., all the numbers
zltx do not divide x. y be the next prime number,
i.e., etc.
- Arbitrary input
- Prime number x
- The next prime y
40Use overlays (im)properly
- Item 1
- Item 1.1
- Item 1.2
- Item 2
- Item 2.1
- Item 2.1.1
41Use colors properly
42The End
http//www.cs.tau.ac.il/msagiv/courses/encapsulat
e.html