Given the description of a problem, how do you determine what classes to define?

53
1 • Given the description of a problem, – how do you determine what classes to define? – how do you design each class? • Need a design methodology

description

Given the description of a problem, how do you determine what classes to define? how do you design each class? Need a design methodology. Object-Oriented Design. Often software mimics the real world Decompose problem into objects identify properties and behaviors model in software - PowerPoint PPT Presentation

Transcript of Given the description of a problem, how do you determine what classes to define?

Page 1: Given the description of a problem, how do you determine what classes to define?

1

• Given the description of a problem,– how do you determine what classes to

define?– how do you design each class?

• Need a design methodology

Page 2: Given the description of a problem, how do you determine what classes to define?

2

Object-Oriented Design

• Often software mimics the real world

• Decompose problem into objects– identify properties and behaviors– model in software

Ex.) FallingBall active objects behave like falling balls

Page 3: Given the description of a problem, how do you determine what classes to define?

3

Abstraction

• An object provides an abstraction– can use without knowing details of its

implementation– abstract away details; understand at higher level

• Follows from our use of objects in real world– can use a stopwatch without knowing exactly how

it works

• Well-designed classes provide good abstractions.

Page 4: Given the description of a problem, how do you determine what classes to define?

4

A Simple Stopwatch

Consider

• implementing a simple stopwatch

• Begin by identifying properties and behaviors to model

Page 5: Given the description of a problem, how do you determine what classes to define?

5

Properties

Properties of an object are the nouns and adjectives that describe it

• Consider properties of a stopwatch; has– buttons that control functionality– display area– internal memory to store timing info

Page 6: Given the description of a problem, how do you determine what classes to define?

6

Modeling Properties

1. Consider properties of the entity to be modeled

2. Decide which properties relevant- which to actually model

3. Decide how to model properties

Page 7: Given the description of a problem, how do you determine what classes to define?

7

Stopwatch Properties

1. Properties of real stopwatches– function buttons, display, memory

2. Which relevant?– Say ours will be used to get time between

mouse clicks– no display or buttons needed

3. How to model?– need to calculate elapsed time– need to model start time for calculation

Page 8: Given the description of a problem, how do you determine what classes to define?

8

Behaviors

The verbs that describe object functionality

• Behaviors of a real stopwatch– reset to zero– report elapsed time– stop– resume timing

• Need to choose which to model

Page 9: Given the description of a problem, how do you determine what classes to define?

9

Stepping Through the Process

Design and implement simple shell game– three cups + 1 marble

– player 1 hides marble: drags marble onto a cup– player 1 shuffles cups: drags cups with mouse– player 2 guesses: clicks on a cup

• cup raised• marble displayed, if there

Page 10: Given the description of a problem, how do you determine what classes to define?

10

Step 1:Identify Objects to Model

• Objects in problem description– players– cups– marble

• Which to model?– cups– marble

Note: players are users of program; provide them interface

Page 11: Given the description of a problem, how do you determine what classes to define?

11

Step 2:List Properties and Behaviors

Cup• Properties

– image on screen– empty or not

• Behaviors– can be moved– can have marble placed in it

– can be raised to reveal contents

Page 12: Given the description of a problem, how do you determine what classes to define?

12

// A class to represent a cup used in the Shell Gamepublic class Cup {

// List of cup properties// Graphical image of the cup// Whether the cup contains the marble // List of cup behaviors // Move the cup // Place a marble in the cup // Reveal the contents of the cup; remove marble from cup, // if there

}

Page 13: Given the description of a problem, how do you determine what classes to define?

13

Marble

• Properties– image on screen– in a cup or not*

• Behaviors– can be moved– can be dropped into cup– can be removed from cup

* will ignore this- already modeling containment in cup

Page 14: Given the description of a problem, how do you determine what classes to define?

14

// A class to represent a marblepublic class Marble {

// List of marble properties// Graphical image of the marble

// List of marble behaviors// Move the marble// Place the marble in a cup// Remove the marble from a cup

}

Page 15: Given the description of a problem, how do you determine what classes to define?

15

Need user interface- controller class

• Properties– Physical appearance– State of the game

• Behaviors– set up game– let user move cup or marble– let user show cup contents

Page 16: Given the description of a problem, how do you determine what classes to define?

16

// Controller class for a simple Shell Gamepublic class ShellGame extends WindowController {

// Properties that describe the shell game// Three cups// A marble// The current position of the mouse// Whether a marble or cup has been selected// Which object has been selected

// Allowable game behaviors// Place three cups and a marble on the canvas// Move a cup or marble// Show contents of a cup

}

