chapter 8 designing classes designing classes –8.1 choosing classes –8.2 cohesion and coupling...

52
Chapter 8 Designing Classes 8.1 Choosing Classes 8.2 Cohesion and Coupling CRC Cards UML Diagrams

Upload: london-gilliom

Post on 29-Mar-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Chapter 8 Designing Classes

–8.1 Choosing Classes–8.2 Cohesion and Coupling–CRC Cards–UML Diagrams

Page 2: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Assignment Chapter 8

Review Exercises (Written)– R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13,

8.15, 8.19, 8.20

Due Thursday, January 10, 2013

CRC Cards – in class assignment to be completed by Friday, Jan 11, 2013

Page 3: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Choosing classes

A class should represent a single concept from the problem domain. – Point– ComplexNumber– Rational– Purse– BankAccount

Page 4: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

Point

What are some responsibilities for the Point class?

Page 5: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

Point– getX– getY– setX– setY– findDistanceFromOrigin– findDistanceFromPoint

Page 6: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

ComplexNumber

What are some responsibilities for the ComplexNumber class?

Page 7: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

ComplexNumber– getReal– getImaginary– add– subtract– multiply

Page 8: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

Rational

What are some responsibilities for the Rational class?

Page 9: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

Rational– getNumerator– getDenominator– add– subtract– multiply– divide– reduce

Page 10: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

Purse

What are some responsibilities for the Purse class?

Page 11: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

Purse– getTotal– getCents– getDollars– addDimes– addNickels– addQuarters– …

Page 12: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

BankAccount

What are some responsibilities for the BankAccount class?

Page 13: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Classes have responsibilities.

BankAccount– getBalance– deposit– withdraw

Page 14: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Choosing Classes A class represents a single concept

from the problem domain Name for a class should be a noun

that describes concept Concepts from mathematics:

PointRectangleEllipse

Concepts from real life BankAccountCashRegister

Page 15: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Choosing Classes - Types Actors (end in -er, -or)–objects do some kinds

of work for you – Scanner– Random // better name: RandomNumberGenerator

Utility classes–no objects, only static methods and constants – Math

Program starters: only have a main method Don't turn actions into classes:

– Paycheck is better name than ComputePaycheck

Page 16: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

The rule of thumb for finding classes is to look for nouns in the problem description

ChessBoard is a good name for a class but

NextMove is not.

Classes

Page 17: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Special Types of Classes Actor Classes

– Do work for you.Random generates random numbers

Utility Classes– Class has no objects but contains a

collection of related methods and constants.

Math has methods abs, sqrt, etc.Math has constant PI.

Classes with one method main whose purpose is to start a program for you.

Page 18: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Buzz words (Kolling)… Coupling

– interconnectedness of classesWe want cooperating classesWe want low degree of coupling

– If we make a change in one class, we do not want to have to change many classes.

Cohesion– One unit of code should be responsible for one

task.method should accomplish one well-defined joba class should represent one type of entity Code duplication is usually a sign of bad

cohesion.

Page 19: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

A good class design…

Think ahead– What might change?

Encapsulate all information about user interface in a clearly defined set of classes.

One class should encapsulate one entity.

One method should do one task

Page 20: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

CRC Cards

Class-Responsibilities-Collaborators A CRC card is an index card created

in informal brainstorming sessions where the behaviors of the application to be modeled are simulated and recorded on the index cards.

Page 21: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

History

Introduced in 1989 by Kent Beck and Ward Cunningham for teaching OOP.

CRC Cards are portable. Promotes good software design. Promotes team work.

Page 22: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

The idea (Adapted from Ward Cunningham Computer Research Laboratory Tektronix, Inc.)

Class Responsibilities Collaborators

Teacher Teach LessonsEvaluates Students

Student

Student Learns Lessons Teacher

Principal Disciplines StudentsHires Staff

TeacherStudent

Page 23: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

A Picture

Class name

Class Responsibilities Collaborators

Page 24: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

An Example

Purse

Class Responsibilities CollaboratorsaddCoinremoveCoingetTotal

Page 25: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

A UML Diagram UML (Unified Modeling Language)

notation for classes and objects describes the dependency relationships among classes.

A "picture."

