Relationships Between Classes
- Dependency ("uses")
- Aggregation ("has")
- Inheritance ("is")
Dependency
- Uses relationship
- C depends on D: Method of C manipulates objects of D
- If C doesn't use D, then C can be developed without knowing about D
- Example: Mailbox depends on Message
Coupling
Aggregation
- Has-a relationship
- Object of a class contains objects of another class
- Example: MessageQueue aggregates Messages
- Example: Mailbox aggregates MessageQueue
- Implemented through instance fields
- Aggregation is a stronger form of dependency
Multiplicities
Inheritance
- Is-a relationship
- Relationship between a more general class (superclass) and a more specialized class (subclass)
- Subclass supports all method interfaces of superclass (but
implementations may differ)
- Subclass may have added methods, added state
- Subclass inherits from superclass
- Example: ForwardedMessage inherits from Message
- Example: Greeting does not inherit from Message
- It is sometimes abused
- Should the class Tire be a subclass of a class Circle?
- The has-a relationship would be more appropriate
Use Cases
- Analysis technique
- Each use case focuses on a specific scenario
- Use case = sequence of actions
- Action = interaction between actor and computer system
- Each action yields a result
- Each result has a value to one of the actors
- Use variations for exceptional situations
Sample Use Case
Leave a Message
- Caller dials main number of voice mail system
- System speaks prompt
Enter mailbox number followed by #
- User types extension number
- System speaks
You have reached mailbox xxxx. Please leave a message now
- Caller speaks message
- Caller hangs up
- System places message in mailbox
Sample Use Case -- Variations
Variation #1
1.1. In step 3, user enters invalid extension number
1.2. Voice mail system speaks
You have typed an invalid mailbox number.
1.3. Continue with step 2.
Variation #2
2.1. After step 4, caller hangs up instead of speaking message
2.3. Voice mail system discards empty message
CRC Cards
- CRC = Classes, Responsibilities, Collaborators
- Developed by Beck and Cunningham
- Use an index card for each class
- Class name on top of card
- Responsibilities on left
- Collaborators on right
CRC Cards
- Responsibilities should be high level
- 1 - 3 responsibilities per card
- Collaborators are for the class, not for each responsibility
Walkthroughs
- Use case: "Leave a message"
- Caller connects to voice mail system
- Caller dials extension number
- "Someone" must locate mailbox
- Neither Mailbox nor Message can do this
- New class: MailSystem
- Responsibility: manage mailboxes