Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mooly Sagiv Schriber 317 Office Hours Wed

Similar presentations


Presentation on theme: "Mooly Sagiv Schriber 317 Office Hours Wed"— Presentation transcript:

1 Mooly Sagiv Schriber 317 msagiv@post Office Hours Wed. 14-15
A Seminar on Encapsulation Mooly Sagiv Schriber 317 Office Hours Wed Noam Rinetzky

2 Outline General information Seminar subject How to give a presentation

3 General Information Prerequisites Requirements
Compilers | Program Analysis| Program Verification| Semantics| Principles of OO Requirements Select a topic (Monday 7/11) Read introductory material Participate in 10 seminar talks (2 lectures) Present a paper

4 Tentative Schedule November 3: Intro November 10: Separation Logic
Student lectures February 2: Summary

5 What is encapsulation?

6 Common 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

7 Common 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).

8 Does private guarantee encapsulation?
Is it that simple? Does private guarantee encapsulation?

9 Encapsulation by Example
class Square { int len; Square(int l) { assert(0 < l); len = l; } int circumference() { return 4*len; main() { Square sq = new Square(2); assert( 0< sq.circumference()); } sq.len = -2; assert( 0 < sq.circumference()); OK oops

10 Importance of Encapsulation (Information Hiding)
class Square { private int len; Square(int l) { assert(0<l); len = l; } int circumference() { return 4*len; main() { Square sq = new Square(2); assert( 0< sq.circumference()); } sq.len = -2; assert( 0 < sq.circumference()); OK Oh, no!

11 Importance of Encapsulation (Information Hiding: Restricted Mutations)
class Square { private int len; Square(int l) { assert(0 < l); len = l; } int circumference() { return 4*len; set(int l) { assert(0<l); main() { Square sq = new Square(2); assert( 0< sq.circumference()); } sq.set(3); assert( 0 < sq.circumference()); sq.set(-3); OK OK Can still do mutation, but the class verifies that they are consistent Oh, no!

12 Importance of Encapsulation (Representation Independence)
class Square { private int circu; Square(int l) { assert(0 < l); circu = 4 * l; } int circumference() { return circu; int setLen(int l) { assert( 0 < l); circu = l*4; Int len() { return circu / 4;} main() { Square sq = new Square(2); assert(0 < sq.circumference()); } assert(0 < sq.len()); OK OK

13 Encapsulation of objects
Example: class frame A Frame is comprised of two squares An outer square An inner square Frame is 2 squares on in each other

14 Encapsulation of (sub)objects
Main() { Square small = new Square(1); Square big = new Square(3); Frame frm = new Frame(small, big); assert(0 < frm.size()); small.setLen(5); } class Frame { private Square outer, inner; Frame(Square i, Square o) { assert(i.len() <o.len()); outer = o; inner = i; } int size() { return outer.len() * outer.len() – inner.len() * inner.len(); OK Frame is 2 squares on in each other oops

15 Deep 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);

16 Deep 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); x 1 x1 h size=0 size=1 size=2 n 3 z 4 w n n 2 y1 h y size=0 size=3 size=2 size=1

17 Modification 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;

18 Modification via Iterators
itr1 c e 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 c e list 1 o h 2 n o 3 n o

19 Modification 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 list h 2 n o n n 1 o o 3 h

20 Encapsulation 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

21 Modular Assume/Guarantee Reasoning
Class Verifier Correct API usage Correct implementation Specification

22 Idea 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

23 Ownership (Clarke & Drossopoulo)
class Main <> { List<this, world> list; Main() writes this { list = new List<this, world>; } void populate() writes under (this.1) { list add(new Data<world>); list.add(new Data<world>);} static void main() writes under world { Main main = new Main<>; main.populate(); class List <owner, data> { Link <this,data> head; void add(Data <data> d) writes under(this) { head = new Link<this, data>(d, head); }

24 Object Graph World Data List Link Main

25 Reasoning about programs using ownership
List<p,world> list1; List<q,world> list2; for (i = 0; i < 10; i++) { list1.add(new Data<world>(i)); // exp1 } list2.add(new Data<world>(i)); // exp2 Questions: Are list1 and list2 aliases? Do exp1 and exp2 interfere? Can the loops be fused?

26 Other concepts Other ownership models Object oriented effect system
SPEC C# Object oriented effect system Based on uniquness Alias types Islands Separation logic (next week)

27 Summary Everybody agrees that encapsulation is desired
But no agreement on the exact definition

28 Related Issues Sharing Aliasing Class (object) invariant Modularity
Confinment

29 Giving a presentation Ian Parbery

30 How to give a presentation
What to say and how to say it Getting through the audience Visual aids

31 What to say and how to say it
Communicate the Key Ideas Don’t 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

32 Introduction Define the problem Motivate the audience
Introduce terminology Discuss earlier work Emphasize the contributions [Provide a road map] Use Examples

33 The body Abstract the major results
Explain the significance of the results Explain the main techniques Use enlightening examples Demonstrations are welcome

34 [Technicalities] Expert only part
Show something really interesting beyond the paper/tool

35 The Conclusion Hindsight is clearer than Foresight
Give open problems/further work Indicate that your talk is over

36 Know your audience Background

37 Getting through the Audience
Use Repetitions Remind, don’t assume Don’t over-run Maintain Eye Contact Control your voice Control your motion Take care of your appearance

38 Visual Aids PowerPoint transparencies Don’t overload transparencies
Don’t use too many transparencies Use Overlays Properly Use Color Effectively Use Pictures and Tables The blackboard can be used too

39 Don’t overload transparencies
The input of the program can be arbitrary. Let x be a prime number, i.e., all the numbers z<x do not divide x. y be the next prime number, i.e., etc. Arbitrary input Prime number x The next prime y

40 Use overlays (im)properly
Item 1 Item 1.1 Item 1.2 Item 2 Item 2.1 Item 2.1.1

41 Use colors properly Item 1 Item 2 Item 3

42 The End


Download ppt "Mooly Sagiv Schriber 317 Office Hours Wed"

Similar presentations


Ads by Google