Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, 2012-2014 This work is licensed under.

Slides:



Advertisements
Similar presentations
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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.
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 ©
Function Composition CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Converting from Immutable to Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.4 © Mitchell Wand, This work is licensed under.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 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.
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.
The Point of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.1 © Mitchell Wand, This work is licensed under a Creative Commons.
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.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © 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.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
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.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Functions vs. Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
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.
Generalizing Similar Functions
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
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 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
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
Examining Two Pieces of Data
A Case Study: Space Invaders
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Design Strategies 3: Divide into cases
Presentation transcript:

Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © 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 this Module Objects can communicate in two basic ways: pull and push. Objects must have stable identity in order to communicate reliably We use stateful objects to implement objects with stable identity. Publish-subscribe is a common pattern for implementing push-style communication Delegates are a refinement of publish-subscribe. 2

Generalization Over Constants Over Expressions Over Contexts Over Data Representations Over Method Implementations Mixed Data Data Representations Basics Recursive Data Functional Data Objects & Classes Stateful Objects Design Strategies Function Composition Structural Decomposition Generalization General Recursion Communication via State Module 11 3

Key Points for Lesson 11.1 Sometimes you need to combine data from two objects. The data could be combined in 3 possible places: – some external function (typical in functional organization, but generally considered bad OO design) – asking the other object to give you its data ("pull" model) – sending your information to the other object, and asking it to do the computation ("push" model) 4

Most methods have an obvious home Most of the time, we want to do calculations in the object where the data is. If you need to compute the area of a circle, make that a method of the Circle% class. 5

Sometimes you need to combine information from two objects How do you determine if two balls intersect? Let’s look at three designs. 6

Design #1: Balls as just data structures (define Ball0 (interface () ;; -> Integer ;; RETURN: x, y coords of center and radius, all in pixels get-x get-y get-r)) (define Ball0% (class* object% (Ball0 ) (init-field x y r) ; interpretation omitted... (super-new) (define/public (get-x) x) (define/public (get-y) y) (define/public (get-r) r))) Design #1: Just use objects in place of structs. We equip our objects with methods that get each field, and do our computation outside of any object. Here is the interface and a sample class definition in this style. 7

Implementation of first design ;; Ball0 Ball0 -> Boolean (define (intersects? b1 b2) (coordinates-intersect? (send b1 get-x) (send b1 get-y) (send b1 get-r) (send b2 get-x) (send b2 get-y) (send b2 get-r))) (define (coordinates-intersect? x1 y1 r1 x2 y2 r2) (<= (+ (sqr (- x1 x2)) (sqr (- y1 y2))) (sqr (+ r1 r2)))) This is considered poor OO design: we are just using objects as structs! We want to package the computation with the data. Contracts should usually be in terms of interfaces, not classes. We almost never care what actual class an argument is– only that it will respond to the messages we send it. If we had some other class that implemented Ball0, our code would work with it. 8

Design #2: Collaborate by pulling information from the other object (define Ball1 (interface () ;; -> Integer ;; RETURN: x, y coords of center and radius, ;; all in pixels get-x get-y get-r ;; Ball1 -> Boolean ;; Does the given ball intersect with this one? intersects? )) In our second design, we add a method intersects? to the interface. 9

(define Ball1% (class* object% (Ball1 ) (init-field x y r) ; interpretation omitted... (super-new) (define/public (intersects? other-b) (coordinates-intersect? (send other-b get-x) (send other-b get-y) (send other-b get-r))) ;; Integer^3 -> Boolean ;; GIVEN: the coordinates of some ball ;; RETURNS: would that ball intersect this one? (define (coordinates-intersect? other-x other-y other-r) (<= (+ (sqr (- x other-x)) (sqr (- y other-y))) (sqr (+ r other-r)))) (define/public (get-x) x) (define/public (get-y) y) (define/public (get-r) r) )) Ask the other ball for its information Do the computation here Be prepared to answer if someone asks you the same questions! Method Definitions for Pull Model 10

Pull model: what happens 1. (send b1 intersects? b2) 2. b1 asks b2 for its data. b2 gives it. 3. then b1 does the arithmetic. OK if x, y, r are already observable. But what if they are not? b1 is the object that actually does the computation. 11

Design #3. Push Model: the object pushes its data to the other object (define Ball2 (interface () ;; Ball2 -> Boolean ;; does the given ball intersect this one? intersects? ;; Integer^3 -> Boolean ;; GIVEN: the x,y,r of some ball ;; RETURNS: would that ball ;; intersect with this one? intersect-responder )) In this design, when this ball is asked whether it intersects with some other ball, it sends its information to the other ball, and asks that ball to compute the intersection. 12

So now we have two methods intersects? sends this ball’s data to the other ball. intersect-responder responds to the request, computing whether or not there is an intersection between the two balls. 13

Method Definitions for push model ;; A Ball2 is a (new Ball2% [x Integer][y Integer][r Integer]) (define Ball2% (class* object% (Ball-Better ) (init-field x y r) ; interpretation omitted... (super-new) (define/public (intersects? other-b) (send other-b coordinates-intersect? x y r)) ;; Integer^3 -> Boolean ;; GIVEN: the coordinates of some ball ;; RETURNS: would that ball intersect this one? (define/public (coordinates-intersect? other-x other-y other-r) (<= (+ (sqr (- x other-x)) (sqr (- y other-y))) (sqr (- r other-r)))) )) Send your data to the other ball and ask him to finish the computation If someone asks you a question, be prepared to answer it 14

Push model: what happens 1. (send b1 intersects? b2) 2.b1 sends its data to b2 3.b2 answers the question. This is sometimes called “double dispatch” b2 doesn’t know who’s asking. A related pattern is called “the visitor pattern.” This is a variation of this design when one of the structures is itemization data. We don’t have time in this course to deal with the visitor pattern, but you should now be equipped to learn about it. In this design, b2 is the ball that does the geometric calculation. This pattern is also sometimes called “double dispatch”. It shows up often in object-oriented programming. 15

Push or pull: how to choose? Most of the time the answer is clear: most operations naturally act on a particular object. Operations should happen in the object where the data resides – our first attempt was not good design Binary operations like intersect? are relatively rare in practice – either design 2 or design 3 would be ok for our purposes – more on this in next lesson... 16

Lesson Summary Sometimes you need to combine data from two objects. The data could be combined in 3 possible places: – some external function (typical in functional organization, but generally considered bad OO design) – asking the other object to give you its data ("pull" model) – sending your information to the other object, and asking it to do the computation ("push" model) 17

Next Steps Study 11-1-communicating-objects.rkt in the Examples folder If you have questions about this lesson, ask them on the Discussion Board Go on to the next lesson 18