Updated on 25 Jun, 202616 mins read 18 views

At this point, we know how to:

Analyze requirements
Discover objects
Identify responsibilities
Build domain models
Apply GRASP principles

But we still have a major problem.

Suppose we discover:

Customer
Order
Product
Payment

Questions 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 exists

But not:

How objects work together

This 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
Interactions

using simple index cards.

Surprisingly:

Many experienced architects still use CRC-style thinking today.

What Is a CRC Card?

CRC stands for:

Class
Responsibility
Collaborator

A 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
Object

Example:

Order

2 Responsibility

Represents:

What the object knows

What the object does

Example:

Calculate Total

Manage Items

Track Status

3 Collaborator

Represents:

Objects required to perform responsibilities

Example:

Product
Payment

Example 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
Loan

These 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
DeliveryPartner

Step 2: Identify responsibilities.

Customer:
	Place Order
	Cancel Order
	Track Order
Order:
	Maintain Status
	Calculate Price
	Assign Delivery Partner
Restaurant:
	Accept Order
	Reject Order
	Prepare Food
DeliveryPartner:
	Accept Delivery
	Delivery Food

Step 3: Identify Collaborators

Customer needs Order
Order needs Restaurant, DeliveryPartner, Payment
Restaurant needs Order
DeliveryPartner needs Order

Final 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
Product

Looks fine.

Then:

Order contains multiple products with quantities.

Question:

Where should quantity live?

Not:

Order
Product

Now concept appears:

OrderItem

CRC reveals missing class.

Order
    |
contains
    |
OrderItem
    |
references
    |
Product

This happens constantly in real projects.

 

Buy Me A Coffee

Leave a comment

Your email address will not be published. Required fields are marked *