#### Module 13

Last updated: Mon, 20 Apr 2015 16:40:05 -0400

##### Supplemental Materials
• Note: These slides are from a previous semester. We provide them because learning is often aided by looking at different presentations of the same material. However, previous iterations of the course had a different structure and different style requirements. Read these slides for their ideas only. When there are discrepancies, follow the rules of our current course.
• Patterns of Communication Between Objects [pptx] [pdf]

• Stateful Objects and Stable Identities [pptx] [pdf]

• Real State vs Simulated State [pptx] [pdf]

• Converting from Immutable to Mutable Objects [pptx] [pdf]

• Testing Mutable Objects [pptx] [pdf]

• Publish-Subscribe [pptx] [pdf]

##### Goals
• Understand how to use mutable objects.

• Understand some common patterns that require mutable objects.

##### In-class

The following big-bang program displays a ball bouncing inside a box (start with (run)).
The box may be resized by clicking and dragging on its right edge. However, the ball does not change behavior when the box is resized because the sharing of the Box% object is lost when the World% creates a new Box% but does not update the Ball%.
Fix this program by using mutation. Use either the "push" or "pull" style, as described in the course notes. If you go with the "push" approach, define explicit Subscriber<%> and Publisher<%> interfaces. Be ready to justify your design choice.
Make sure your code follows the Effectful Methods style guide.
Here are some Racket functions and forms that may be useful in effectful programs:
Also, some Racket forms have an implicit begin in their bodies:
This means that the following f and g functions are the same.
> (define Y 100)
> (define Z 200)
 > (define (f x) (begin (set! Y (+ Y x)) (set! Z (+ Z x)) (+ Y Z x)))
 > (define (g x) (set! Y (+ Y x)) (set! Z (+ Z x)) (+ Y Z x))
> (f 10)

330

> Y

110

> Z

210

> (g 10)

350

> Y

120

> Z

220

Solution 1 (pull):
Solution 2 (push):

Problem Set 13