Use Case Diagram, Class Diagram, Sequence Diagram

บทนำ: ทำไมต้องวาดภาพก่อนเขียนโค้ด?

ก่อนสร้างบ้าน สถาปนิกต้องวาด แบบบ้าน ก่อน เพื่อให้:

  • ทุกคนเข้าใจเหมือนกัน (ลูกค้า, ช่างก่อสร้าง, วิศวกร)
  • เห็นภาพรวม ก่อนลงมือทำ
  • หาข้อผิดพลาด ได้ตั้งแต่ออกแบบ (ก่อนสร้างจริง)

UML (Unified Modeling Language) = ภาษามาตรฐานสำหรับ “วาดแบบซอฟต์แวร์”

text3 UML Diagrams หลัก:
├─ Use Case Diagram  → แสดง "ใครใช้ระบบทำอะไร"
├─ Class Diagram     → แสดง "โครงสร้างของ classes"
└─ Sequence Diagram  → แสดง "ลำดับการทำงานของ objects"

1. Use Case Diagram: ใครใช้ระบบทำอะไร?

ความหมาย

Use Case Diagram = แผนภาพที่แสดง actors (ผู้ใช้) และ use cases (การใช้งาน) ของระบบ

องค์ประกอบ:

  • Actor = ผู้ใช้ระบบ (คน หรือ ระบบอื่น)
  • Use Case = งานที่ actor ทำได้
  • System Boundary = กรอบระบบ
  • Relationship = ความสัมพันธ์

สัญลักษณ์

textActor:           Use Case:        System Boundary:
  🧑                ⭕              ┌──────────┐
 /|\             ( Login )         │ System   │
 / \                               └──────────┘

Association:     Include:         Extend:
─────────        ···><<include>>  ···><<extend>>

ตัวอย่างที่ 1: ระบบ E-Commerce

textActors:
- Customer (ลูกค้า)
- Admin (ผู้ดูแลระบบ)
- Payment Gateway (ระบบชำระเงินภายนอก)

Use Cases:
- Browse Products (เรียกดูสินค้า)
- Add to Cart (เพิ่มลงตะกร้า)
- Checkout (ชำระเงิน)
- Manage Inventory (จัดการสต็อก)
- View Reports (ดูรายงาน)

Use Case Diagram (Text representation):

text┌───────────────────────────────────────┐
│       E-Commerce System               │
│                                       │
│  Customer                             │
│    |                                  │
│    |──── (Browse Products)            │
│    |                                  │
│    |──── (Add to Cart)                │
│    |         |                        │
│    |         |──include──>(Login)     │
│    |                                  │
│    |──── (Checkout)                   │
│             |                         │
│             |──include──>(Process     │
│                           Payment)────┤
│                                       │
│  Admin                                │
│    |                                  │
│    |──── (Manage Inventory)           │
│    |         |                        │
│    |         |──include──>(Login)     │
│    |                                  │
│    |──── (View Reports)               │
│                                       │
└───────────────────────────────────────┘
                                    |
                            Payment Gateway

ตัวอย่าง Use Case Description (รายละเอียด)

Use Case: Checkout

FieldDescription
Use Case IDUC-003
Use Case NameCheckout
ActorCustomer
PreconditionCustomer must be logged in and have items in cart
Main Flow1. Customer clicks “Checkout”
2. System displays order summary
3. Customer enters shipping address
4. Customer selects payment method
5. System processes payment
6. System confirms order
Alternative Flow3a. Address invalid → show error
5a. Payment failed → show error, retry
PostconditionOrder created, inventory updated

ตัวอย่างที่ 2: ระบบห้องสมุด (Library System)

text┌───────────────────────────────────────┐
│       Library System                  │
│                                       │
│  Member                               │
│    |                                  │
│    |──── (Search Books)               │
│    |                                  │
│    |──── (Borrow Book)                │
│    |         |                        │
│    |         |──include──>(Check      │
│    |                      Availability)│
│    |                                  │
│    |──── (Return Book)                │
│    |         |                        │
│    |         |──extend──>(Pay Fine)   │
│    |                                  │
│  Librarian                            │
│    |                                  │
│    |──── (Add Book)                   │
│    |                                  │
│    |──── (Remove Book)                │
│    |                                  │
│    |──── (Generate Report)            │
│                                       │
└───────────────────────────────────────┘