Page 17: Given the description of a problem, how do you determine what classes to define?

17

Step 3:Model Properties with Inst. Vars.

// A class to represent a cup used in the Shell Gamepublic class Cup {

// List of cup properties

// Graphical image of the cupprivate FilledRect cupSide;private FilledOval cupTop, cupBottom;private FramedRect sideFrame;private FramedOval topFrame, bottomFrame;

// Whether the cup contains the marbleprivate boolean containsMarble;

// List of cup behaviors// Move the cup// Place a marble in the cup// Reveal the contents of the cup; remove marble from cup, if there

}

Page 18: Given the description of a problem, how do you determine what classes to define?

18

// A class to represent a marblepublic class Marble {

// Marble properties// Graphical image of the marbleprivate FilledOval theMarble;

// Marble behaviors// Move the marble// Place the marble in a cup// Remove the marble from a cup

}

Page 19: Given the description of a problem, how do you determine what classes to define?

19

// Properties that describe the shell game// Three cups// A marble// The current position of the mouse// Whether a marble or cup has been selected// Which object has been selected

• On second thought: don’t really need current mouse location. Can get in onMousePress, etc, as needed.

• But need last mouse position for dragging

Page 20: Given the description of a problem, how do you determine what classes to define?

20

Design is a Process

• Well-defined set of steps guides process

• Need to be open to modifying early decisions

Second Level ShellGame design

Page 21: Given the description of a problem, how do you determine what classes to define?

21

Step 4:Model Behaviors with Methods

• First focus on method headers (signatures)

• Describe interface between entities in our program

Page 22: Given the description of a problem, how do you determine what classes to define?

22

Cup

• Need to “Move the cup”public void move( double dx, double dy )

• “Place a marble in the cup”public void dropIn( Marble aMarble )

• “Reveal the contents”public void showContents()

Note: can’t reveal marble if no ref. to it in the class. Need to add instance variable!

Page 23: Given the description of a problem, how do you determine what classes to define?

23

• Don’t forget about constructors!

public Cup( Location upperLeft, double width, double height, DrawingCanvas canvas )

Page 24: Given the description of a problem, how do you determine what classes to define?

24

Making Choices

• Often need to think hard about design choices

• Ex. Define cup width and height in Cup class? Controller?

• "Third level design: Cup class"

Page 25: Given the description of a problem, how do you determine what classes to define?

25

Marble

• Need to “Move the Marble”public void move( double dx, doubly dy )

• Need to “Place marble in cup”– But cup already does this!

Page 26: Given the description of a problem, how do you determine what classes to define?

26

// A class to represent a marblepublic class Marble {

// Graphical image of the marbleprivate FilledOval theMarble;

// Move the marble// dx, dy - distance to move in the vertical and horizontal directionspublic void move( double dx, double dy )

}

Marble class so far: Nothing but a FilledOval that moves! Class is unnecessary!

Page 27: Given the description of a problem, how do you determine what classes to define?

27

ShellGame controller

• Need to “set up”public void begin()

• “Select marble or cup”public void onMousePress( Location

mousePos)

– a problem: how to determine if mouse in a cup?

– add contains method to Cup

Third Level for ShellGameClass

Page 28: Given the description of a problem, how do you determine what classes to define?

28

Refinement

Designing software is a process of refinement

• Begin at highest level of abstraction; fill in details

• Change earlier design decisions, if necessary

Page 29: Given the description of a problem, how do you determine what classes to define?

29

Filling in the Details• If method simple, write method body• If method complex

– outline method body– fill in details of outline

• Constructor– useful to consider each instance variable– determine instance variable initialization

Cup instance variables and constructor

Page 30: Given the description of a problem, how do you determine what classes to define?

30

Cup methods

• move– straightforward– delegate to components

• contains– easy

• dropIn and showContents more complex

• "Cup class method details"

Page 31: Given the description of a problem, how do you determine what classes to define?

31

ShellGame methods

• begin– similar to constructor– review instance variables; initialize.– specifying a value for a variable is a signal

that constant needed

begin()

Page 32: Given the description of a problem, how do you determine what classes to define?

32

• onMousePress– check whether mouse in a cup; then check

marble– why check cups first?

onMousePress

Page 33: Given the description of a problem, how do you determine what classes to define?

33

• onMouseDrag– determine what selected for drag– move it

onMouseDrag

Page 34: Given the description of a problem, how do you determine what classes to define?

34

• onMouseRelease

• onMouseClick

Page 35: Given the description of a problem, how do you determine what classes to define?

35

Design Process Summary1. Identify objects to be modeled2. For each type of object

– list properties– list behaviors

