บทนำ: ปัญหา “ใครควรทำอะไร?”
เมื่อดูไปที่ Class Diagram คุณเห็นว่า classes มีอะไรบ้าง แต่คำถามที่เกิดขึ้นคือ:
- ใครควรรับผิดชอบ (Responsibility) ในการทำ task นี้?
- ใคร ต้อง collaborate (ร่วมมือ) กับใคร?
- ความสัมพันธ์ระหว่าง objects เป็นอย่างไร?
CRC Card (Class-Responsibility-Collaboration) = เครื่องมือที่ช่วยให้เรา ตัดสินใจว่าใครทำอะไร ในระบบแบบ structured
CRC Card คืออะไร?
CRC Card = บัตรกระดาษ (หรือตาราง) ที่แสดง:
- Class: ชื่อ class
- Responsibility: หน้าที่/ความรับผิดชอบของ class นี้
- Collaboration: class อื่นๆ ที่ต้อง collaborate กับ class นี้
โครงสร้าง CRC Card แบบดั้งเดิม (Physical Card)
text┌─────────────────────────────────────┐
│ Class Name: Student │
├─────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store student information │
│ - Calculate GPA │
│ - Enroll in courses │
│ │
├─────────────────────────────────────┤
│ COLLABORATORS │
│ - Course │
│ - GradeBook │
│ - Enrollment │
└─────────────────────────────────────┘
โครงสร้าง CRC Card แบบตาราง
text┌──────────┬──────────────────────┬────────────────┐
│ Class │ Responsibility │ Collaborator │
├──────────┼──────────────────────┼────────────────┤
│ Student │ Store info │ │
│ │ Calculate GPA │ GradeBook │
│ │ Enroll in course │ Course │
└──────────┴──────────────────────┴────────────────┘
ตัวอย่างที่ 1: Simple E-Commerce System
ขั้นตอนการสร้าง CRC Cards
Step 1: ระบุ Classes หลัก
- Customer
- Order
- Product
- PaymentProcessor
Step 2: กำหนด Responsibilities
text┌────────────────────────────────────────┐
│ Class: Customer │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store customer info (name, email) │
│ - Validate customer data │
│ - Place an order │
│ - View order history │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Order (creates) │
│ - Product (views) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Order │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store order details │
│ - Calculate total price │
│ - Manage order status │
│ - Process payment │
│ - Track items in order │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Customer (belongs to) │
│ - OrderItem (contains) │
│ - Product (via OrderItem) │
│ - PaymentProcessor (uses) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Product │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store product details │
│ - Maintain price information │
│ - Check stock availability │
│ - Update inventory │
│ - Display product info │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - OrderItem (is ordered as) │
│ - Inventory (via stock check) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: PaymentProcessor │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Validate payment method │
│ - Process payment transaction │
│ - Handle payment errors │
│ - Return payment status │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Order (processes payment for) │
│ - BankGateway (communicates with) │
└────────────────────────────────────────┘
ตัวอย่างที่ 2: Library Management System (Advanced)
CRC Cards ที่ละเอียด
text┌────────────────────────────────────────┐
│ Class: LibraryMember │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store member information │
│ - Manage membership status │
│ - Track borrowed books │
│ - Incur fines for late returns │
│ - View available books │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Book (searches/borrows) │
│ - Loan (creates/manages) │
│ - Fine (pays) │
│ - Librarian (interacts with) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Book │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store book metadata │
│ - Report availability status │
│ - Track physical condition │
│ - Manage ISBN and catalog info │
│ - List current borrower │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Loan (is borrowed via) │
│ - LibraryMember (borrowed by) │
│ - BookCatalog (registered in) │
│ - Librarian (managed by) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Loan │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Record loan details │
│ - Calculate due date │
│ - Calculate overdue status │
│ - Manage loan lifecycle │
│ - Generate fine if late │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - LibraryMember (created by) │
│ - Book (for which loan is made) │
│ - Fine (generates if overdue) │
│ - Librarian (manages) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Fine │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Calculate fine amount │
│ - Track payment status │
│ - Store fine reason (overdue/damage) │
│ - Issue fine notification │
│ - Record payment │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Loan (calculates from) │
│ - LibraryMember (pays) │
│ - PaymentProcessor (processes) │
│ - Librarian (issues) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Librarian │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Add new books to system │
│ - Remove books from system │
│ - Verify member identity │
│ - Process checkouts │
│ - Process returns │
│ - Manage member accounts │
│ - Issue fines │
│ - Generate reports │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Book (adds/removes) │
│ - LibraryMember (verifies/manages) │
│ - Loan (manages) │
│ - Fine (issues) │
│ - BookCatalog (updates) │
└────────────────────────────────────────┘
ตัวอย่างที่ 3: Bank Account System
CRC Cards เชิงการทำงาน
text┌────────────────────────────────────────┐
│ Class: BankAccount │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store account information │
│ - Maintain account balance │
│ - Validate transaction requests │
│ - Update balance after transactions │
│ - Report current balance │
│ - Freeze/unfreeze account │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - AccountHolder (owns) │
│ - Transaction (records) │
│ - Ledger (reports to) │
│ - FraudDetection (alerts to) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: Transaction │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Record transaction details │
│ - Timestamp transaction │
│ - Calculate transaction fee │
│ - Validate transaction amount │
│ - Update related account balances │
│ - Track transaction status │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - BankAccount (updates balance) │
│ - TransactionLog (records in) │
│ - FraudDetection (checks against) │
│ - Audit (logs to) │
└────────────────────────────────────────┘
text┌────────────────────────────────────────┐
│ Class: FraudDetection │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Analyze transaction patterns │
│ - Flag suspicious transactions │
│ - Compare against fraud rules │
│ - Alert account holder if needed │
│ - Block high-risk transactions │
│ - Report to compliance team │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Transaction (analyzes) │
│ - BankAccount (may freeze) │
│ - ComplianceTeam (reports to) │
│ - RiskRules (checks against) │
└────────────────────────────────────────┘
ตัวอย่างที่ 4: CRC Card Matrix (Tabular Format)
Hotel Reservation System
text┌───────────────────┬─────────────────────────────┬──────────────────────┐
│ Class │ Responsibility │ Collaborator │
├───────────────────┼─────────────────────────────┼──────────────────────┤
│ Guest │ - Store guest info │ - Reservation │
│ │ - Validate guest data │ - Room │
│ │ - Make reservation │ - Payment │
│ │ - View reservation history │ │
├───────────────────┼─────────────────────────────┼──────────────────────┤
│ Reservation │ - Store reservation details │ - Guest │
│ │ - Calculate total cost │ - Room │
│ │ - Manage reservation status │ - PaymentProcessor │
│ │ - Handle cancellations │ - Notification │
│ │ - Generate confirmation │ │
├───────────────────┼─────────────────────────────┼──────────────────────┤
│ Room │ - Store room info │ - Reservation │
│ │ - Report availability │ - Maintenance │
│ │ - Manage room status │ - RoomService │
│ │ - Track occupancy │ - Housekeeping │
├───────────────────┼─────────────────────────────┼──────────────────────┤
│ PaymentProcessor │ - Validate payment method │ - Reservation │
│ │ - Process payment │ - PaymentGateway │
│ │ - Handle refunds │ - Receipt │
│ │ - Record transaction │ │
├───────────────────┼─────────────────────────────┼──────────────────────┤
│ Notification │ - Send confirmations │ - Reservation │
│ │ - Send reminders │ - Guest │
│ │ - Send cancellations │ - EmailService │
│ │ - Track notification status │ - SMSService │
├───────────────────┼─────────────────────────────┼──────────────────────┤
│ Housekeeping │ - Manage cleaning schedule │ - Room │
│ │ - Report room status │ - Maintenance │
│ │ - Manage staff assignments │ - Staff │
│ │ - Track maintenance issues │ - Notification │
└───────────────────┴─────────────────────────────┴──────────────────────┘
วิธีสร้าง CRC Cards: Step by Step
Step 1: ระบุ Classes
textเริ่มจาก Use Case Diagrams และ Class Diagrams
→ ระบุ classes ทั้งหมดในระบบ
Step 2: ลำดับความสำคัญ
textลำดับสำคัญ:
1. Domain objects (Student, Product, Book)
2. Service/processor objects (PaymentProcessor, FraudDetection)
3. UI/Controller objects (UserInterface, Controller)
4. Utility/Helper objects (Logger, Validator)
Step 3: ระบุ Responsibilities
textสำหรับแต่ละ class ถาม:
- Class นี้ต้องเก็บข้อมูลอะไร?
- Class นี้ต้องคำนวณอะไร?
- Class นี้ต้องตัดสินใจอะไร?
- Class นี้ต้องกระทำอะไร?
Step 4: ระบุ Collaborations
textสำหรับแต่ละ responsibility ถาม:
- Class อื่นไหนต้องเกี่ยวข้อง?
- ใครต้อง "ขอ" service จาก class นี้?
- Class นี้ต้อง "ขอ" service จากใคร?
ตัวอย่างที่ 5: CRC Card Analysis (Detailed)
Order Processing Sequence with CRC
text┌─────────────────────────────────────────────┐
│ 1. Customer Places Order │
│ │
│ CRC: Customer │
│ ├─ Responsibility: Initiate order │
│ ├─ Collaboration: Order │
│ └─ Action: Creates new Order object │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 2. Order Validates Items │
│ │
│ CRC: Order │
│ ├─ Responsibility: Validate items │
│ ├─ Collaboration: Product │
│ └─ Action: Asks Product if available │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 3. Product Checks Stock │
│ │
│ CRC: Product │
│ ├─ Responsibility: Check availability │
│ ├─ Collaboration: Inventory │
│ └─ Action: Queries Inventory │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 4. Order Calculates Total │
│ │
│ CRC: Order │
│ ├─ Responsibility: Calculate price │
│ ├─ Collaboration: OrderItem │
│ └─ Action: Sums up OrderItems │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 5. Order Processes Payment │
│ │
│ CRC: Order │
│ ├─ Responsibility: Process payment │
│ ├─ Collaboration: PaymentProcessor │
│ └─ Action: Delegates to PaymentProcessor │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 6. PaymentProcessor Handles Transaction │
│ │
│ CRC: PaymentProcessor │
│ ├─ Responsibility: Process payment │
│ ├─ Collaboration: BankGateway │
│ └─ Action: Communicates with bank │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 7. Update Product Inventory │
│ │
│ CRC: Product │
│ ├─ Responsibility: Update stock │
│ ├─ Collaboration: Inventory │
│ └─ Action: Decreases stock │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ 8. Send Confirmation │
│ │
│ CRC: Order │
│ ├─ Responsibility: Send confirmation │
│ ├─ Collaboration: Notification, Customer │
│ └─ Action: Notifies customer │
└─────────────────────────────────────────────┘
CRC Cards vs Class Diagrams: ความแตกต่าง
| เกณฑ์ | CRC Card | Class Diagram |
|---|---|---|
| มุมมอง | นักพัฒนา + ทีม | Architecture |
| โฟกัส | “ใครทำอะไร?” | “สร้างจากอะไร?” |
| ประโยชน์ | คิด design | เห็นโครงสร้าง |
| ใช้ตอน | วิเคราะห์ + วางแผน | Implementation |
| รูปแบบ | บัตรกระดาษ/ตาราง | Diagram/visual |
| Collaboration | ชัดเจน | ต้องอ่าน carefully |
Collaboration Patterns (รูปแบบการทำงานร่วมกัน)
Pattern 1: One-way Request
textClassA → ClassB
(ClassA ขอ service จาก ClassB เท่านั้น)
ตัวอย่าง: Order → Product
Pattern 2: Two-way Interaction
textClassA ↔ ClassB
(ทั้งสองขอ service จากกันและกัน)
ตัวอย่าง: Reservation ↔ Guest
Pattern 3: Chain of Responsibility
textClassA → ClassB → ClassC → ClassD
(ส่งต่องาน ไปตามลำดับ)
ตัวอย่าง: Order → PaymentProcessor → BankGateway → Bank
Pattern 4: Hub Pattern
text ↗ ClassB ↖
ClassA ↗ ClassC ↖ ClassD
↗ ClassE ↖
ตัวอย่าง: Order (hub) ↔ Customer, Product, PaymentProcessor, Notification
Best Practices: CRC Cards
| ทำได้ | ไม่ทำ |
|---|---|
| ✓ Keep responsibilities focused | ❌ Too many responsibilities per class |
| ✓ Use clear, action-oriented verbs | ❌ Vague descriptions |
| ✓ List only direct collaborators | ❌ List all possible collaborators |
| ✓ Iterate and refine | ❌ Get it perfect first try |
| ✓ Discuss with team | ❌ Do it alone |
| ✓ Use in design workshops | ❌ Only on paper without discussion |
| ✓ Update when design changes | ❌ Create once and ignore |
ตัวอย่างที่ 6: CRC Cards for Responsible Design
ตัวอย่าง: E-Learning Platform
text┌────────────────────────────────────────┐
│ Class: Course │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store course information │
│ - Maintain list of enrolled students │
│ - Manage course materials │
│ - Report course statistics │
│ - Update course status │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Student (enrolls in) │
│ - Instructor (taught by) │
│ - Lesson (contains) │
│ - Quiz (has) │
│ - Assessment (evaluates via) │
└────────────────────────────────────────┘
Rationale: Course เป็น central entity ที่ manage
ข้อมูลและความสัมพันธ์กับ objects อื่น
text┌────────────────────────────────────────┐
│ Class: Student │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store student profile │
│ - Track enrolled courses │
│ - Submit assignments │
│ - View grades │
│ - Generate progress report │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Course (enrolls in) │
│ - Assignment (submits) │
│ - Grade (receives) │
│ - Notification (receives messages) │
└────────────────────────────────────────┘
Rationale: Student ทำงานร่วมกับ Course เพื่อ
enroll และ Assignment เพื่อ submit
text┌────────────────────────────────────────┐
│ Class: Assessment │
├────────────────────────────────────────┤
│ RESPONSIBILITIES │
│ - Store assessment questions │
│ - Grade student responses │
│ - Calculate scores │
│ - Track assessment status │
│ - Generate feedback │
│ │
├────────────────────────────────────────┤
│ COLLABORATORS │
│ - Course (belongs to) │
│ - Student (completes) │
│ - Grade (produces) │
│ - Rubric (uses for evaluation) │
└────────────────────────────────────────┘
Rationale: Assessment ไม่ควรรับผิดชอบ student
management หรือ course management
มันควรเฉพาะเจาะจง focus ที่ evaluation
สรุป
CRC Cards (Class-Responsibility-Collaboration) เป็นเครื่องมือที่ ช่วยให้ทีมคิดว่า “ใครทำอะไร” ในระบบ:
ประโยชน์:
- ชัดเจน – ทุกคนเห็น responsibilities ของแต่ละ class
- ง่ายต่อการสนทนา – นำมาคุยกับทีมได้เลย
- ตรวจหาปัญหา – พบ overlapping responsibilities ตั้งแต่ออกแบบ
- ยืดหยุ่น – เปลี่ยนแปลงได้ง่าย
ความสัมพันธ์กับ UML:
- Use Case Diagram → ระบุ “ใครใช้อะไร”
- Class Diagram → ระบุ “classes มีอะไร”
- CRC Cards → ระบุ “ใครทำอะไร และคร่วมมือกับใคร”
- Sequence Diagram → ระบุ “ลำดับการทำงาน”
CRC Cards เป็น bridge ที่เชื่อมระหว่าง abstract design และ concrete implementation – ทำให้ abstract concepts กลายเป็นรูปธรรมที่ทีมเข้าใจตรงกัน
เมื่อใช้ CRC Cards อย่างถูกต้อง ระบบที่ออกแบบจะมี clarity ที่ดี, low coupling, และ high cohesion – ซึ่งเป็นพื้นฐานของ good OOP design
