Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented...

44
Object-Oriented Analysis 1 Object-Oriented Analysis Using CRC Cards and Scenarios to Get Started on a Good Object-Oriented Design Slides from Jochen Rick adapted by Dan Fleck

Transcript of Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented...

Page 1: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 1

Object-Oriented Analysis

Using CRC Cards and Scenarios to Get Started on a Good Object-

Oriented DesignSlides from Jochen Rick adapted by Dan Fleck

Page 2: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 2

Motivation

• It’s all very well understanding what an object is, but it’s much harder to decide what kinds of object you should design in order to implement an application in Smalltalk. Indeed given a set of requirements for an application and a development system like Smalltalk, ‘finding the objects’ is easily the most difficult task an experienced OO developer has to face. —Simon Lewis, The Art and Science of Smalltalk

Page 3: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 3

Good Object-Oriented Style

• You can hack in any language, but how do you get reusable, maintainable code?

• Just using Objects doesn’t ensure a good design– Many C++ programs have only have a single class– That’s not good object-oriented style

• No process can guarantee good results– A good process just makes them more likely

Page 4: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 4

Design is a Process,not a Waterfall

• Design is an iterative activity– Start with Object-Oriented Analysis and Design– Move on to OO Programming– Return to OOA/OOD when necessary

• When creating new functionality• To solve problems with the code

• OO Analysis is just another perspective– Good designers (in any field) shift perspective

frequently to create a better design– The boundary between OOA and OOD is fuzzy.

Page 5: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 5

OOA vs. OOD

• In analysis, we are mostly concerned with the DOMAIN MODEL. What are the objects in the domain and how do they collaborate.

• In design, we need to integrate an APPLICATION MODEL. What objects do I need to add to get this thing to run on a computer and to be realized in some programming language?

Page 6: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 6

Object-Oriented Analysis• CRC Cards by Ward Cunningham• Goal: Understand the Domain as Objects

– Object-Oriented Analysis is Language Independent– Force Developers to “think” in objects

• Steps1. Brainstorm Candidate Classes2. Create Initial Class-Responsibility-Collaboration Cards3. Come up with scenarios of use in the domain4. Use Scenarios and role playing to refine CRC Cards

• When do we do it?– Before we begin coding (to get a good start)– To add new functionality (update CRC cards and scenarios regularly)

Page 7: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 7

OOA Step 1Brainstorming Candidate Classes

• Write down all the objects that relate– Domain Analysis– Focus on the nouns (objects are nouns)– Good objects will have attributes and services

• Now, filter and refine the candidates– Deal with the interface later (Not part of the

domain, that’s the application model)– Are some candidates attributes of others?– Are some subclasses of others?– Are some instances of others?

Page 8: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 8

Brainstorming Tips

• Two’s only company, Ten’s a crowd

• Don’t forget the user

• All ideas are potentially good

• Analyze – make connections

• Think fast – ponder later

• Every voice gets a turn

• Relax – humor is OK.

Page 9: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 9

Candidate Class Review

• Has clear unambiguous name, recognized by domain experts

• Has name that fits in with other systems developed by your organization

• Uses singular noun for name• Begins with Uppercase Letter• Has responsibilities (what NOT how)• Remembers (knowledge)• Is needed (collaborates)• Actively participates

from CRC Card Book

Page 10: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 10

Filter Candidate Classes

• Core Classes (pretty sure these are in Analysis model)

• Undecided Classes (probably not classes – might be attributes)

• Eliminated Classes – (outside scope of system, Application model classes like UI components tied to implementation)

Page 11: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 11

OOA Step 2CRC Cards

• For each core candidate class, create one CRC card

• What’s a CRC Card?– Class-Responsibility-

Collaboration– It’s just a 4x6in index

card

• Analysis vs. Design Information

Page 12: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 12

CRC Cards: What Goes Where?

Page 13: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 13

CRC Card Format (Front)

Class Name (OOA)

