Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.

Slides:



Advertisements
Similar presentations
Real State vs. Simulated State CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.3 © Mitchell Wand, This work is licensed under a Creative.
Advertisements

Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Testing Simple Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.6 © Mitchell Wand, This work is licensed under a Creative Commons.
Patterns of Interaction 2: Publish-Subscribe CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.6 © Mitchell Wand, This work is licensed under.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.3 © Mitchell Wand, This work is licensed under a Creative.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Converting from Immutable to Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.4 © Mitchell Wand, This work is licensed under.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Testing Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.5 © Mitchell Wand, This work is licensed under a Creative Commons.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Design Strategies 3: Divide into cases CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Using Inheritance to Share Implementations CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Introduction to Universe Programs CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
A Simple Introduction to Git: a distributed version-control system CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.5 © Mitchell Wand, This.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
How to Design Worlds CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
A Case Study: Space Invaders CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Testing Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Patterns of Interaction 2: Publish-Subscribe CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Callbacks and Interacting Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Testing Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Generalizing this Design CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Observerables are not Fields CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.7 © Mitchell Wand, This work is licensed under a Creative.
Using Inheritance to Share Implementations CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Converting from Immutable to Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Examining Two Pieces of Data
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
Introduction to Invariants
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
Generalizing this Design
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
Callbacks and Interacting Objects
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.2
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Patterns of Interaction 2: Publish-Subscribe
Patterns of Interaction 2: Publish-Subscribe
Solving Your Problem by Generalization
A Case Study: Space Invaders
Testing Mutable Objects
Generalizing Similar Functions
The Iterative Design Recipe
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Examining Two Pieces of Data
A Case Study: Space Invaders
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License 1

Key Points for Lesson 11.2 Sometimes objects need to ask questions of each other over time. To accomplish this, the object being queried needs to have a stable identity that the querier can rely on. In this lesson, we'll show what can happen when this fails. 2

Sometimes making a new object doesn't do what's needed We now begin a sequence of programs illustrating patterns of object communication. These programs will involve a ball bouncing in a box. What’s interesting, though, is that the box has an adjustable wall, so the ball and the box need to communicate about the position of the wall. 3

adjustable-box.rkt Let’s begin with just the box. We will have many objects in our world, so we make our world a container that just passes the messages from big-bang down to the objects it contains. 4

Translating from the videos to the example files We’re about to view a set of videos. These videos were recorded a while ago, so our terminology has changed a little bit. Here are the most significant differences: We’ve also updated the tests to our current framework. 5 Name in VideosName in Example Files Container%World% Container-and-box.rktAdjustable-box.rkt Bouncing-ball-try1.rktBouncing-ball-functional.rkt

Code walkthrough: adjustable-box.rkt Watch the video at TY17_UA (14:40) TY17_UA 6

Video demonstration: bouncing-ball-functional.rkt Djo (14:21) Djo 7

What happened here? Let’s draw some pictures We'll use a simple world with one box, called box, and one ball, called ball We'll call the world that big-bang sees world. 8

What is the invariant? On the next slide, we see pictures of the world before and after a tick. The world obeys the invariant that the world’s ball always points to the correct box: namely, the world’s box. This stays true even though after a tick, we have a new world. 9

x = 250 w = 100 (right-edge) => 300 x = 220 box = x = 225 box = (send world after-tick) = (new World% [box (send box after-tick)] [ball (send ball after-tick)]) world (send after-tick) World-after-tick in bouncing-ball- functional.rkt box after-tick returns itself INVARIANT: the world’s ball always points to the world’s box 10

What happens after a drag After a drag, however, the world has a new box with the new dimensions. But it still has the old ball, pointing to the old box. The invariant has been violated. The ball still bounces at 300, where the right edge used to be. Here's a picture: 11

x = 250 w = 100 (right-edge) => 300 x = 220 box = (send world after-mouse...) = (new World% [box (send box after-mouse...)] [ball (send ball after-mouse...)]) world World after drag in bouncing-ball- functional.rkt x = 252 w = 104 (right-edge) => 304 ball after-mouse returns itself FAIL!!! INVARIANT: the world’s ball always points to the world’s box 12

What we want What we want is for the box to have a stable identity. After a drag, the box’s fields will change, but it is still the same box, so the ball will still see it. The invariant will be preserved. Again, here's a picture: 13

x = 250 w = 100 (right-edge) => 300 x = 220 box = (send world after-mouse...) = (new World% [box (begin (send box after-mouse...) box)] [ball (send ball after-mouse...)]) world What we want: x = 252 w = 104 (right-edge) => 304 Container always has same box ball after-mouse returns itself INVARIANT: the world’s ball always points to the world’s box WIN! 14

We need to give the box STATE! We need to give the box a stable identity, so balls will know who to ask. But the information in the box must change! Solution: we need to make the box MUTABLE. In other words, it should have state. 15

Next Steps Study adjustable-box.rkt and bouncing-ball- functional.rkt in the Examples folder. In the next lesson, we'll consider the difference between real state and simulated state in a little more detail. Then we'll consider how to program systems with state in our framework. 16