3. Model Properties with inst. variables4. Model behaviors with methods

– Method headers– Don’t forget constructors

5. Implementation details– if method simple, write it– if complex, outline first

Page 36: Given the description of a problem, how do you determine what classes to define?

36

Incremental Testing

• Test and debug individual components as they are developed– Finding bugs easier if testing

smaller/simpler code– can rely on behavior of smaller entities

when testing/debugging larger components

Page 37: Given the description of a problem, how do you determine what classes to define?

37

Unreal Entities• Not all program classes model entities in the

real worldEx. Animated Shell Game– Player places marble in cup as before– Computer shuffles cups

(when player moves mouse off canvas and back again)

– Player clicks on cup to reveal contents• Program entities

– cups, marble (Real)– Shuffle animation (Not so “real”)

Page 38: Given the description of a problem, how do you determine what classes to define?

38

New Shell Game Design

• Marble - FilledOval as before• Cups

– identical properties to earlier version– nearly identical behaviors– additional behaviors needed:

• ability to move to specific location• ability to report location• ability to be lowered (after being raised to reveal

contents)

• Additional marble methods

Page 39: Given the description of a problem, how do you determine what classes to define?

39

Shuffler Design Steps 1-4

*Will need to extend ActiveObject for animation• Identify properties and behaviors (instance

variables and methods)• Behaviors

– perform shuffling animation (run method)• Properties

– harder to identify– not modeling a “real-world” cup shuffler– consider instance variables needed

• 3 cups• possibly more later

Page 40: Given the description of a problem, how do you determine what classes to define?

40

// A class to animate shuffling in a Shell Gamepublic class Shuffle extends ActiveObject {

// Three cups to be shuffledprivate Cup cup1, cup2, cup3;

// Construct a shuffler

// Shuffle the cupspublic void run()

}

Page 41: Given the description of a problem, how do you determine what classes to define?

41

Shuffler Design Step 5

Refine ideas for animation• Select two cups

*Need RandomIntGenerator• Swap selected cups

– can’t simply swap locations– swap must be visible to player– Move 1st cup to temp location– Move 2nd cup to 1st cup’s original spot– Move 1st cup to 2nd cup’s original spot

*Note: need instance variable for temp location

Page 42: Given the description of a problem, how do you determine what classes to define?

42

Page 43: Given the description of a problem, how do you determine what classes to define?

43

Page 44: Given the description of a problem, how do you determine what classes to define?

44

Page 45: Given the description of a problem, how do you determine what classes to define?

45

• Constructor– will need to be passed 3 cups– needs to set up temp location

(choose to pass in as parameter)– needs to construct RandomIntGenerator

for shuffling– needs to start animation

Shuffler class design

Page 46: Given the description of a problem, how do you determine what classes to define?

46

User interface class

• can reuse some of previous version

• Player can’t shuffle cups– remove selectedCup– remove onMousePress, onMouseDrag

Page 47: Given the description of a problem, how do you determine what classes to define?

47

• onMouseEnter– causes shuffling to begin– only makes sense if marble hidden

New Controller design

Page 48: Given the description of a problem, how do you determine what classes to define?

48

Filling in the Details• Easy

– follow comments that outlined our ideas– comments continue to serve as documentation

• Let’s only consider Shuffler here– Constructor

• remember parameters with instance variables• start animation

shuffler Constructor

– run method• a bit more interesting

Page 49: Given the description of a problem, how do you determine what classes to define?

49

• run method– select 2 different cups to swap

• clever idea: pick the 1 cup that won’t be moved!

– perform the swap– pause between moves to make swap visible

Implementation of shuffling

Page 50: Given the description of a problem, how do you determine what classes to define?

50

Writing Comments• Each class should have a class comment

– Description of class– Author’s name and date

• Each constant and variable should be commented– describe purpose (what, not how!)– parameters– return value, if any

• For long/complex methods– include comments in method– explain what is happening– balance clarity and brevity

Page 51: Given the description of a problem, how do you determine what classes to define?

51

Encapsulation

• notion of taking variables and data structures and wrapping them inside a class definition

Information Hiding

• notion of hiding details of implementation as much as possible

Page 52: Given the description of a problem, how do you determine what classes to define?

52

Why Important?

• protects variables from being modified inappropriately by other classes

• can present classes to others for use by simply specifying interface

ex. You used objectdraw library without knowing any implementation details!

• can change details of class definition without affecting those using it.

Page 53: Given the description of a problem, how do you determine what classes to define?

53

Summary

• Design is a process of iterative refinement

• Good comments/documentation useful for the designer and the reader

• Keep in mind notions of encapsulation and information hiding