Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 ©...
-
Upload
samuel-blake -
Category
Documents
-
view
213 -
download
0
Transcript of Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 ©...
![Page 1: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/1.jpg)
1
Stateful Objects and Stable Identities
CS 5010 Program Design Paradigms"Bootcamp"Lesson 11.2
© Mitchell Wand, 2012-2014This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
![Page 2: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/2.jpg)
2
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.
![Page 3: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/3.jpg)
3
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.
![Page 4: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/4.jpg)
4
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.
![Page 5: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/5.jpg)
5
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.
Name in Videos Name in Example Files
Container% World%
Container-and-box.rkt Adjustable-box.rkt
Bouncing-ball-try1.rkt Bouncing-ball-functional.rkt
![Page 6: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/6.jpg)
6
Code walkthrough: adjustable-box.rkt
• Watch the video at http://www.youtube.com/watch?v=ihE-TY17_UA (14:40)
![Page 7: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/7.jpg)
7
Video demonstration: bouncing-ball-functional.rkt
• http://www.youtube.com/watch?v=q1pkdjd9Djo (14:21)
![Page 8: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/8.jpg)
8
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.
![Page 9: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/9.jpg)
9
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.
![Page 10: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/10.jpg)
10
x = 250w = 100
(right-edge) => 300
x = 220box =
x = 225box =
(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
![Page 11: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/11.jpg)
11
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:
![Page 12: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/12.jpg)
12
x = 250w = 100
(right-edge) => 300
x = 220box =
(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 = 252w = 104
(right-edge) => 304
ball after-mouse returns itself
FAIL!!!INVARIANT: the world’s ball always points to the world’s box
![Page 13: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/13.jpg)
13
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:
![Page 14: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/14.jpg)
14
x = 250w = 100
(right-edge) => 300
x = 220box =
(send world after-mouse ...) = (new World% [box (begin (send box after-mouse ...) box)] [ball (send ball after-mouse ...)])
world
What we want:
x = 252w = 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!
![Page 15: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/15.jpg)
15
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.
![Page 16: Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, 2012-2014 This work is licensed under a.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d8b5503460f94a719bc/html5/thumbnails/16.jpg)
16
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.