At this point, we know how to:
Analyze requirements
Discover objects
Identify responsibilities
Build domain models
Apply GRASP principlesBut we still have a major problem.
Suppose we discover:
Customer
Order
Product
PaymentQuestions remain:
Who talks to whom?
How does an Order get created?
Who calculates totals?
Who performs payment?
How do objects collaborate?Domain modeling tells us:
What existsBut not:
How objects work togetherThis is where CRC Cards enter the picture.
Historical Context
CRC Cards were introduced by: Ward Conningham and Kent Beck
during the late 1980s.
The goal was simple;
Instead of immediately writing code:
class Order
{
};Designers would first think about:
Responsibilities
Collaborations
Interactionsusing simple index cards.
Surprisingly:
Many experienced architects still use CRC-style thinking today.
What Is a CRC Card?
CRC stands for:
Class
Responsibility
CollaboratorA CRC Card is a lightweight modeling tool.
Structure:
+------------------------+
| Class Name |
+------------------------+
| Responsibilities |
| |
| |
+------------------------+
| Collaborators |
| |
+------------------------+The Core Idea
Every class should answer:
Who am I?
What am I responsible for?
Who do I need help from?That's it.
The Three Components
1 Classs
Represents:
Concepts
Entity
ObjectExample:
Order2 Responsibility
Represents:
What the object knows
What the object doesExample:
Calculate Total
Manage Items
Track Status3 Collaborator
Represents:
Objects required to perform responsibilitiesExample:
Product
PaymentExample 1
Library System
Requirement
Members borrow books.Potential CRC Card:
+----------------------+
| Member |
+----------------------+
| Borrow Book |
| Return Book |
| View Loans |
+----------------------+
| Book |
| Loan |
+----------------------+Question:
Can Member perform these actions alone?
No.
Needs:
Book
LoanThese become collaborators.
Order System
Class: Order
+----------------------+
| Order |
+----------------------+
| Add Item |
| Remove Item |
| Calculate Total |
| Mark Paid |
+----------------------+
| Product |
| Payment |
+----------------------+Interpretation:
Order can:
- Manage items
- Calculate totals
- Track payment state
But requires:
- Products
- Payments
to complete its work.
CRC Card Walkthrough
Food Delivery System
Requirement:
Customer places order.
Restaurant prepares order.
Delivery partner delivers order.Step 1: Discover classes.
Customer
Order
Restaurant
DeliveryPartnerStep 2: Identify responsibilities.
Customer:
Place Order
Cancel Order
Track OrderOrder:
Maintain Status
Calculate Price
Assign Delivery PartnerRestaurant:
Accept Order
Reject Order
Prepare FoodDeliveryPartner:
Accept Delivery
Delivery FoodStep 3: Identify Collaborators
Customer needs Order
Order needs Restaurant, DeliveryPartner, Payment
Restaurant needs Order
DeliveryPartner needs OrderFinal CRC Cards:
Customer:
+----------------------+
| Customer |
+----------------------+
| Place Order |
| Cancel Order |
| Track Order |
+----------------------+
| Order |
+----------------------+
Order:
+----------------------+
| Order |
+----------------------+
| Calculate Price |
| Maintain Status |
| Assign Driver |
+----------------------+
| Restaurant |
| DeliveryPartner |
| Payment |
+----------------------+
Restaurant:
+----------------------+
| Restaurant |
+----------------------+
| Accept Order |
| Reject Order |
| Prepare Food |
+----------------------+
| Order |
+----------------------+Discovering Missing Classes
One of the biggest strengths of CRC Cards.
Requirement:
Customer places order.
Order contains products.Initial model:
Customer
Order
ProductLooks fine.
Then:
Order contains multiple products with quantities.Question:
Where should quantity live?
Not:
Order
ProductNow concept appears:
OrderItemCRC reveals missing class.
Order
|
contains
|
OrderItem
|
references
|
ProductThis happens constantly in real projects.
Leave a comment
Your email address will not be published. Required fields are marked *