We will look at class UML Diagrams first.– An object UML diagram underlines the

class name, a class UML diagram does not underline the class name.

Page 26: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

A UML Diagram

Purse

Coin

The Purse class depends on the Coin class.

The Coin class does NOT depend on the Purse class.

dotted arrow with open end pointing to dependent class.

Page 27: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Coupling

If many dependencies exist among classes, we say that coupling is high.

If few dependencies exist among classes, we say that coupling is low.

It is a good practice to minimize the coupling between classes. WHY?

Page 28: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Low Coupling

Page 29: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

High Coupling

Page 30: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

C-R-C

A Class represents a collection of similar objects. A class can be any concept important to the system. Look for nouns in the problem description. The Class name appears across the top of the CRC card.

Page 31: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

C-R-C

A Responsibility is anything that the class knows or does. Responsibilities are things that the class has knowledge about itself, or things the class can do with knowledge it has. Look for verbs in the problem description

The Responsibilities of a class appear along the left side of the CRC card.

Page 32: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

C-R-C

A Collaborator is another class that is used to get information for this class or to complete actions for this class.

The Collaborators of a class appear along the right side of the CRC card.

Page 33: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Procedure

Organize in a group of 2-3 people. Group Responsibilities:

– Brainstorm to find classes in problem domain. (Look for nouns.)

– Filter the list of classes and assign cards to those classes remaining.

– Assign responsibilities to each class (on its index card).

– Attributes CAN be assigned, but should not be emphasized.

Page 34: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Procedure

Group Responsibilities (continued): – Often, as you are brainstorming, the

responsibilities demonstrate the need for additional classes.

– List the collaborators of each class. What other classes does this class interact with? What classes does the object need to fulfill its responsibilities?

Page 35: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Group Responsibilities:

Place cards on table so that the layout is a visual representation of the solution. Classes that collaborate should be near to each other.

Page 36: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Problem Definition This program will simulate the TV

Lottery drawing for the Pick-6 New Jersey Lottery game. In this game, balls are placed in a container that keeps them popping around in some random movements.

The "Lottery Picker" picks a ball from the container (6 times).

The chosen balls are displayed. Create C-R-C Cards and a UML

diagram for the problem.

Page 37: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Candidate classes

Ball Game Picker Container

Ball Container

Picker Game

Page 38: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Lottery

Ball

Class Responsibilities Collaborators

getValue

Page 39: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Lottery

Container

Class Responsibilities Collaborators

holdBalls BallremoveBall

Page 40: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Lottery

Picker

Class Responsibilities CollaboratorschooseBall Ball

(Random Generator)

Page 41: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Lottery

Game

Class Responsibilities Collaboratorsplay Picker

Container

Page 42: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Problem Definition

Coins are placed in a vending machine and a product is selected by pushing a button. If the inserted coins are sufficient to cover the purchase price of the product, the product is dispensed and the change is given. Otherwise the inserted coins are returned.

Create C-R-C Cards and a UML diagram for the problem.

Page 43: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Candidate Classes

VendingMachine Product Coin

Page 44: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Vending Machine Problem

Vending Machine

Class Responsibilities Collaborators HoldProducts Product TakeCoins Coin GiveChange

Page 45: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Vending Machine Problem

Product

Class Responsibilities Collaborators

getPrice getName

Page 46: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Vending Machine Problem

Coin

Class Responsibilities Collaborators

getValue getName

Page 47: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Candidate Classes

VendingMachine Product Coin

Page 48: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Hmmmmm…

Vending Machine– holds products

how many?are there any left?

– makes changewhat if machine can't make exact change?how does it figure out what coins to give

back?

Page 49: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Maybe

We should think of additional classes for this design!

Possibilities?

Page 50: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Problem Definition

Customers order products from a store. Invoices are generated to list the items and quantities ordered, payments received, and amounts still due. Products are shipped to the shipping address of the customer and bills are sent to the customer's billing address.

Create C-R-C Cards and a UML diagram for the problem.

Page 51: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Invoice Problem

Invoice

Customer

Product

Address

Page 52: Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Assignment Chapter 8

Review Exercises (Written)– R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13,

8.15, 8.19, 8.20

Due Thursday, January 10, 2013

CRC Cards Due Friday, January 11, 2013