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

Slides:



Advertisements
Similar presentations
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Advertisements

Real State vs. Simulated State CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.3 © Mitchell Wand, This work is licensed under a Creative.
Draggable Cat CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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.
Testing and Debugging CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.3 © Mitchell Wand, This work is licensed under a Creative Commons.
Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Language Issues for Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.3 © 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 ©
Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, This work is licensed under a.
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.
Invariants and Performance 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.
Model-View-Controller Architecture CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
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.
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.
The DD  OO Recipe CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
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.
Functions vs. Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4
Model-View-Controller Architecture
Generalizing Similar Functions
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.1
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
Introduction to Invariants
Generalizing this Design
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
A Case Study: Space Invaders
Testing Mutable Objects
The Design Recipe using Classes
Generalizing Similar Functions
The Iterative Design Recipe
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
Design Strategies 3: Divide into cases
Presentation transcript:

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

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 on a canvas What’s interesting, though, is that the canvas has an draggable wall, so the ball needs to find out about the position of the wall at every tick. 3

Let's look at some code: 10-2A-ball- and-wall.rkt ;; The World implements the WorldState interface (define WorldState (interface () ; -> WorldState ; GIVEN: no arguments ; RETURNS: the state of the world at the next tick after-tick ; Integer Integer MouseEvent-> WorldState ; GIVEN: a location ; RETURNS: the state of the world that should follow the ; given mouse event at the given location. after-mouse-event ; KeyEvent : KeyEvent -> WorldState ; GIVEN: a key event ; RETURNS: the state of the world that should follow the ; given key event after-key-event ; -> Scene ; GIVEN: a scene ; RETURNS: a scene that depicts this World to-scene )) ;; Every object that lives in the world must implement the Widget ;; interface. (define Widget (interface () ; -> Widget ; GIVEN: no arguments ; RETURNS: the state of this object that should follow the next tick after-tick ; Integer Integer -> Widget ; GIVEN: a location ; RETURNS: the state of this object that should follow the ; specified mouse event at the given location. after-button-down after-button-up after-drag ; KeyEvent : KeyEvent -> Widget ; GIVEN: a key event and a time ; RETURNS: the state of this object that should follow the ; given key event after-key-event ; Scene -> Scene ; GIVEN: a scene ; RETURNS: a scene like the given one, but with this object ; painted on it. add-to-scene )) 4 WorldState and Widget interfaces as before

Wall interface (define Wall (interface (Widget ) ; -> Int ; RETURNS: the x-position of the wall get-pos )) 5 The wall will have an extra method that returns the current position of the wall. This information is needed by the ball. This means that the Wall interface includes all the methods from the Widget interface. This is called "interface inheritance."

The Ball% class ;; A Ball is a (new Ball% ;; [x Int][y Int][speed Int][w Wall]) (define Ball% (class* object% (Widget ) (init-field w) ;; the Wall... ;; after-tick : -> Ball ;; RETURNS: state of this ball ;; after a tick. (define/public (after-tick) (if selected? this (new Ball% [x (next-x-pos)] [y y] [speed (next-speed)] [selected? selected?] [saved-mx saved-mx] [saved-my saved-my] [w w]))) ;; -> Integer ;; position of the ball at the next ;; tick. ;; STRATEGY: ask the wall for its ;; position and use that to ;; calculate the upper bound for ;; the ball's x position (define (next-x-pos) (limit-value radius (+ x speed) (- (send w get-pos) radius))) ;; Number^3 -> Number ;; WHERE: lo <= hi ;; RETURNS: val, but limited to the ;; range [lo,hi] (define (limit-value lo val hi) (max lo (min val hi))) 6 At every tick, the ball asks w about its position The wall is an init-field of the ball

The Wall% class ;; A Wall is (new Wall% [pos Integer] ;; [saved-mx Integer] ;; [selected? Boolean]) ;; all these fields have default values. (define Wall% (class* object% (Wall ) ;; the x position of the wall (init-field [pos INITIAL-WALL-POSITION]) ;; is the wall selected? Default is false. (init-field [selected? false]) ;; if the wall is selected, the x position of ;; the last button-down event near the wall, ;; relative to the wall position (init-field [saved-mx 0]) (super-new) ;; the extra behavior for Wall (define/public (get-pos) pos) ; after-button-down : Integer Integer -> Wall ; GIVEN: the location of a button-down event ; STRATEGY: Cases on whether the event is near ; the wall ; RETURNS: A wall like this one, but selected, and ; with mouse x location (relative to the wall ; position) recorded (define/public (after-button-down mx my) (if (near-wall? mx) (new Wall% [pos pos] [selected? true] [saved-mx (- mx pos)]) this)) ; after-drag : Integer Integer -> Wall ; GIVEN: the location of a drag event ; STRATEGY: Cases on whether the wall is selected. ; If it is selected, returns a wall like this one, ; except that ; the vector from its position to ; the drag event is equal to saved-mx (define/public (after-drag mx my) (if selected? (new Wall% [pos (- mx saved-mx)] [selected? true] [saved-mx saved-mx]) this)) 7 The code for the Wall% class is perfectly routine

Here's a demo 8 If you have difficulty with this video, look at in on YouTube, or just run 10- 2A-ball-and-wall.rkt.YouTube

What went wrong? After a drag, however, the world has a new wall at the new position. But the ball still points at the original wall, in the original position. So the ball bounces at the position where the wall used to be. 9

We need to make the wall stateful We need to give the wall a stable identity, so balls will know who to ask. But the information in the wall must change! Solution: we need to make the box MUTABLE. In other words, it should have state. What does that mean? How do we do this? That is the topic of the next two lessons. 10

Next Steps Study 10-2A-ball-and-wall.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. 11