Responsibility Driven Design with CRC cards

Download Report

Transcript Responsibility Driven Design with CRC cards

Responsibility Driven Design
with CRC cards
Richard Gesick
SWE 4743
• Analysis (or requirements analysis)
involves studying the problem to be
solved. It identifies what the problem is
without addressing how it will be solved.
•
• Design is the creative process that
transforms a problem into a solution. It
identifies how the problem will be
solved.
SWE 4743
2- 18
• The boundaries between analysis and design
are fuzzy, although the focus of each is quite
distinct.
• In analysis, we seek to model the world by
discovering the classes and objects that form
the vocabulary of the problem domain,
• in design, we invent the abstractions and
mechanisms that provide the behavior that
this model requires.
SWE 4743
3- 18
IDENTIFYING CLASSES AND OBJECTS
• Write an English description of the
problem and underline the nouns and
verbs.
• Consider the nouns as candidate
objects.
• Consider the verbs as candidate
operations upon the objects
SWE 4743
4- 18
RESPONSIBILITY-DRIVEN DESIGN
• Design technique driven by the delegation of
responsibilities
• Goal -- To focus on building a model of interacting
objects before filling out precise details of each
class
• Involves reducing data & control dependencies
between modules (information hiding) and
designing reusable code
SWE 4743
5- 18
• Philosophy -- No action can take place
without an agent (object) performing the
action.
• Central focus of OOD -- establishing
WHO is responsible for each action
SWE 4743
6- 18
CRC cards:
• CRC cards: Class, Responsibility, &
Collaboration
• An object’s responsibilities are high-level
statements about the knowledge it
maintains and the operations it supports
• A collaboration is a request made by
one object to another
SWE 4743
7- 18
Each class = a CRC card
class name
_______________________________
[subclass: ]
[superclass: ]
responsibilities
| collaborators
|
|
SWE 4743
8- 18
CRC Elements
• Class:Choose meaningful names (they influence
the design)
• Responsibilities:
• Describe the problem to be solved
• Use short verb phrases
• Show WHAT is to be done and not HOW it is to be
done
• View them as a contract
• They should fit on the card -- may require moving
• responsibilities or dividing the task
SWE 4743
9- 18
CRC Elements
• Collaborators:
• All classes of which the class needs to be
aware
• MUST include all classes that provide
services
• MAY include classes that require services
provided by this class
SWE 4743
10- 18
Types of classes
• Data managers, data, or state classes:
– Responsibility-- to maintain data or state
information
– The NOUNS in the problem description
– The fundamental building blocks of the design
• Data sinks or data sources:
– Classes that GENERATE data or ACCEPT
data and then PROCESS them further
– They don't hold the data for a period of time
SWE 4743
11- 18
Types of classes
• View or observer classes:
– Classes used to display information
– Display behavior is isolated in separate
classes from those classes that maintain the
data being displayed
– The actual base data is called the MODEL and
the display class is called the VIEW
– One model may have more than one view
SWE 4743
12- 18
Types of classes
• Facilitator or helper classes:
– These classes maintain little or no state
information themselves
– They assist in the execution of complex tasks
SWE 4743
13- 18
DESIGN PROCESS:
Discover classes and define what
responsibilities are assigned to each class
– Start from obvious classes and progress to the
unknown by simulating SCENARIOS
– Assign each action as a responsibility to a specific
object (place on a crc card)
– Different scenarios generate more
responsibilities.
– Task becomes one of developing a CLEAR
DESCRIPTION and an UNDERSTANDING of
each component of the system.
SWE 4743
14- 18
DESIGN PROCESS:
• Decide HOW those responsibilities are to be
achieved
– Any value accessed or modified widely or that
exists for a significant period of time should be
MANAGED; (one class has responsibility for the
actions taken to view or modify the data)
– Add the data values managed by the class
(hidden data) to the back of the crc card
SWE 4743
15- 18
DESIGN PROCESS:
• Discover relationships between classes to
identify responsibilities :
– Find IS-KIND-OF relationships to facilitate reuse -- If a
class is a kind of another class, it is an IS-A relationship.
This allows you to assign responsiblities to the
superclass.
– Find IS-ANALOGOUS-TO relationships -- If two or more
classes have some of the same responsibilities, it is
usually a sign that they share a superclass.
– Find IS-PART-OF relationships -- Identifying a clear
distinction between whole and part helps to determine
where responsibilities should be.
SWE 4743
16- 18
DESIGN PROCESS:
• . Discover relationships between classes to
identify collaborations :
– Find IS-PART-OF relationships -- Whole-part
relationships come in two types:
– Composite class and the objects that compose it -- The
composite class is responsible for knowing about its parts
(i.e., a collaboration); (e.g., a car must collaborate with its
part steering wheel, which turns the wheels, in order to
fulfill its responsibility of turning)
– Container class and its elements -- May or may not require
a collaboration, depends on the domain. (e.g., arrays
contain elements but do not need to send messages to
them)
SWE 4743
17- 18
DESIGN PROCESS:
– Find HAS-KNOWLEDGE-OF relationships -- Implies there
is a responsibility to know information. One class knows
about another class because it needs to get information
from that class.
– Find DEPENDS-UPON relationships -- Usually indicated in
the specification by “changes with” implying a connection
between classes. (e.g., a drawing changes with the
addition of drawing elements; the drawing depends upon
the structure of the drawing elements)
SWE 4743
18- 18