Purpose/Role (OOA):

Patterns (OOD):

Stereotypes(OOA/OOD):

Page 14: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 14

CRC Card Format (Back)

Class NameSuperclass: Subclasses:

Responsibilities Collaborators

Page 15: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 15

CRC Card Sample

DocumentPurpose: A Document acts as a container for

graphics and text.

Patterns: Composite-ComponentStereotypes: Structurer

Page 16: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 16

CRC Card Sample

DocumentSuperclass: Subclasses:

Responsibilities CollaboratorsKnow Contents

Know Storage Location

Insert and Removes text,graphics and other elements

Page 17: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 17

Stereotypes

• Information Holder– knows and provides information

• Structurer– Maintains relationships between objects and information about those

relationships

• Service Provider– Performs work for other objects

• Coordinator– Reacts to events by delegating tasks to others

• Controller– Makes decisions and closely directs others’ actions

• Interfacer– Transforms information and requests between different parts of our

system

Page 18: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 18

OOA Step 3Scenarios with CRC Cards

• Invent Scenarios– What should these objects do?– What if…?

• Play the Cards– Assign Roles– Go Through Scenario– Write down new responsibility– Add collaborator objects to help with that

responsibility– Team members hold up cards as they participate

Page 19: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 19

Scenario Guidelines

• Concrete:– Bob tries to Login to the system with an

incorrect password.– Sally creates a new Sorceress character

and chooses auto-configuration.

• Focus on “must do” items first

• Start easy and move to complex

Page 20: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 20

Scenario Guidelines

• Keep a record of scenarios played out

• Explore exception conditions last

• Separate role-play from analysis

Page 21: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 21

OOA for a Clock

• We want to make a clock. The clock should:– Have a way to set the current time– Display the time in hours, minutes, and

seconds in different formats– Update the time to keep it current

Page 22: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 22

OOA for a Clock

• Brainstorm (Step 1 – Candidate Classes)– Time– Counter– , Clock, Display– , – , TimeZones,

Page 23: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 23

OOA for a Clock

• Brainstorm– Face for the clock, internal ticker, hours, minutes,

seconds, knob for setting the clock

• Filter for Core Objects

Page 24: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 24

OOA for a Clock

• Brainstorm

• Filter

• Define the Clock class– Attributes: hours, mins, secs, timezone,

state (low bat, etc…), – Services: update, setTime, alarm,

setAlarm, display/time, displayDate, setAmPm, getAmPm

Page 25: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 25

OOA for a Clock

• Brainstorm– Face for the clock, internal ticker, hours, minutes,

seconds, knob for setting the clock

• Filter – Ticker, hours, minutes, seconds

• Define the Clock class– Attributes: seconds, minutes, hours, displayFormat– Services: get/set, nextSecond, display, setFormat

Page 26: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 26

ALERT!!!!

• Bad OO Design in Progress

• What did we do wrong?

I’m still designing a program – what’s my data and what do I have to do?

I need to think about a community of cooperating objects!!!

Page 27: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 27

Errors in this Design

• We assumed there was only one class: Clock– What’s reusable in that?

• We started with data and not with what the object should be responsible for

• We were way too focused on the programming from the start

• Thinking in get/set mode (be aware of your idioms!)– Java/Python does that, but typically not Smalltalk

Page 28: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 28

OOA for a Clock: Second Try

• Brainstorm objects for Clock– Display, Time, Ticker/SecondTimer, Clock,

Formatter

• Filter:• CRC Cards

– Two scenarios1. When the ticker pulses the clock, the internal

representation of time must increment2. When a display is requested, the time must be fetched

and formatted

Page 29: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 29

Scenario 1: The Ticker Ticks

• The SecondsTicker pulses the Clock

Page 30: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 30

Scenario 1: The Ticker Ticks

• The SecondsTicker pulses the Clock

• The Clock updates Time

Page 31: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 31

Scenario 1: The Ticker Ticks

