บทนำตอนนี้: Behavior Diagrams
ถ้า Class Diagram แสดง โครงสร้าง แล้ว Behavior Diagrams แสดง การทำงาน ของระบบ
ตอนนี้เราจะศึกษา:
- Use Case Diagram – ฟังก์ชันของระบบ (requirements)
- Activity Diagram – ขั้นตอนการทำงาน (workflow)
- Sequence Diagram – ลำดับการสื่อสารตามเวลา
- State Machine Diagram – สถานะของ object
- บทนำตอนนี้: Behavior Diagrams
- Use Case Diagram: ฟังก์ชันของระบบ
- Use Case Diagram คืออะไร?
- โครงสร้าง Use Case Diagram
- 1. Actor (ผู้ใช้/ระบบภายนอก)
- 2. Use Case (ฟังก์ชัน)
- 3. System Boundary
- Use Case Relationships
- 1. Association (เชื่อมโยง)
- 2. Include (รวม)
- 3. Extend (ขยาย)
- 4. Generalization (ทั่วไป → เฉพาะ)
- ตัวอย่างที่ 1: Online Examination System
- Activity Diagram: ขั้นตอนการทำงาน
- Activity Diagram คืออะไร?
- โครงสร้าง Activity Diagram
- 1. Activity (การกระทำ)
- 2. Transition (ส่วนเชื่อม)
- 3. Decision (เงื่อนไข)
- 4. Fork/Join (ทำพร้อม)
- ตัวอย่างที่ 2: Online Shopping Process
- Sequence Diagram: ลำดับการสื่อสารตามเวลา
- Sequence Diagram คืออะไร?
- โครงสร้าง Sequence Diagram
- 1. Actor/Object (แนวตั้ง)
- 2. Message (แนวนอน)
- 3. Activation Box (กล่องการทำงาน)
- ตัวอย่างที่ 3: Login Process
- ตัวอย่างที่ 4: ATM Withdrawal
- State Machine Diagram: สถานะของ Object
- State Machine Diagram คืออะไร?
- โครงสร้าง State Machine Diagram
- 1. State (สถานะ)
- 2. Initial/Final State
- 3. Transition
- ตัวอย่างที่ 5: Door State Machine
- ตัวอย่างที่ 6: Order Processing State Machine
- ตัวอย่างสมบูรณ์: E-Commerce System (ทั้งหมด)
- 1. Use Case Diagram
- 2. Activity Diagram: Place Order Process
- 3. Sequence Diagram: Order Checkout
- 4. State Machine Diagram: Order Lifecycle
- 5. Class Diagram: Data Model
- สรุปตอนที่ 2
Use Case Diagram: ฟังก์ชันของระบบ
Use Case Diagram คืออะไร?
Use Case Diagram แสดง:
- Actors (ผู้ใช้/ระบบภายนอก)
- Use Cases (ฟังก์ชันของระบบ)
- ความสัมพันธ์ระหว่างกัน
ใช้เมื่อ: เก็บ requirements, สื่อสารกับ client
โครงสร้าง Use Case Diagram
1. Actor (ผู้ใช้/ระบบภายนอก)
text ┌──────┐
│(icon)│
│ User │ ← Stick figure = Actor
└──────┘
ตัวอย่าง:
textStudent Teacher Admin External System
│ │ │ │
○ ○ ○ ▭
2. Use Case (ฟังก์ชัน)
text ┌─────────────────┐
│ Login System │ ← Oval = Use Case
└─────────────────┘
ตัวอย่าง:
text┌──────────────┐
│ Register │
└──────────────┘
┌──────────────┐
│ View Grades │
└──────────────┘
┌──────────────┐
│ Submit Quiz │
└──────────────┘
3. System Boundary
text┌─────────────────────────────────┐
│ Student Portal │
│ ┌─────────────┐ │
│ │ View Grades │ │
│ └─────────────┘ │
│ │
│ ┌─────────────┐ │
│ │ Submit Quiz │ │
│ └─────────────┘ │
└─────────────────────────────────┘
↑ System boundary = rectangle
Use Case Relationships
1. Association (เชื่อมโยง)
Actor สามารถดำเนิน Use Case นี้
text┌─────────┐ ┌──────────────┐
│ Student │───────────│ View Grades │
└─────────┘ └──────────────┘
2. Include (รวม)
Use Case นี้ต้องเรียก Use Case อื่น
text ┌──────────────┐
│ Submit Quiz │
└──────────────┘
│
│ <<include>>
↓
┌──────────────┐
│ Save Answer │ ← ต้องทำด้วย
└──────────────┘
Code คล้ายๆ:
javapublic void submitQuiz() {
// ...
saveAnswer(); // include นี้
// ...
}
3. Extend (ขยาย)
Use Case นี้อาจทำ Use Case อื่นตามเงื่อนไข
text ┌──────────────┐
│ Submit Quiz │
└──────────────┘
△
│ <<extend>>
│ (if timeout)
│
┌──────────────┐
│ Show Warning │ ← อาจทำ
└──────────────┘
4. Generalization (ทั่วไป → เฉพาะ)
Use Cases ที่มีลักษณะเหมือนกัน
text ┌─────────────┐
│ Authenticate│
└─────────────┘
△
│
┌─────┴─────┐
│ │
┌────────┐ ┌────────┐
│ Login │ │ Sign Up│
└────────┘ └────────┘
ตัวอย่างที่ 1: Online Examination System
text ┌────────────────────────────────────┐
│ Online Examination System │
│ │
┌───────┐ │ ┌──────────────┐ │
│Student│────┼──│ Take Exam │ │
└───────┘ │ └──────────────┘ │
│ │ │
│ │<<include>> │
│ ├──────────────────────┐ │
│ ↓ ↓ │
│ ┌──────────────┐ ┌──────────────┐│
│ │ Submit Answer│ │ Save Progress││
│ └──────────────┘ └──────────────┘│
│ │
│ ┌──────────────┐ │
┌────────┐ │ │ View Results │ │
│ Admin │───┼─│ │ │
└────────┘ │ └──────────────┘ │
│ △ │
│ │<<extend>> │
│ │(if premium) │
│ ↓ │
│ ┌──────────────────┐ │
│ │ View Detailed │ │
│ │ Analysis │ │
│ └──────────────────┘ │
│ │
│ ┌──────────────┐ │
│ │ Manage Exams │ │
│ └──────────────┘ │
│ │
└────────────────────────────────────┘
Actors:
- Student – สอบ และดูผลลัพธ์
- Admin – จัดการข้อสอบ
Use Cases:
- Take Exam (include Save Progress)
- View Results (extend View Detailed Analysis)
- Manage Exams
Activity Diagram: ขั้นตอนการทำงาน
Activity Diagram คืออะไร?
Activity Diagram เป็น flowchart ของระบบ แสดง:
- Activity (การกระทำ)
- Decision (การตัดสินใจ)
- Flow (การไหล)
ใช้เมื่อ: วาดกระบวนการ, workflow
โครงสร้าง Activity Diagram
1. Activity (การกระทำ)
text┌──────────────────┐
│ Activity Name │
└──────────────────┘
ตัวอย่าง:
text┌──────────────────┐
│ Read Question │
└──────────────────┘
┌──────────────────┐
│ Answer Question │
└──────────────────┘
┌──────────────────┐
│ Submit Exam │
└──────────────────┘
2. Transition (ส่วนเชื่อม)
text┌──────────┐ ┌──────────┐
│ Activity │────────>│ Activity │
└──────────┘ └──────────┘
3. Decision (เงื่อนไข)
text ┌──────────┐
│ Activity │
└──────────┘
│
↓
◇<decision>
╱ ╲
yes no
╱ ╲
↓ ↓
┌──┐ ┌──┐
│..│ │..│
└──┘ └──┘
ตัวอย่าง:
text ┌─────────────┐
│ Check Score │
└─────────────┘
│
↓
◇ Score > 50
╱ ╲
yes no
╱ ╲
↓ ↓
┌─────┐ ┌──────┐
│PASS │ │ FAIL │
└─────┘ └──────┘
4. Fork/Join (ทำพร้อม)
text ┌─────────────┐
│ Start Exam │
└─────────────┘
│
↓
───────── ← Fork: แบ่งเป็น 2 ทาง
╱ ╲
↓ ↓
┌──────────┐ ┌──────────┐
│ Answer │ │ Check │
│ Question │ │ Time │
└──────────┘ └──────────┘
│ │
└─────┬──────┘
↓
───────── ← Join: รวมเป็น 1 ทาง
│
↓
┌────────────┐
│ Finish Exam│
└────────────┘
ตัวอย่างที่ 2: Online Shopping Process
text ┌────────────────┐
│ Start Shopping │
└────────────────┘
│
↓
┌────────────────┐
│ Browse Products│
└────────────────┘
│
↓
┌────────────────┐
│ Add to Cart │
└────────────────┘
│
↓
◇ Continue?
╱ ╲
yes no
╱ ╲
↓ ↓
┌─────┐ ┌──────────────┐
│Browse │ Proceed to │
│More │ │ Checkout │
└─────┘ └──────────────┘
│ │
└─────┬──────┘
↓
┌────────────────┐
│ Review Order │
└────────────────┘
│
↓
◇ Confirm?
╱ ╲
yes no
╱ ╲
↓ ↓
┌──────────┐ ┌──────────┐
│ Process │ │ Edit │
│ Payment │ │ Order │
└──────────┘ └──────────┘
│ │
└─────┬──────┘
↓
┌────────────────────┐
│ Send Confirmation │
└────────────────────┘
│
↓
┌────────────────┐
│ End │
└────────────────┘
Sequence Diagram: ลำดับการสื่อสารตามเวลา
Sequence Diagram คืออะไร?
Sequence Diagram แสดง:
- Objects/Actors (แนวตั้ง)
- Messages/Interactions (แนวนอน)
- ลำดับเหตุการณ์ (จากบนลงล่าง)
ใช้เมื่อ: วิเคราะห์ interaction ระหว่าง objects
โครงสร้าง Sequence Diagram
1. Actor/Object (แนวตั้ง)
text┌──────────────┐
│ Student │ ← Actor/Object
└──────────────┘
│
│ (lifeline)
│
2. Message (แนวนอน)
text┌──────────────┐ ┌──────────────┐
│ Student │ │ System │
└──────────────┘ └──────────────┘
│ │
│ submit() │
│──────────────────────────────>│
│ │
│ confirm() │
│<──────────────────────────────│
│ │
3. Activation Box (กล่องการทำงาน)
text┌──────────────┐
│ Student │
└──────────────┘
│
┃────────── ← ทำงาน
│ submit()
│──────────>┌──────────────┐
┃──────────
│
├──────────
┃
│ confirm()
│<──────────┃
┃──────────
│
ตัวอย่างที่ 3: Login Process
text┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Student │ │ LoginUI │ │ Database │
└──────────────┘ └──────────────┘ └──────────────┘
│ │ │
│ enter(email, pwd) │ │
│────────────────────────>│ │
│ │ │
│ │ checkCredentials(...) │
│ │────────────────────────>│
│ │ │
│ │ userFound = true │
│ │<────────────────────────│
│ │ │
│ loginSuccess() │ │
│<────────────────────────│ │
│ │ │
ลำดับ:
- Student ส่ง email และ password ไป LoginUI
- LoginUI ตรวจสอบ database
- Database ส่งผลลัพธ์กลับ
- LoginUI ส่ง confirmation ไป Student
ตัวอย่างที่ 4: ATM Withdrawal
text┌────────────┐ ┌───────────┐ ┌──────────────┐
│ Customer │ │ ATM │ │ Bank │
└────────────┘ └───────────┘ └──────────────┘
│ │ │
│ insertCard() │ │
│────────────────────>│ │
│ │ │
│ enterPIN() │ │
│────────────────────>│ │
│ │ │
│ │ verifyPIN() │
│ │─────────────────────>│
│ │ │
│ │ pinValid = true │
│ │<─────────────────────│
│ │ │
│ selectWithdraw() │ │
│────────────────────>│ │
│ │ │
│ enterAmount(5000) │ │
│────────────────────>│ │
│ │ │
│ │ checkBalance(5000) │
│ │─────────────────────>│
│ │ │
│ │ balanceOK = true │
│ │<─────────────────────│
│ │ │
│ │ deductAmount(5000) │
│ │─────────────────────>│
│ │ │
│ │ success = true │
│ │<─────────────────────│
│ │ │
│ dispenseCash() │ │
│<────────────────────│ │
│ │ │
│ ejectCard() │ │
│<────────────────────│ │
│ │ │
State Machine Diagram: สถานะของ Object
State Machine Diagram คืออะไร?
State Machine Diagram แสดง:
- States (สถานะ)
- Transitions (การเปลี่ยนแปลง)
- Events (เหตุการณ์ที่ทำให้เปลี่ยน)
ใช้เมื่อ: ออกแบบ lifecycle ของ object
โครงสร้าง State Machine Diagram
1. State (สถานะ)
text┌─────────────┐
│ State │
└─────────────┘
ตัวอย่าง:
text┌───────┐ ┌────────┐ ┌──────┐
│ Login │ │ Logged │ │ Logout
│ │ │ In │ │
└───────┘ └────────┘ └──────┘
2. Initial/Final State
text● ← Initial state (filled circle)
○ ← Final state (hollow circle)
3. Transition
text┌───────────────┐
│ State 1 │
└───────────────┘
│
│ event [condition] / action
↓
┌───────────────┐
│ State 2 │
└───────────────┘
ตัวอย่าง:
text┌───────┐
│ Idle │
└───────┘
│
│ power_on
↓
┌──────────┐
│ Running │
└──────────┘
│
│ power_off
↓
┌───────┐
│ Off │
└───────┘
ตัวอย่างที่ 5: Door State Machine
text ┌─────────────┐
│ Initial │
└─────────────┘
│
│ create()
↓
┌──────────────┐
│ Closed │ ← locked, no movement
└──────────────┘
↑ │ ↑
│ │ │
close │ │ │ open
(if │ │ │ unlock
open) │ │ │ [key]
│ │ │
│ ↓ │
┌──────────────┐
│ Open │ ← accessible
└──────────────┘
↑ │
close │ │ open()/unlock
│ ↓
┌──────────────┐
│ Locked │ ← locked, closed
└──────────────┘
ตัวอย่างที่ 6: Order Processing State Machine
text ┌─────────┐
│ Initial │
└─────────┘
│
│ order_created
↓
┌─────────────────┐
│ Pending │
└─────────────────┘
│
│ payment_received / send_confirmation
↓
┌─────────────────┐
│ Processing │
└─────────────────┘
│
│ pack_item / prepare_shipment
↓
┌─────────────────┐
│ Packing │
└─────────────────┘
│
│ dispatch / notify_customer
↓
┌─────────────────┐
│ Shipped │
└─────────────────┘
│
│ delivery_confirmed
↓
┌─────────────────┐
│ Delivered │
└─────────────────┘
│
│ delete_order
↓
┌─────────┐
│ Final │
└─────────┘
Exception paths:
─────────────────
Pending ──→ payment_failed ──→ Cancelled
Processing ──→ out_of_stock ──→ Cancelled
Shipped ──→ delivery_failed ──→ Returned
ตัวอย่างสมบูรณ์: E-Commerce System (ทั้งหมด)
1. Use Case Diagram
text ┌──────────────────────────────────────┐
│ E-Commerce System │
│ │
┌────────┐ │ ┌──────────────┐ │
│Customer│──┼──│ Browse Items │ │
└────────┘ │ └──────────────┘ │
│ │ │
│ │<<include>> │
│ ↓ │
│ ┌──────────────┐ │
│ │ Search Items │ │
│ └──────────────┘ │
│ │
│ ┌──────────────┐ │
├──│ Place Order │ │
│ └──────────────┘ │
│ │ │
│ │<<include>> │
│ ↓ │
│ ┌──────────────┐ │
│ │ Process │ │
│ │ Payment │ │
│ └──────────────┘ │
│ │ │
│ │<<extend>> │
│ │(if discount) │
│ ↓ │
│ ┌──────────────┐ │
│ │ Apply Coupon │ │
│ └──────────────┘ │
│ │
┌────────┐ │ ┌──────────────┐ │
│ Admin │──┼──│ Manage Items │ │
└────────┘ │ └──────────────┘ │
│ │
│ ┌──────────────┐ │
├──│ Manage Orders│ │
│ └──────────────┘ │
│ │
└──────────────────────────────────────┘
2. Activity Diagram: Place Order Process
text ┌────────────┐
│ Start │
└────────────┘
│
↓
┌──────────────┐
│ Browse Items │
└──────────────┘
│
↓
◇ Items?
╱ ╲
yes no
╱ ╲
↓ ↓
┌───────────┐ ┌──────┐
│ Add Cart │ │ Exit │
└───────────┘ └──────┘
│
└──────┬──────┘
↓
┌─────────────┐
│ Proceed to │
│ Checkout │
└─────────────┘
│
─────────────┼─────────────
╱ Continue ╱ ╲ Continue ╲
│ Shopping │ │ Checkout │
│ │ │ │
↓ ↓ ↓ ↓
┌─────┐ ┌──────────────┐
│....│ │Review Order │
└─────┘ └──────────────┘
│ │
└─────┬───────┘
↓
◇ Confirm
╱ ╲
yes no
╱ ╲
↓ ↓
┌──────┐ ┌──────┐
│Pay │ │Edit │
│ │ │Order │
└──────┘ └──────┘
│ │
└──┬─────┘
↓
┌─────────────┐
│ Send │
│Confirmation │
└─────────────┘
│
↓
┌──────┐
│ End │
└──────┘
3. Sequence Diagram: Order Checkout
text┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│ Customer │ │OrderUI │ │ Order │ │ Payment │
└──────────┘ └──────────┘ └──────────┘ └──────────┘
│ │ │ │
│ checkout() │ │ │
│─────────────>│ │ │
│ │ │ │
│ │ createOrder() │ │
│ │──────────────>│ │
│ │ │ │
│ │<──────────────│ │
│ │ orderID │ │
│ │ │ │
│ payment() │ │ │
│─────────────>│ │ │
│ │ │ process(...) │
│ │──────────────────────────────>│
│ │ │ │
│ │ │ transactionID │
│ │<──────────────────────────────│
│ │ │ │
│ │ updateOrder() │ │
│ │──────────────>│ │
│ │ │ │
│ │<──────────────│ │
│ │ success │ │
│ │ │ │
│<─ confirmation ─────────────│ │
│ │ │ │
4. State Machine Diagram: Order Lifecycle
text ┌─────────┐
│ Initial │
└─────────┘
│
│ create()
↓
┌─────────────┐
│ Pending │ [Customer places order]
└─────────────┘
│
│ payment_received
↓
┌──────────────┐
│ Processing │ [Payment confirmed]
└──────────────┘
│
│ items_packed
↓
┌──────────────┐
│ Packing │ [Staff packing items]
└──────────────┘
│
│ dispatch
↓
┌──────────────┐
│ Shipped │ [In transit]
└──────────────┘
│
│ delivered
↓
┌──────────────┐
│ Delivered │ [Received by customer]
└──────────────┘
│
│ complete
↓
┌──────────────┐
│ Completed │
└──────────────┘
│
│ archive
↓
┌─────────┐
│ Final │
└─────────┘
Error Handling:
────────────────
Pending ──→ payment_failed ──→ Cancelled
Processing ──→ out_of_stock ──→ Cancelled
Shipped ──→ delivery_failed ──→ Return
5. Class Diagram: Data Model
text┌───────────────────────┐
│ Customer │
├───────────────────────┤
│ - customerID: String │
│ - name: String │
│ - email: String │
│ - address: String │
├───────────────────────┤
│ + login(): boolean │
│ + logout(): void │
└───────────────────────┘
│ 1
│ places
│ *
↓
┌───────────────────────┐
│ Order │
├───────────────────────┤
│ - orderID: String │
│ - date: Date │
│ - total: double │
│ - status: String │
├───────────────────────┤
│ + checkout(): boolean │
│ + cancel(): void │
└───────────────────────┘
│ 1
│ contains
│ *
↓
┌───────────────────────┐
│ OrderItem │
├───────────────────────┤
│ - itemID: String │
│ - quantity: int │
│ - price: double │
├───────────────────────┤
│ + getTotal(): double │
└───────────────────────┘
│ *
│ references
│ 1
↓
┌───────────────────────┐
│ Product │
├───────────────────────┤
│ - productID: String │
│ - name: String │
│ - description: String │
│ - price: double │
│ - stock: int │
├───────────────────────┤
│ + getInfo(): String │
│ + updateStock(): void │
└───────────────────────┘
สรุปตอนที่ 2
เราได้เรียนรู้:
✅ Use Case Diagram – ฟังก์ชันและ actors
✅ Activity Diagram – ขั้นตอนการทำงาน (flowchart)
✅ Sequence Diagram – ลำดับการสื่อสารตามเวลา
✅ State Machine Diagram – สถานะของ object
✅ ตัวอย่างสมบูรณ์ – E-Commerce System
🎯 ในตอนต่อไป เราจะเรียนรู้:
- Communication Diagram (Interaction diagram)
- Component Diagram
- Deployment Diagram
- Package Diagram
- Advanced patterns ใน Class Diagram
- Design Patterns ที่นิยม
