Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative.

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.
Function Composition CS 5010 Program Design Paradigms “Bootcamp” 1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.:
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 ©
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.
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.
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 1: Combine Simpler Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed.
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.
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.
How to Design Worlds 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.
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.
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.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
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.
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.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
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
Solving Your Problem by Generalization
A Case Study: Space Invaders
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
The Iterative Design Recipe
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:

Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.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

Module Introduction In this module, we will see how classes, objects, and interfaces fit into our account of information analysis and data design We'll see how the functional and the object- oriented models are related We'll learn how to apply the design recipe in an object-oriented setting. 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 10 3

Goals of this lesson Learn the basics about classes, objects, fields, and methods. Learn how these ideas are expressed in the Racket object system 4

What is an object? An object is another way of representing compound data, like a struct. Like a struct, it has fields. It has one built-in field, called this, which always refers to this object Here are pictures of two simple objects: x = 10 y = 20 r = 10 this = h = 30 w = 15 color = "blue" this = We assume that you've seen some kind of object-oriented programming before, so we're just reviewing vocabulary here. If you've really never used OOP before, go do some outside reading before continuing. 5

How do you compute with an object? Every object comes equipped with a set of procedures, called methods. Each method has a name. To invoke a method of an object, we send the object a message. For example, to invoke the area method of an object obj1, we write (send obj1 area) 6

Classes Every object has a class. The class specifies the fields of the object. – so it's like a define-struct. The class contains the methods of that object. In a typical design, we are likely to have many objects of the same class. To create an object, we say (new C) where C is the name of the new object's class. You say more than this, but this is good enough right now. 7

Every object knows its class (1) 8 x = 10 y = 20 r = 10 x = 15 y = 35 r = 5 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n))...) Here are two objects of the same class. In the class definition, the init-field declaration specifies that each object of this class has 3 fields, named x, y, and r. The class definition also defines two methods, named foo and bar, that are applicable to any object of this class. These objects also have a this field, but we don't show it unless we need to.

Every object knows its class (2) 9 x = 10 y = 20 r = 10 x = 15 y = 35 r = 5 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n))...) obj1 obj2 The variables in the method declarations refer to the fields in the object. So: (send obj1 foo) returns 30 (send obj2 foo) returns 50

Every object knows its class (3) 10 x = 10 y = 20 r = 10 x = 15 y = 35 r = 5 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n))...) obj1 obj2 Methods can also take arguments, just like functions. So (send obj1 bar 8) returns 18 (send obj2 bar 8) returns 13

Every object knows its class (4) 11 x = 10 y = 20 r = 10 this = x = 15 y = 35 r = 5 this = (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n)) (define/public (baz n) (+ (send this foo) n)))...) obj1 obj2 Methods are just Racket functions, so they can do anything a Racket function can do, including send messages to objects. (send obj1 baz 20) returns ( ) = 50 (send obj2 baz 20) returns ( ) = 70

Every object knows its class (5) 12 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n)) (define/public (baz n) (+ (send this foo) n)))...) obj1 obj2 x = 10 y = 20 r = 10 this = x = 15 y = 35 r = 5 this = x = 15 y = 35 r = 5 this = (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (- r n)) (define/public (baz n) (+ (send this foo) n)))...) obj3 Here's another object, obj3, of a different class (observe that the bar method is different). If we send a message to obj3, then obj3's methods will be invoked.

Every object knows its class (6) 13 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n)) (define/public (baz n) (+ (send this foo) n)))...) obj1 obj2 x = 10 y = 20 r = 10 this = x = 15 y = 35 r = 5 this = x = 15 y = 35 r = 5 this = (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (- r n)) (define/public (baz n) (+ (send this foo) n)))...) obj3 So (send obj2 bar 8) = (+ 5 8) = 13 (send obj3 bar 8) = (- 5 8) = -3

Using The Racket Class System We will use full Racket (yay!) Write #lang racket at the beginning of each file And set the Language level to "Determine Language from Source" 14

First demonstration system: space- invaders.rkt A simple animated system using the universe module and the Racket object system Specifications: We have classes for – worlds – bombs – helicopters 15

Game Description When the system starts, the world contains just a helicopter the helicopter rises at a constant rate Press space to drop a new bomb Bombs fall at a constant rate Bombs are draggable 16

Goal We'll walk through the code of this system to illustrate the Racket object system. 17

Demonstration: space-invaders.rkt Demonstration and code walkthrough space-invaders.rkt Demonstration: (0:48) Walkthrough: – Part 1: (7:09) – Part 2: (7:03) 18 As with other videos, these videos were recorded earlier, and may not represent our best current practice. In particular, they use Number instead of Integer.

Lesson Summary We’ve learned the basics about classes, objects, fields, and methods. We’ve seen how these ideas are expressed in the Racket object system 19

Next Steps Study the file 10-1-space-invaders.rkt 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 20