• The SecondsTicker pulses the Clock

• The Clock updates Time

• Time updates itself

Page 32: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 32

Scenario 1: The Ticker Ticks

• The SecondsTicker pulses the Clock

• The Clock updates Time

• Time updates itself

End of Scenario

Page 33: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 33

Scenario 2: Clock Responds with the Time

• Display the time

Page 34: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 34

Scenario 2: Clock Responds with the Time

• Display the time• Return hours,

minutes, & seconds

Page 35: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 35

Scenario 2: Clock Responds with the Time

• Display the time• Return hours,

minutes, & seconds• Translate the time

into the display format

Page 36: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 36

Scenario 2: Clock Responds with the Time

• Display the time• Return hours,

minutes, & seconds• Translate the time

into the display format

End of Scenario

Page 37: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 37

OOA for a Clock: Second Try

Page 38: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 38

Why CRC Cards?

• Help you identify objects and their responsibilities

• Help you understand how the objects interact• Cards form a useful record of design activity• Cards work well in group situations and are

understandable by non-technical stakeholders.

Page 39: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 39

In-class Exercise

• This application will support the operations of a technical library for an R&D organization. This includes the searching for and lending of technical library materials, including books, videos, and technical journals. Users will enter their company ids in order to use the system; and they will enter material ID numbers when checking out and returning items.Each borrower can be lent up to five items. Each type of library item can be lent for a different period of time (books 4 weeks, journals 2 weeks, videos 1 week). If returned after their due date, the library user's organization will be charged a fine, based on the type of item( books $1/day, journals $3/day, videos $5/day).Materials will be lent to employees with no overdue lendables, fewer than five articles out, and total fines less than $100....(Design Constraints)...

Page 40: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 40

In-class Exercise: Handout

• Brainstorm Candidate Classes - Whole Class• Candidate Class Review - Whole class• Filter Classes - Individual Groups• Create the cards - Individual Groups• Information Holder - knows and provides information• Structurer - Maintains relationships between objects and information about those relationships• Service Provider - Performs work for other objects• Coordinator - Reacts to events by delegating tasks to others• Controller - Makes decisions and closely directs others’ actions• Interfacer - Transforms information and requests between different parts of our system

• Play the cards - Individual Groups– Scenarios -

• What happens when Johnny Codewarrior returns the book Document, Your job depends on it two days late? He has no other item checked out and no acquired fines.

• What happens when Ivar Jacobson uses the search feature to look for the book Object-Oriented Software Engineering of which there are 2 copies available?

• What happens when Grady Booch uses the search feature to look for the book Object-Oriented Analysis and Design with Application of which there is 1 copy available and the database is down?

• Present Final Card Set - Individual Groups

Document

Purpose: …

Patterns:

Stereotypes: …

Document

Purpose: …

Patterns:

Stereotypes: …

Page 41: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 41

OO Analysis Exercise:Checkers

• Checkers is played by two people, one with light and one with dark pieces.

• Pieces move diagonally and opponents are captured by jumping over them.

Page 42: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 42

OO Analysis Exercise:Checkers

• Things to keep in mind– http://www.jimloy.com/checkers/rules2.htm – A Piece can only move and capture forward.– When a piece makes it to the other side of the

board, it becomes a king and can move diagonally in any direction as far as it wants.

– Capturing is mandatory. A piece (or king) that is captured is removed from the board.

– The player who has no pieces left or cannot move anymore has lost the game.

Page 43: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 43

Candidate Classes

• Brainstorming:

Page 44: Object-Oriented Analysis1 Using CRC Cards and Scenarios to Get Started on a Good Object- Oriented Design Slides from Jochen Rick adapted by Dan Fleck.

Object-Oriented Analysis 44

Scenarios

• Bob moves first in the new game and moves a checker with a legal move.

• Late in the game, Sally moves a checker into Bob’s home row, changing it to a King.

• Sally takes Bob’s last checker, ending the game.