cmsc 202 a design exercise. 2 oo design activities finding the objects defining the responsibilities...

13
CMSC 202 A Design Exercise

Upload: dorthy-griffin

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

CMSC 202

A Design Exercise

Page 2: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

2

OO design activities

• Finding the Objects

• Defining the responsibilities– Services– Attributes

• Discovering relationships

Page 3: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

3

Finding Objects

• In OO design, objects are the building blocks of our system

• Identifying objects is the most important and most difficult step

• Use the things to be modeled (nouns) relying on experience and knowledge of the application domain– Natural, direct, reliable– But, only helps us find tangible objects and terminal

objects (those with no subclasses)

• Other techniques exist

Page 4: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

4

Determining Responsibilities• An object is an abstraction of something in the

problem domain reflecting the capabilities of the system to keep information about it, interact with it, or both

• In OOD, we are interested in an object for its services. From system perspective, an object is defined by it’s public interface.

• But also need to identify the data necessary to support those services

• So, an object is the encapsulation of the of the attribute values (data) and associated methods (services)

Page 5: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

5

What’s an Attribute?

• From analysis point of view it’s an abstraction of a real world characteristic from the problem domain

• From technical point of view, it’s a variable for which each instance of a class (object) holds it’s own value

Page 6: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

6

Identifying Attributes

• What data do we believe the object is responsible for knowing and owning?– Look for adjectives and possessive phrases

• 40-year old man

• Color of the truck

• position of the cursor

Page 7: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

7

Specifying attributes

• Yourdon/Coad – “Make each attribute capture an atomic concept”– Each attribute is either a single value or tightly

related group of variables that the application treats as a whole

• Age

• Address (street, city, zipcode)

• Birthday (month, day, year)

Page 8: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

8

What’s a service?

• Public work an object is willing to perform when requested by another object via message passing

• Defined by prototypes

Page 9: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

9

Identifying Services

• Look for action verbs in the problem.

• English sentence form is usually “subject – action verb – object”

• Example : “A person hit the ball”– For OOD, this means that the Ball object

defines the “receive a hit” service. – Ball must have a prototype service to receive

the “hit” message from the person.

Page 10: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

10

Specifying A Service

• By defining the prototype– Give a generic name– Define the signature by identifying argument

list

Page 11: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

11

Discovering Relationships

• Before object A can send a message to object B (to request a service), object A must have a “handle” to object B.

• One way for the calling object to have such a handle is to have a relationship (aggregation/composition or link) with the other object.

• No object is an island – objects typically depend on other objects for services. These interdependencies are called relationships.

• Relationships carry semantic meaning too

Page 12: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

12

Types of relationships• Generalization/specification

– “is a” (later)

• Aggregation (composition)– “whole-part” or “has a”– Objects composed of other objects– Parts have some functional/structural

relationship to the whole

• Links between objects (physical or conceptual)– Often seen as verbs -- “keeps track of”

Page 13: CMSC 202 A Design Exercise. 2 OO design activities Finding the Objects Defining the responsibilities –Services –Attributes Discovering relationships

13

Simplified BlackJackAn interactive program to play the game of blackjack. The

computer acts as the dealer and will play against the user (like in a casino).

The object of the game is to get your cards as close to 21 without going over. Face cards count 10, Aces count 11.

A hand begins with the player and dealer each getting two cards. The player asks for up to 3 more cards, one at a time, from the dealer until he wishes to stop or the total is over 21. The dealer then draws up to 3 more cards, one at a time while the total of his hand is less than 21. Once the dealers hand reaches 17 or higher, no more cards are dealt. If the dealer’s hand and player’s hand have the same value, or both are over 21, the hand is a draw. Otherwise, the hand closest to 21 wins. Hands are dealt repeatedly until there are not enough cards in the deck to deal a complete hand.