responsibility

Download Report

Transcript responsibility

Overview of CRC
CS 4311
Chapters 3 and 4 of
Wirfs-Brock, R., Wilkerson, B., and Wiener, L., Designing ObjectOriented Software, Prentice Hall, 1990.
CS 4311
1
Outline

Basics of CRC


Identifying Objects (Classes)
 Identifying Responsibilities
 Assigning

CS 4311
2
CRC Cards



Invented in 1989 by Kent Beck and Ward
Cunningham
A simple yet powerful object-oriented
(analysis/design) technique
Uses a collection of (standard index) cards that
are divided into three sections:



CS 4311
3
Class, Responsibility, and
Collaborator

A responsibility is anything that a class
knows or does.
 A collaborator is another class that is used
to get information for, or performs actions
for the class at hand.

CS 4311
4
Example
Class: Person
Superclass:
Subclasses: Student
Responsibilities
Knows name
Knows address
Knows phone number
…
CS 4311
Collaborations
AddressBook
PhoneBook
5
More on CRC Cards

3x5 (or 4x6) index cards, post-its, etc.

In addition to CRC, can also write
superclasses and subclasses.
 On the back, can write a description of
purpose of the class (with its attributes).

CS 4311
6
Why CRC Cards?

Easy to arrange and reorganize
 Thus, makes the structure easy to change

 Encourages
experimentation
 Encourages change
CS 4311
7
CRC Approach – The Process

Exploratory phase
 Find
classes
 Determine operations and knowledge for
each class (responsibilities)
 Determine how objects collaborate to
discharge responsibilities

Analysis phase
 Collect
CS 4311
into subsystems
8
How to Find Objects and Their
Responsibilities?

Use nouns and verbs in requirements as clues
 Noun phrases leads to objects
 Verb phrases lead to responsibilities

Determine how objects collaborate to fulfill their
responsibilities


Why is this important?
 Objects lead to classes
 Responsibilities lead to operations
or methods
 Collaborations and roles lead to associations
CS 4311
9
Outline

Basics of CRC
 CRC
Cards
 CRC Process
Identifying Objects (Classes)
 Identifying Responsibilities
 Assigning Responsibilities

CS 4311
10
Identifying Objects (Classes)
Start with requirements specification
1. Look for noun phrases.

2.
Refine to a list of candidate classes.
CS 4311
11
Guidelines for Refining Candidate
Classes

Model physical objects –

Model conceptual objects – e.g., windows, files,
shots, picks.
Choose one word for one concept – what does it
mean within the domain?
Be wary of adjectives – does it really signal a
separate class?


CS 4311
12
Guidelines for Refining (Cont.)




Be wary of missing or misleading subjects –
rephrase in active voice.
Model categories of classes – delay modeling of
inheritance.
Model interfaces to the system – e.g., user
interface, program interface.
Model attribute values, not attributes – e.g.,
customer vs. customer address.
CS 4311
13
Example: Mail-Order Software
Imagine that you are developing order-processing software for a mailorder company, which is a reseller of products purchased from various
suppliers.




CS 4311
Twice a year the company publishes a catalog of products,
which is mailed to customers and other interested people.
Customers purchases products by submitting a list of
products with payment to the company. The company fills
the order and ships the products to the customer’s
address.
The order processing software will track the order from the
time it is received until the product is shipped.
The company will provide quick service. They should be
able to ship a customer’s order by the fastest, most
efficient means possible.
14
Example: Mail-Order Software
Imagine that you are developing order-processing software for a mailorder company, which is a reseller of products purchased from various
suppliers.




CS 4311
Twice a year the company publishes a catalog of products,
which is mailed to customers and other interested people.
Customers purchases products by submitting a list of
products with payment to the company. The company fills
the order and ships the products to the customer’s
address.
The order processing software will track the order from the
time it is received until the product is shipped.
The company will provide quick service. They should be
able to ship a customer’s order by the fastest, most
efficient means possible.
15
Candidate Classes
Nouns and Synonyms
Candidate Class Name
software
mail-order company, company, reseller
products
suppliers
catalog of products
customers, interested people
list of products, order, customer’s order
payment
customer’s address
time it is received
time products is shipped
quick service
-: don’t model the system
Company (?: model ourselves)
Product (+: core concept)
Supplier (+: core concept)
Catalog (+: core concept)
Customer (+: core concept)
Order (+: core concept)
Payment (+: core concept)
Address (?: customer’s attribute)
-: attribute of Order
-: attribute of Order
-: attribute of Company
CS 4311
16
Candidate Classes (Cont.)

Expect the list to evolve as design
proceeds
 Record
why you decided to include or reject
candidates
 Candidate class list follows configuration
management and version control
CS 4311
17
A Good Class …

Has a name that is recognizable by
domain experts
 Begins with an uppercase letter and is a
singular noun
 Has responsibilities
 May actively participates in system

CS 4311
18
Outline

Basics of CRC
 CRC
Cards
 CRC Process
Identifying Objects (Classes)
 Identifying Responsibilities
 Assigning Responsibilities

CS 4311
19
What Are Responsibilities?

The public services that an object may provide
to other objects:
 The


knowledge an object maintains and provides
That is,
 Convey
a sense of purpose of an object and its place
in the system
 Record services that a class provides to fulfill roles
within the system
 Record knowledge (maintenance) and manipulation
of information in the system
CS 4311
20
Knowledge and Action

Knowing responsibilities
 Knowing
about private encapsulated data

 Knowing

about things it can derive or calculate
Doing responsibilities
 Doing
