The DD  OO Recipe CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.4 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial.

Slides:



Advertisements
Similar presentations
Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Advertisements

Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
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.:
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © 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.
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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 ©
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.3 © Mitchell Wand, This work is licensed under a Creative.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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.
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.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures 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.
Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
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.
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.
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.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
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.
How to use an Observer Template
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
The Different Kinds of Data
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Presentation transcript:

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 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License 1

Introduction We’ve seen the connections: – objects are like structs – classes are like define-structs – interfaces are like data definitions We’ve seen that an OO program is just like a functional program, but with the pieces arranged in a different way If I had a functional program, just what would I need to do to convert it to an OO program? 2

Goals of this lesson Learn how to translate from data definitions to classes and interfaces Learn how to go from function definitions to method definitions (in more detail) 3 If the last lesson was the Big Picture, then this is the Small Picture.

Objects and Classes as Data Designs Objects and Classes provide a new representation for information. This representation is different from the one we have been using, but the correspondence between the two representations is simple. Here it is: 4

Representing information using classes and interfaces Functional OrganizationObject-Oriented Organization Compound DataClass with the same fields Itemization DataInterface Mixed Data Interface specifies functions that work on that information One class for each variant, with the same fields as the variant Each class implements the interface 5

Shapes: Data Definition (define-struct my-circle (x y r color) #:transparent) (define-struct my-square (x y l color) #:transparent) (define-struct my-composite (front back) #:transparent) ;; A Shape is one of ;; -- (make-my-circle Number Number Number ColorString) ;; -- (make-my-square Number Number Number ColorString) ;; -- (make-my-composite Shape Shape) ;; interp: ;;... Here is the data definition for our shapes example. 6

Shapes: Class Diagram And here is the corresponding class diagram. We have an interface for shapes, and three classes corresponding to the three variants. Each class has fields corresponding to the fields of the variant. Note that we don't know the class of front or back. We only know their interface, but that's enough! 7

Recipe for converting from data defs to interfaces and classes Converting from Data Definitions to Interfaces and Classes 1. Define an interface for each kind of enumeration data. 2. Define a class for each kind of compound data. In the class, put in an init-field for each field in the struct. 3. Convert (make-whatever...) to (new whatever% [field1...][field2...]) 4. For each function that follows the template, add a method to the interface. 5. Convert functions to methods. (see recipe below) Here is a recipe for converting from data definitions and functions to interfaces and classes. The details of this recipe are specific to Racket, but the ideas can be used in any object-oriented language. 8

Example (define-struct foo (field1 left right)) (define-struct bar (lo hi)) ;; Data Definition ;; A Baz is one of ;; -- (make-foo Number Baz Baz) ;; -- (make-bar Number Number) (define Baz (interface ()... )) (define Foo% (class* object% (Baz ) (init-field field1 left right)... (super-new))) (define Bar% (class* object% (Baz ) (init-field lo hi)... (super-new))) Here’s an example of steps 1 and 2 of the recipe. The compound data Baz, with variants foo and bar, turns into an interface Baz, with classes Foo% and Bar% that Implement Baz. 9

Creating Objects Replace (make-bar 12 13) by (new Bar% [lo 12][hi 13]) Here’s an example of step 3 of the recipe. 10

From function calls to method calls Instead of saying (baz-fn a-baz n) say (send a-baz fn n) Think: "Send John a book" (not "send a book to John"). 11

From Function Definitions to Method Definitions Converting a Function Definition to a Method Definition 1. Add function name to interface 2. Pull out the parts 3. Change selectors to field references 4. Change function calls to method calls 5. Put method definitions into classes Here is a recipe for converting a function definition to a method definition. This expands step 5 of the recipe above. 12

Turning a function definition into a set of method definitions Example: ;; baz-mymin : Baz Number -> Number (define (baz-mymin b n) (cond [(foo? b) (min (foo-field1 b) (baz-mymin (foo-left b) n) (baz-mymin (foo-right b) n) (other-fn b n))] [(bar? b) (min n (bar-lo b) (bar-hi b))])) 13

1. Add function name to the interface Add to baz interface: (define baz (interface () mymin ; Number -> Number other-fn ; Number -> Number... )) First we add a method for this function to the interface. The name of the method need not be the same as the name of the function, but it should be similar. The function version had contract Baz Number -> Number, so the method has contract Number -> Number, since the Baz argument is replaced by "this" object. Here's another function that we have put in the interface. 14

Original Function Definition Example: ;; baz-mymin : Baz Number -> Number (define (baz-mymin b n) (cond [(foo? b) (min (foo-field1 b) (baz-mymin (foo-left b) n) (baz-mymin (foo-right b) n) (other-fn b n))] [(bar? b) (min n (bar-lo b) (bar-hi b))])) 15

2. Pull Out the Parts Example: ;; baz-mymin : Baz Number -> Number (define (baz-mymin b n) (cond For a foo: (min (foo-field1 b) (baz-mymin (foo-left b) n) (baz-mymin (foo-right b) n) (other-fn b n)) For a bar: (min n (bar-lo b) (bar-hi b)) 16

3. Change Selectors to field references Example: ;; baz-mymin : Baz Number -> Number (define (baz-mymin b n) (cond For a foo: (min field1 (baz-mymin left n) (baz-mymin right n) (other-fn this n)) For a bar: (min n lo hi) (foo-left b) is replaced by the field left. A b that appears by itself refers to the whole struct, so it is replaced by this. 17

4. Change Function Calls to Method Calls Example: ;; baz-mymin : Baz Number -> Number (define (baz-mymin b n) (cond For a foo: (min field1 (send left mymin n) (send right mymin n) (send this other-fn n)) For a bar: (min n lo hi) 18

5. Put method definitions into classes Example: ;; baz-mymin : Baz Number -> Number (define (b n) In Foo%: (define/public (mymin n) (min field1 (send left mymin n) (send right mymin n) (send this other-fn n)) In Bar%: (define/public (mymin n) (min n lo hi) 19

Summary We've seen the connections: – objects are like structs – classes are like define-structs – interfaces are like data definitions We've seen how to translate from data definitions to classes and interfaces We've seen how to go from function definitions to method definitions. 20

Summary You should now be able to convert any set of functions working on mixed data to an OO style. 21

Next Steps Study example 10-7 in the Examples folder. Do the Guided Practices If you have questions about this lesson, ask them on the Discussion Board Go on to the next lesson 22