คำอธิบาย Relationships:

  • Include (<>): Use case หนึ่ง จำเป็นต้องใช้ อีก use case หนึ่ง
    • “Borrow Book” include “Check Availability” = ต้องเช็คก่อนยืมทุกครั้ง
  • Extend (<>): Use case หนึ่ง อาจจะขยายไป อีก use case (ไม่จำเป็น)
    • “Return Book” extend “Pay Fine” = ถ้าคืนช้า ต้องจ่ายค่าปรับ (บางครั้ง)

2. Class Diagram: โครงสร้างของ Classes

ความหมาย

Class Diagram = แผนภาพที่แสดง classes, attributes, methods, และ relationships

องค์ประกอบ:

text┌─────────────────┐
│   ClassName     │  ← Class name
├─────────────────┤
│ - attribute1    │  ← Attributes (- = private, + = public, # = protected)
│ - attribute2    │
├─────────────────┤
│ + method1()     │  ← Methods
│ + method2()     │
└─────────────────┘

Relationships

textAssociation (เชื่อมโยง):
ClassA ────────> ClassB

Aggregation (มีส่วนหนึ่ง - ส่วนย่อยอยู่ได้โดยอิสระ):
ClassA ◇────────> ClassB

Composition (เป็นส่วนหนึ่ง - ส่วนย่อยไม่มี ClassA ไม่ได้):
ClassA ◆────────> ClassB

Inheritance (สืบทอด):
SubClass ──────▷ SuperClass

Interface Implementation:
Class ┄┄┄┄┄▷ Interface

ตัวอย่างที่ 3: E-Commerce System (Advanced)

text┌─────────────────────┐
│      User           │
├─────────────────────┤
│ - userId: int       │
│ - username: String  │
│ - email: String     │
│ - password: String  │
├─────────────────────┤
│ + login()           │
│ + logout()          │
│ + updateProfile()   │
└─────────────────────┘
          △
          │ (inheritance)
    ┌─────┴─────┐
    │           │
┌───────┐   ┌────────┐
│Customer│   │ Admin  │
├───────┤   ├────────┤
│- address│  │- role  │
├───────┤   ├────────┤
│+ placeOrder()│ + manageInventory()
└───────┘   │+ viewReports()    │
    │       └────────┘
    │ (association)
    │ 1..*
    ▼
┌─────────────────────┐
│      Order          │
├─────────────────────┤
│ - orderId: int      │
│ - orderDate: Date   │
│ - status: String    │
│ - totalAmount: double│
├─────────────────────┤
│ + calculateTotal()  │
│ + updateStatus()    │
└─────────────────────┘
    ◆ (composition)
    │ 1
    │
    │ 1..*
    ▼
┌─────────────────────┐
│   OrderItem         │
├─────────────────────┤
│ - quantity: int     │
│ - price: double     │
├─────────────────────┤
│ + getSubtotal()     │
└─────────────────────┘
    │ (association)
    │ *
    ▼
┌─────────────────────┐
│     Product         │
├─────────────────────┤
│ - productId: int    │
│ - name: String      │
│ - price: double     │
│ - stock: int        │
├─────────────────────┤
│ + updateStock()     │
│ + isAvailable()     │
└─────────────────────┘
    △ (inheritance)
    │
    ├──────────┬──────────┐
    │          │          │
┌─────────┐ ┌─────────┐ ┌─────────┐
│Physical │ │Digital  │ │Service  │
│Product  │ │Product  │ │Product  │
├─────────┤ ├─────────┤ ├─────────┤
│-weight  │ │-fileSize│ │-duration│
│-dimension││-format  │ │-provider│
└─────────┘ └─────────┘ └─────────┘

อธิบาย Multiplicity (จำนวน):

  • 1 = ต้องมีหนึ่งเดียว
  • 0..1 = 0 หรือ 1
  • 1..* = 1 หรือมากกว่า
  • * = 0 หรือมากกว่า

ตัวอย่าง:

  • Customer 1 ────── 1..* Order = ลูกค้า 1 คน มีได้หลาย orders
  • Order 1 ◆────── 1..* OrderItem = Order 1 ใบ มีได้หลาย items (composition)

ตัวอย่างที่ 4: Banking System

text┌─────────────────────┐
│      Account        │ (abstract)
├─────────────────────┤
│ - accountNumber     │
│ - balance: double   │
│ - owner: Customer   │
├─────────────────────┤
│ + deposit(amount)   │
│ + withdraw(amount)  │
│ + getBalance()      │
│ {abstract} calculateInterest()│
└─────────────────────┘
          △
          │
    ┌─────┴─────┐
    │           │
┌────────────┐ ┌────────────┐
│SavingsAccount│ │CheckingAccount│
├────────────┤ ├────────────┤
│-interestRate│ │-overdraftLimit│
├────────────┤ ├────────────┤
│+calculateInterest()│ │+calculateInterest()│
│+addInterest()│ │+allowOverdraft()│
└────────────┘ └────────────┘

┌─────────────────────┐
│     Customer        │
├─────────────────────┤
│ - customerId: int   │
│ - name: String      │
│ - phone: String     │
├─────────────────────┤
│ + openAccount()     │
│ + closeAccount()    │
└─────────────────────┘
    │ (association)
    │ 1
    │
    │ 1..*
    ▼
┌─────────────────────┐
│   Transaction       │
├─────────────────────┤
│ - transactionId     │
│ - date: Date        │
│ - amount: double    │
│ - type: String      │
├─────────────────────┤
│ + execute()         │
│ + rollback()        │
└─────────────────────┘

3. Sequence Diagram: ลำดับการทำงาน

ความหมาย

Sequence Diagram = แผนภาพที่แสดง ลำดับการส่ง messages ระหว่าง objects ตามเวลา (timeline)

องค์ประกอบ:

textActor/Object:    Lifeline:       Message:        Activation:
    □               │            ────────>          ▌
   User             │                               ▌
                    │                               ▌

ตัวอย่างที่ 5: Login Process

text┌──────┐         ┌────────┐      ┌──────────┐     ┌──────────┐
│ User │         │  UI    │      │Controller│     │ Database │
└──┬───┘         └───┬────┘      └────┬─────┘     └────┬─────┘
   │                 │                │                │
   │ 1: Enter        │                │                │
   │ credentials     │                │                │
   ├────────────────>│                │                │
   │                 │                │                │
   │                 │ 2: login(      │                │
   │                 │    username,   │                │
   │                 │    password)   │                │
   │                 ├───────────────>│                │
   │                 │                │                │
   │                 │                │ 3: query       │
   │                 │                │    user        │
   │                 │                ├───────────────>│
   │                 │                │                │
   │                 │                │ 4: return      │
   │                 │                │    user data   │
   │                 │                │<───────────────┤
   │                 │                │                │
   │                 │                │ 5: validate    │
   │                 │                │    password    │
   │                 │                │───┐            │
   │                 │                │   │ (self-call)│
   │                 │                │<──┘            │
   │                 │                │                │
   │                 │ 6: return      │                │
   │                 │    success     │                │
   │                 │<───────────────┤                │
   │                 │                │                │
   │ 7: Show         │                │                │
   │    Dashboard    │                │                │
   │<────────────────┤                │                │
   │                 │                │                │

อ่านว่า:

  1. User ป้อน credentials ให้ UI
  2. UI ส่ง login() ไปยัง Controller
  3. Controller query user จาก Database
  4. Database return user data
  5. Controller validate password (self-call)
  6. Controller return success ให้ UI
  7. UI แสดง Dashboard ให้ User

ตัวอย่างที่ 6: Order Processing (Advanced)

text┌────────┐  ┌────────┐  ┌──────────┐  ┌───────────┐  ┌──────────┐
│Customer│  │  Cart  │  │OrderService│ │PaymentService│ │Inventory │
└───┬────┘  └───┬────┘  └─────┬────┘  └─────┬─────┘  └────┬─────┘
    │           │             │              │             │
    │ 1: Add    │             │              │             │
    │    item   │             │              │             │
    ├──────────>│             │              │             │
    │           │             │              │             │
    │           │ 2: check    │              │             │
    │           │    stock    │              │             │
    │           ├────────────────────────────────────────>│
    │           │             │              │             │
    │           │ 3: return   │              │             │
    │           │    available│              │             │
    │           │<────────────────────────────────────────┤
    │           │             │              │             │
    │ 4: Display│             │              │             │
    │    updated│             │              │             │
    │<──────────┤             │              │             │
    │           │             │              │             │
    │ 5: Checkout│            │              │             │
    ├──────────>│             │              │             │
    │           │             │              │             │
    │           │ 6: create   │              │             │
    │           │    order    │              │             │
    │           ├────────────>│              │             │
    │           │             │              │             │
    │           │             │ 7: process   │             │
    │           │             │    payment   │             │
    │           │             ├─────────────>│             │
    │           │             │              │             │
    │           │             │              │ 8: authorize│
    │           │             │              │───┐         │
    │           │             │              │   │         │
    │           │             │              │<──┘         │
    │           │             │              │             │
    │           │             │ 9: payment   │             │
    │           │             │    success   │             │
    │           │             │<─────────────┤             │
    │           │             │              │             │
    │           │             │ 10: update   │             │
    │           │             │     stock    │             │
    │           │             ├────────────────────────────>│
    │           │             │              │             │
    │           │             │ 11: stock    │             │
    │           │             │     updated  │             │
    │           │             │<────────────────────────────┤
    │           │             │              │             │
    │           │ 12: order   │              │             │
    │           │     confirmed│             │             │
    │           │<────────────┤              │             │
    │           │             │              │             │
    │ 13: Show  │             │              │             │
    │     confirmation        │              │             │
    │<──────────┤             │              │             │
    │           │             │              │             │

Alternative Flow (Payment Failed):

text                 [at step 9]
    │           │             │              │             │
    │           │             │ 9: payment   │             │
    │           │             │    failed    │             │
    │           │             │<─────────────┤             │
    │           │             │              │             │
    │           │ 10: cancel  │              │             │
    │           │     order   │              │             │
    │           │<────────────┤              │             │
    │           │             │              │             │
    │ 11: Show  │             │              │             │
    │     error │             │              │             │
    │<──────────┤             │              │             │

ตัวอย่างที่ 7: Library Book Borrowing

text┌──────┐  ┌─────────┐  ┌─────────┐  ┌──────────┐  ┌────────┐
│Member│  │Librarian│  │  System │  │BookCatalog│ │Database│
└──┬───┘  └────┬────┘  └────┬────┘  └─────┬────┘  └───┬────┘
   │           │            │             │           │
   │ 1: Request│            │             │           │
   │    book   │            │             │           │
   ├──────────>│            │             │           │
   │           │            │             │           │
   │           │ 2: search  │             │           │
   │           │    book    │             │           │
   │           ├───────────>│             │           │
   │           │            │             │           │
   │           │            │ 3: query    │           │
   │           │            │    book     │           │
   │           │            ├────────────>│           │
   │           │            │             │           │
   │           │            │             │ 4: get    │
   │           │            │             │    data   │
   │           │            │             ├──────────>│
   │           │            │             │           │
   │           │            │             │ 5: book   │
   │           │            │             │    info   │
   │           │            │             │<──────────┤
   │           │            │             │           │
   │           │            │ 6: book     │           │
   │           │            │    available│           │
   │           │            │<────────────┤           │
   │           │            │             │           │
   │           │ 7: book    │             │           │
   │           │    found   │             │           │
   │           │<───────────┤             │           │
   │           │            │             │           │
   │           │ 8: check   │             │           │
   │           │    member  │             │           │
   │           │    status  │             │           │
   │           ├───────────>│             │           │
   │           │            │             │           │
   │           │            │ 9: validate │           │
   │           │            ├──────────────────────────>│
   │           │            │             │           │
   │           │            │ 10: member  │           │
   │           │            │     OK      │           │
   │           │            │<──────────────────────────┤
   │           │            │             │           │
   │           │            │ 11: create  │           │
   │           │            │     loan    │           │
   │           │            ├──────────────────────────>│
   │           │            │             │           │
   │           │            │ 12: loan    │           │
   │           │            │     created │           │
   │           │            │<──────────────────────────┤
   │           │            │             │           │
   │           │ 13: loan   │             │           │
   │           │     success│             │           │
   │           │<───────────┤             │           │
   │           │            │             │           │
   │ 14: Give  │            │             │           │
   │     book  │            │             │           │
   │<──────────┤            │             │           │
   │           │            │             │           │

การใช้งานร่วมกัน: 3 Diagrams ทำงานเป็นทีม

ตัวอย่าง: Hotel Reservation System

1. Use Case Diagram (ระดับภาพรวม)

text┌───────────────────────────────────────┐
│   Hotel Reservation System            │
│                                       │
│  Guest                                │
│    |                                  │
│    |──── (Search Rooms)               │
│    |                                  │
│    |──── (Make Reservation)           │
│    |         |                        │
│    |         |──include──>(Check      │
│    |                      Availability)│
│    |         |                        │
│    |         |──include──>(Process    │
│    |                      Payment)    │
│    |                                  │
│    |──── (Cancel Reservation)         │
│    |         |                        │
│    |         |──extend──>(Get Refund) │
│                                       │
│  Hotel Staff                          │
│    |                                  │
│    |──── (Check In Guest)             │
│    |                                  │
│    |──── (Check Out Guest)            │
│    |                                  │
│    |──── (Manage Rooms)               │
│                                       │
└───────────────────────────────────────┘

2. Class Diagram (โครงสร้างระบบ)

text┌─────────────────────┐
│      Guest          │
├─────────────────────┤
│ - guestId: int      │
│ - name: String      │
│ - email: String     │
│ - phone: String     │
├─────────────────────┤
│ + searchRooms()     │
│ + makeReservation() │
└─────────────────────┘
    │ (makes)
    │ 1
    │
    │ 0..*
    ▼
┌─────────────────────┐
│   Reservation       │
├─────────────────────┤
│ - reservationId     │
│ - checkInDate: Date │
│ - checkOutDate: Date│
│ - status: String    │
│ - totalAmount       │
├─────────────────────┤
│ + calculateTotal()  │
│ + confirm()         │
│ + cancel()          │
└─────────────────────┘
    │ (for)
    │ 1
    │
    │ 1..*
    ▼
┌─────────────────────┐
│       Room          │
├─────────────────────┤
│ - roomNumber: String│
│ - type: String      │
│ - pricePerNight     │
│ - status: String    │
├─────────────────────┤
│ + isAvailable()     │
│ + book()            │
│ + release()         │
└─────────────────────┘
    △ (inheritance)
    │
    ├──────────┬──────────┐
    │          │          │
┌─────────┐ ┌─────────┐ ┌─────────┐
│Standard │ │Deluxe   │ │Suite    │
│Room     │ │Room     │ │Room     │
├─────────┤ ├─────────┤ ├─────────┤
│+features│ │+features│ │+features│
└─────────┘ └─────────┘ └─────────┘

┌─────────────────────┐
│     Payment         │
├─────────────────────┤
│ - paymentId: int    │
│ - amount: double    │
│ - method: String    │
│ - status: String    │
├─────────────────────┤
│ + process()         │
│ + refund()          │
└─────────────────────┘

3. Sequence Diagram (การทำงาน: Make Reservation)

text┌─────┐  ┌────────┐  ┌───────────┐  ┌──────┐  ┌─────────┐
│Guest│  │   UI   │  │ReservationService│Room│ │PaymentService│
└──┬──┘  └───┬────┘  └─────┬─────┘  └──┬───┘  └────┬────┘
   │         │             │           │           │
   │ 1: Select│            │           │           │
   │  dates  │             │           │           │
   ├────────>│             │           │           │
   │         │             │           │           │
   │         │ 2: search   │           │           │
   │         │  available  │           │           │
   │         │  rooms      │           │           │
   │         ├────────────>│           │           │
   │         │             │           │           │
   │         │             │ 3: check  │           │
   │         │             │  status   │           │
   │         │             ├──────────>│           │
   │         │             │           │           │
   │         │             │ 4: return │           │
   │         │             │  available│           │
   │         │             │  rooms    │           │
   │         │             │<──────────┤           │
   │         │             │           │           │
   │         │ 5: display  │           │           │
   │         │  rooms      │           │           │
   │         │<────────────┤           │           │
   │         │             │           │           │
   │ 6: Select│            │           │           │
   │  room   │             │           │           │
   ├────────>│             │           │           │
   │         │             │           │           │
   │         │ 7: create   │           │           │
   │         │  reservation│           │           │
   │         ├────────────>│           │           │
   │         │             │           │           │
   │         │             │ 8: reserve│           │
   │         │             │  room     │           │
   │         │             ├──────────>│           │
   │         │             │           │           │
   │         │             │ 9: room   │           │
   │         │             │  reserved │           │
   │         │             │<──────────┤           │
   │         │             │           │           │
   │         │             │ 10: process           │
   │         │             │   payment │           │
   │         │             ├──────────────────────>│
   │         │             │           │           │
   │         │             │ 11: payment           │
   │         │             │   success │           │
   │         │             │<──────────────────────┤
   │         │             │           │           │
   │         │ 12: confirmation        │           │
   │         │<────────────┤           │           │
   │         │             │           │           │
   │ 13: Show│             │           │           │
   │  confirmation         │           │           │
   │<────────┤             │           │           │

Best Practices: UML Diagrams

Diagramใช้เมื่อไรควรระบุอะไร
Use Caseต้องการเข้าใจ “ใครใช้ระบบอย่างไร”Actors, Use Cases, Relationships
Classต้องการออกแบบโครงสร้าง codeClasses, Attributes, Methods, Relationships
Sequenceต้องการเข้าใจ “ลำดับการทำงาน”Objects, Messages, Timing

สรุป

UML Diagrams เป็นเครื่องมือสำคัญในการออกแบบซอฟต์แวร์ที่ทำให้ทีมงานเข้าใจตรงกัน:

Use Case Diagram ตอบคำถาม:

  • ใครคือผู้ใช้ระบบ?
  • พวกเขาต้องการทำอะไร?
  • ความสัมพันธ์ระหว่าง use cases เป็นอย่างไร?

Class Diagram ตอบคำถาม:

  • ระบบมี classes อะไรบ้าง?
  • แต่ละ class มี attributes/methods อะไร?
  • Classes สัมพันธ์กันอย่างไร?

Sequence Diagram ตอบคำถาม:

  • Objects สื่อสารกันอย่างไร?
  • ลำดับการส่ง messages เป็นอย่างไร?
  • Alternative flows มีอะไรบ้าง?

การใช้ UML ร่วมกันทำให้:

  • ทีมเข้าใจตรงกัน – มี visual ที่ชัดเจน
  • ลดข้อผิดพลาด – หาปัญหาตั้งแต่ขั้นออกแบบ
  • Documentation ดี – มีเอกสารอธิบายระบบ
  • Maintenance ง่าย – คนใหม่เข้าใจระบบเร็วขึ้น

UML ไม่ใช่แค่ภาพวาด แต่เป็น “ภาษากลาง” ที่ทุกคนในโลก software engineering เข้าใจ และเป็นพื้นฐานสำคัญในการสร้างซอฟต์แวร์คุณภาพสูง