something itself, such as creating an object or
doing a calculation
 Initiating action in other objects
 Controlling and coordinating activities of other objects
CS 4311
21
Identifying Responsibilities
Use mixtures of:
 Verb phrase identification. Similar to noun phrase
identification, except verb phrases are candidate
responsibilities.
 Scenarios and role play. Perform scenario walkthrough of the system where different persons “play”
the classes, thinking aloud about how they will
delegate to other objects.
 Class enumeration. Enumerate all candidate classes
and come up with an initial set of responsibilities.
 Class relationship examination. Examine all classes
and their relationships to compare how they fulfill
responsibilities.
CS 4311
22
Example of Verb Phrase
Identification
Imagine that you are developing order-processing software for a
mail-order company, which is a reseller of products purchased
from various suppliers.




CS 4311
Twice a year the company publishes a catalog of products,
which is mailed to customers and other interested people.
Customers purchases products by submitting a list of
products with payment to the company. The company fills
the order and ships the products to the customer’s
address.
The order processing software will track the order from the
time it is received until the product is shipped.
The company will provide quick service. They should be
able to ship a customer’s order by the fastest, most
efficient means possible.
23
Example of Verb Phrase
Identification
Imagine that you are developing order-processing software for a
mail-order company, which is a reseller of products purchased
from various suppliers.




CS 4311
Twice a year the company publishes a catalog of products,
which is mailed to customers and other interested people.
Customers purchases products by submitting a list of
products with payment to the company. The company fills
the order and ships the products to the customer’s
address.
The order processing software will track the order from the
time it is received until the product is shipped.
The company will provide quick service. They should be
able to ship a customer’s order by the fastest, most
efficient means possible.
24
Candidate Responsibilities
Verbs and Synonyms
Candidate Responsibility
Publishes a catalog
Is mailed to customers
Purchases products; submitting
Fills the order
Ships the products; is shipped
Track the order
Is received
Provides quick service
Create a catalog (+: core)
Mail a catalog (+: core)
?: Receive an order
Process an order (+: core)
Ship a product (+: core)
?: Track an order
?: Receive products
-: outside the domain
CS 4311
25
Examining Class Relationships
To identify more responsibilities
 Types of relations:

 Is-kind-of:
 Is-analogous-to:
subclasses of same
superclass?
 Is-part-of: not inheritance
CS 4311
26
Is-kind-of Relationship
Often indicates superclass and subclass
 Try to identify common attributes or
actions

CS 4311
27
Is-analogous-to Relationship
May indicate superclass and subclass.
 Look for common or similar responsibilities.

CS 4311
28
Is-part-of Relationship

Not superclass and subclass, i.e., the
whole need not act like the parts.


Object composed of other objects need to
know about its parts.
CS 4311
29
Outline

Basics of CRC
 CRC
Cards
 CRC Process
Identifying Objects (Classes)
 Identifying Responsibilities
 Assigning Responsibilities

CS 4311
30
Assigning Responsibilities

Heuristics
 H1:
Distribute intelligence.
 H2: State responsibilities as generally as
possible.
 H3: Keep behavior with related information.
 H4: Keep information about one thing in one
place.
 H5:
CS 4311
31
Heuristics
H1: Distribute intelligence.

What’s intelligence?



Intelligence is what the system knows, actions that it can perform,
and impact it has on other systems and users with which it
interacts
A class gets intelligence based on how much it knows or can do,
and how many other objects it can affect; it often amounts to the
number of responsibilities assigned for the class.
Why distribute?

CS 4311
To make responsibilities smart.
32
Centralized vs. Distributed
Intelligence

Centralized control (extreme)
 One
object would know as much as possible about
the system, including flow of control.
 All other objects would be devoid of intelligence.
 Disadvantages: makes modification difficult; less
behavior an object has, more unintelligent classes
needed to implement same system.
CS 4311
33
Centralized Control
logic begins
and ends here
CS 4311
objects hold
information
34
Centralized vs. Distributed (Cont.)
 Distributed
intelligence
 Each
object needs to know relatively fewer
things
 Produces a more flexible system
 Ensure that responsibilities are written at
the same level of detail
CS 4311
35
Distributed Control
no one object knows
or does much
logic begins
here
CS 4311
logic
ends here
36
Delegated Control
coordinators
manage each step
logic begins
here
CS 4311
objects knows
and do a substep
logic
ends here
37
Heuristics (Cont.)
H2: State responsibilities as generally as
possible.
To find common responsibilities that can
be shared.
 To make responsibilities reusable.

CS 4311
38
Heuristics (Cont.)
H3: Keep behavior with related
information



CS 4311
If information changes, no update messages
must be sent between objects.
The object that needs to know that something
has changed will know it.
In sum, this makes responsibilities efficient.
39
Heuristics (Cont.)
H4: Keep information about one thing in
one place.
Distribution of information often leads to
a duplication, and thus inconsistency.
 In sum, make responsibilities safe.

CS 4311
40
Heuristics (Cont.)
H5: Share responsibilities.
A certain responsibility or a compound
responsibility can often be best divided
or shared among a few closely related
objects.
 In sum, this make responsibilities simple.

CS 4311
41
Example: Mail-Order System
Class
Responsibility
Product
Supplier
Catalog
Customer
Order
Payment
Create a catalog
Mail a catalog
Process an order
Ship a product
Track an order
? Company
? Address
CS 4311
?: Receive an order
42
Group Exercise
1.
Based on the requirement specification
provided, identify classes by dividing them into
the following three categories:



2.
Obvious (+)
Discarded (-)
Unsure (?)
Repeat the exercise for candidate
responsibilities.
CS 4311
43