UML Diagram (ตอนที่ 2)

บทนำตอนนี้: Behavior Diagrams

ถ้า Class Diagram แสดง โครงสร้าง แล้ว Behavior Diagrams แสดง การทำงาน ของระบบ

ตอนนี้เราจะศึกษา:

  1. Use Case Diagram – ฟังก์ชันของระบบ (requirements)
  2. Activity Diagram – ขั้นตอนการทำงาน (workflow)
  3. Sequence Diagram – ลำดับการสื่อสารตามเวลา
  4. State Machine Diagram – สถานะของ object
ยาวไปอยากเลือกอ่าน
  1. บทนำตอนนี้: Behavior Diagrams
  2. Use Case Diagram: ฟังก์ชันของระบบ
  3. Use Case Diagram คืออะไร?
  4. โครงสร้าง Use Case Diagram
  5. 1. Actor (ผู้ใช้/ระบบภายนอก)
  6. 2. Use Case (ฟังก์ชัน)
  7. 3. System Boundary
  8. Use Case Relationships
  9. 1. Association (เชื่อมโยง)
  10. 2. Include (รวม)
  11. 3. Extend (ขยาย)
  12. 4. Generalization (ทั่วไป → เฉพาะ)
  13. ตัวอย่างที่ 1: Online Examination System
  14. Activity Diagram: ขั้นตอนการทำงาน
  15. Activity Diagram คืออะไร?
  16. โครงสร้าง Activity Diagram
  17. 1. Activity (การกระทำ)
  18. 2. Transition (ส่วนเชื่อม)
  19. 3. Decision (เงื่อนไข)
  20. 4. Fork/Join (ทำพร้อม)
  21. ตัวอย่างที่ 2: Online Shopping Process
  22. Sequence Diagram: ลำดับการสื่อสารตามเวลา
  23. Sequence Diagram คืออะไร?
  24. โครงสร้าง Sequence Diagram
  25. 1. Actor/Object (แนวตั้ง)
  26. 2. Message (แนวนอน)
  27. 3. Activation Box (กล่องการทำงาน)
  28. ตัวอย่างที่ 3: Login Process
  29. ตัวอย่างที่ 4: ATM Withdrawal
  30. State Machine Diagram: สถานะของ Object
  31. State Machine Diagram คืออะไร?
  32. โครงสร้าง State Machine Diagram
  33. 1. State (สถานะ)
  34. 2. Initial/Final State
  35. 3. Transition
  36. ตัวอย่างที่ 5: Door State Machine
  37. ตัวอย่างที่ 6: Order Processing State Machine
  38. ตัวอย่างสมบูรณ์: E-Commerce System (ทั้งหมด)
  39. 1. Use Case Diagram
  40. 2. Activity Diagram: Place Order Process
  41. 3. Sequence Diagram: Order Checkout
  42. 4. State Machine Diagram: Order Lifecycle
  43. 5. Class Diagram: Data Model
  44. สรุปตอนที่ 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()     │                         │
       │<────────────────────────│                         │
       │                         │                         │

ลำดับ:

  1. Student ส่ง email และ password ไป LoginUI
  2. LoginUI ตรวจสอบ database
  3. Database ส่งผลลัพธ์กลับ
  4. 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 ที่นิยม