UML Diagram (ตอนที่ 3)

บทนำตอนนี้: Structural Diagrams ขั้นสูง

ตอนนี้เราจะศึกษา diagrams ที่เน้น architecture ของระบบ:

  1. Communication Diagram – Interaction แบบเน้น structure
  2. Component Diagram – โครงสร้างของ components
  3. Deployment Diagram – วาง hardware/network
  4. Package Diagram – จัดกลุ่ม classes
ยาวไปอยากเลือกอ่าน
  1. บทนำตอนนี้: Structural Diagrams ขั้นสูง
  2. Communication Diagram: Interaction แบบเน้น Structure
  3. Communication Diagram คืออะไร?
  4. โครงสร้าง Communication Diagram
  5. ตัวอย่างที่ 1: Email Notification System
  6. ตัวอย่างที่ 2: Payment Processing
  7. Component Diagram: โครงสร้างของ Components
  8. Component Diagram คืออะไร?
  9. โครงสร้าง Component Diagram
  10. 1. Component (ชิ้นส่วน)
  11. 2. Interface (อินเตอร์เฟส)
  12. 3. Dependency (พึ่งพา)
  13. ตัวอย่างที่ 3: E-Commerce System Architecture
  14. ตัวอย่างที่ 4: หลายสถานการณ์ Component
  15. Deployment Diagram: วาง Hardware/Network
  16. Deployment Diagram คืออะไร?
  17. โครงสร้าง Deployment Diagram
  18. 1. Node (Server/Hardware)
  19. 2. Connection (เชื่อมโยง)
  20. ตัวอย่างที่ 5: Traditional 3-Tier Deployment
  21. ตัวอย่างที่ 6: Microservices Architecture Deployment
  22. ตัวอย่างที่ 7: Cloud Deployment (AWS)
  23. Package Diagram: จัดกลุ่ม Classes
  24. Package Diagram คืออะไร?
  25. โครงสร้าง Package Diagram
  26. 1. Package (กลุ่ม)
  27. 2. Dependency ระหว่าง Packages
  28. ตัวอย่างที่ 8: University System Package Structure
  29. ตัวอย่างที่ 9: Detailed Package Dependencies
  30. Advanced Class Diagram Features
  31. 1. Constraints (ข้อจำกัด)
  32. 2. Tagged Values (ข้อมูลเพิ่มเติม)
  33. 3. Navigability (ทิศทางการใช้)
  34. ตัวอย่างที่ 10: Advanced Class Diagram
  35. Communication Protocol: สัญลักษณ์ในทั่ว UML
  36. Relationships Quick Reference
  37. Complete Real-World Example: Hospital Management System
  38. ข้อมูลระบบ
  39. Use Case Diagram
  40. Class Diagram (Partial)
  41. Component Diagram
  42. Sequence Diagram: Book Appointment
  43. สรุปตอนที่ 3

Communication Diagram: Interaction แบบเน้น Structure

Communication Diagram คืออะไร?

Communication Diagram คล้ายกับ Sequence Diagram แต่:

  • เน้น structure ของ objects มากกว่า timeline
  • แสดง links (เชื่อมโยง) ระหว่าง objects
  • เหมาะสำหรับ static structure ของ interaction

ใช้เมื่อ: วิเคราะห์ interaction ที่เน้น relationships


โครงสร้าง Communication Diagram

text┌─────────────────┐         ┌─────────────────┐
│   Customer      │         │   OrderSystem   │
│  (object1)      │ ───────│  (object2)      │
└─────────────────┘  link   └─────────────────┘
       │                           │
       │ 1: placeOrder()           │
       │───────────────────────────>│
       │                           │
       │ 2: confirmOrder()         │
       │<───────────────────────────│
       │                           │

ตัวอย่างที่ 1: Email Notification System

text┌──────────────┐    ┌───────────────┐    ┌──────────────┐
│   Order      │────│  Mailer       │────│  EmailServer │
│  (obj)       │    │   (obj)       │    │  (obj)       │
└──────────────┘    └───────────────┘    └──────────────┘
       │                    │                     │
       │ 1: send()          │                     │
       │───────────────────>│                     │
       │                    │ 2: sendEmail()     │
       │                    │─────────────────────>│
       │                    │                     │
       │                    │ 3: sent = true      │
       │                    │<─────────────────────│
       │ 4: confirmed()     │                     │
       │<───────────────────│                     │
       │                    │                     │

Code equivalent:

javaOrder order = new Order(123);
Mailer mailer = new Mailer();
EmailServer server = new EmailServer();

// 1
order.send();
// 2
mailer.sendEmail();
// 3
server.sendResult = true;
// 4
order.confirmed();

ตัวอย่างที่ 2: Payment Processing

text┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│  Customer    │────│  PaymentUI   │────│  Bank        │
└──────────────┘    └──────────────┘    └──────────────┘
       │                    │                     │
       │ 1: enterCard()     │                     │
       │───────────────────>│                     │
       │                    │ 2: processPayment() │
       │                    │─────────────────────>│
       │                    │                     │
       │                    │ 3: checkFunds()     │
       │                    │<─────────────────────│ (parallel)
       │                    │                     │
       │                    │ 4: authorizePayment │
       │                    │─────────────────────>│
       │                    │                     │
       │                    │ 5: success = true   │
       │                    │<─────────────────────│
       │ 6: showSuccess()   │                     │
       │<───────────────────│                     │
       │                    │                     │

Component Diagram: โครงสร้างของ Components

Component Diagram คืออะไร?

Component Diagram แสดง:

  • Components (ชิ้นส่วนของระบบ)
  • Interfaces (อินเตอร์เฟส)
  • Dependencies (พึ่งพากัน)

ใช้เมื่อ: วาง architecture ของระบบใหญ่ที่แบ่งเป็น modules


โครงสร้าง Component Diagram

1. Component (ชิ้นส่วน)

text┌──────────────────────┐
│<<component>>         │
│ ComponentName        │
└──────────────────────┘

ตัวอย่าง:

text┌──────────────────────┐
│<<component>>         │
│ OrderService         │
└──────────────────────┘

┌──────────────────────┐
│<<component>>         │
│ PaymentGateway       │
└──────────────────────┘

┌──────────────────────┐
│<<component>>         │
│ Database             │
└──────────────────────┘

2. Interface (อินเตอร์เฟส)

text     ◇────────────── ← Provided interface
     │
┌────────────────────┐
│<<component>>       │
│  ComponentA        │
└────────────────────┘
     │
     ◇────────────── ← Required interface

3. Dependency (พึ่งพา)

text┌──────────────┐        ┌──────────────┐
│ OrderUI      │───────>│ OrderService │
└──────────────┘ depends└──────────────┘

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

text                        ┌──────────────────────────────┐
                        │   Web/Client Layer           │
                        │  ┌───────────────────────┐   │
                        │  │<<component>>          │   │
                        │  │ Web UI                │   │
                        │  │ - HTML/CSS/JS         │   │
                        │  └───────────────────────┘   │
                        └──────────────────────────────┘
                                     │
                                     │ depends
                                     ↓
                        ┌──────────────────────────────┐
                        │   Business Logic Layer       │
                        │                              │
    ┌────────────────┐  │  ┌──────────────────────┐   │
    │<<component>>   │  │  │<<component>>         │   │
    │ OrderService   │  │  │ PaymentService       │   │
    └────────────────┘  │  └──────────────────────┘   │
         │ depends      │           │ depends          │
         │              │           │                  │
         └──────────────┼───────────┘                  │
                        │  ┌──────────────────────┐   │
                        │  │<<component>>         │   │
                        │  │ NotificationService  │   │
                        │  └──────────────────────┘   │
                        └──────────────────────────────┘
                                     │
                                     │ depends
                                     ↓
                        ┌──────────────────────────────┐
                        │   Data Access Layer          │
                        │  ┌──────────────────────┐   │
                        │  │<<component>>         │   │
                        │  │ Database Connector   │   │
                        │  │ - SQL Queries        │   │
                        │  └──────────────────────┘   │
                        │  ┌──────────────────────┐   │
                        │  │<<component>>         │   │
                        │  │ Cache Layer          │   │
                        │  └──────────────────────┘   │
                        └──────────────────────────────┘
                                     │
                                     │ depends
                                     ↓
                        ┌──────────────────────────────┐
                        │   External Services Layer    │
                        │  ┌──────────────────────┐   │
                        │  │<<component>>         │   │
                        │  │ Payment Gateway      │   │
                        │  │ (Stripe API)         │   │
                        │  └──────────────────────┘   │
                        │  ┌──────────────────────┐   │
                        │  │<<component>>         │   │
                        │  │ Email Service        │   │
                        │  │ (SendGrid API)       │   │
                        │  └──────────────────────┘   │
                        └──────────────────────────────┘

ตัวอย่างที่ 4: หลายสถานการณ์ Component

text                  ┌────────────────────────────────┐
                  │   Mobile App                   │
                  └────────────────────────────────┘
                            │
                            │ depends
                            ↓
        ┌───────────────────────────────────────┐
        │     API Gateway (REST/GraphQL)        │
        └───────────────────────────────────────┘
                    │       │       │
         ┌──────────┘       │       └──────────┐
         │                  │                  │
         ↓                  ↓                  ↓
    ┌─────────┐        ┌─────────┐        ┌─────────┐
    │ Order   │        │ User    │        │ Product │
    │ Service │        │ Service │        │ Service │
    └─────────┘        └─────────┘        └─────────┘
         │                  │                  │
         └──────────┬───────┴────────┬────────┘
                    │               │
                    ↓               ↓
            ┌──────────────┐  ┌──────────────┐
            │  Database    │  │  Redis Cache │
            │  (PostgreSQL)│  │  (In-Memory) │
            └──────────────┘  └──────────────┘

Deployment Diagram: วาง Hardware/Network

Deployment Diagram คืออะไร?

Deployment Diagram แสดง:

  • Hardware/Servers ที่ติดตั้งระบบ
  • Network connections ระหว่าง servers
  • Components ที่ติดตั้งในแต่ละ server

ใช้เมื่อ: วางแผน deployment, DevOps planning


โครงสร้าง Deployment Diagram

1. Node (Server/Hardware)

text┌──────────────────────┐
│ <<device>>           │
│ ServerName           │
└──────────────────────┘

2. Connection (เชื่อมโยง)

text┌─────────┐             ┌─────────┐
│ Server1 │─────────────│ Server2 │
└─────────┘  TCP/IP     └─────────┘

ตัวอย่างที่ 5: Traditional 3-Tier Deployment

text                    ┌──────────────────────┐
                    │   <<device>>         │
                    │   Client Browser     │
                    │   - Web Client       │
                    │   - JavaScript       │
                    └──────────────────────┘
                               │
                               │ HTTP/HTTPS
                               ↓
                    ┌──────────────────────┐
                    │   <<device>>         │
                    │   Web Server         │
                    │   - Tomcat/Nginx     │
                    │   - Spring Boot      │
                    │   - Order Service    │
                    │   - User Service     │
                    └──────────────────────┘
                               │
                               │ JDBC
                               ↓
                    ┌──────────────────────┐
                    │   <<device>>         │
                    │   Database Server    │
                    │   - PostgreSQL       │
                    │   - MySQL            │
                    │   - Data Storage     │
                    └──────────────────────┘

ตัวอย่างที่ 6: Microservices Architecture Deployment

text┌─────────────────────────────────────────────────────────────┐
│                  Internet / Cloud                            │
│                                                              │
│  ┌──────────────────────────────────────────────────────┐  │
│  │ API Gateway (Load Balancer)                          │  │
│  │ - nginx / HAProxy                                    │  │
│  │ - Request Routing                                    │  │
│  │ - Authentication                                     │  │
│  └──────────────────────────────────────────────────────┘  │
│                     │                                       │
│         ┌───────────┼───────────┬───────────┐              │
│         │           │           │           │              │
│         ↓           ↓           ↓           ↓              │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐     │
│  │ Order    │ │ User     │ │ Payment  │ │ Product  │     │
│  │ Service  │ │ Service  │ │ Service  │ │ Service  │     │
│  │ :8001   │ │ :8002   │ │ :8003   │ │ :8004   │     │
│  └──────────┘ └──────────┘ └──────────┘ └──────────┘     │
│         │           │           │           │              │
│         └───────────┼───────────┴───────────┘              │
│                     │                                       │
│                     ↓                                       │
│          ┌──────────────────────┐                          │
│          │ Message Queue        │                          │
│          │ (RabbitMQ/Kafka)     │                          │
│          └──────────────────────┘                          │
│                     │                                       │
│         ┌───────────┴───────────┐                          │
│         │                       │                          │
│         ↓                       ↓                          │
│  ┌──────────────┐       ┌──────────────┐                  │
│  │ Database     │       │ Cache        │                  │
│  │ (PostgreSQL) │       │ (Redis)      │                  │
│  └──────────────┘       └──────────────┘                  │
│                                                              │
└─────────────────────────────────────────────────────────────┘

ตัวอย่างที่ 7: Cloud Deployment (AWS)

text┌──────────────────────────────────────────────────────────┐
│                   AWS Cloud                              │
│                                                          │
│  ┌────────────────────────────────────────────────────┐ │
│  │ CloudFront (CDN)                                   │ │
│  │ - Static Content Distribution                      │ │
│  └────────────────────────────────────────────────────┘ │
│                     │                                    │
│                     ↓                                    │
│  ┌────────────────────────────────────────────────────┐ │
│  │ ALB (Application Load Balancer)                    │ │
│  │ - Distribute Traffic                              │ │
│  └────────────────────────────────────────────────────┘ │
│                     │                                    │
│         ┌───────────┴───────────┐                       │
│         │                       │                       │
│         ↓                       ↓                       │
│  ┌──────────────────┐   ┌──────────────────┐           │
│  │ EC2 Instances    │   │ EC2 Instances    │           │
│  │ (Auto Scaling)   │   │ (Auto Scaling)   │           │
│  │ - App Server 1-N │   │ - App Server N+1 │           │
│  └──────────────────┘   └──────────────────┘           │
│         │                       │                       │
│         └───────────┬───────────┘                       │
│                     │                                    │
│                     ↓                                    │
│  ┌────────────────────────────────────────────────────┐ │
│  │ RDS (Relational Database Service)                 │ │
│  │ - PostgreSQL / MySQL                              │ │
│  │ - Multi-AZ Replication                            │ │
│  └────────────────────────────────────────────────────┘ │
│         │                                               │
│         ├─────────────────────────────────────────┐    │
│         │                                         │    │
│         ↓                                         ↓    │
│  ┌──────────────────┐              ┌──────────────────┐│
│  │ ElastiCache      │              │ S3 Storage       ││
│  │ (Redis)          │              │ (Static Files)   ││
│  └──────────────────┘              └──────────────────┘│
│                                                          │
└──────────────────────────────────────────────────────────┘

Package Diagram: จัดกลุ่ม Classes

Package Diagram คืออะไร?

Package Diagram แสดง:

  • Packages (กลุ่มของ classes)
  • Dependencies ระหว่าง packages
  • Organizing โปรเจค

ใช้เมื่อ: จัดระเบียบ project structure


โครงสร้าง Package Diagram

1. Package (กลุ่ม)

text┌────────────────────────┐
│ com.example.models     │
├────────────────────────┤
│ - Student              │
│ - Course               │
│ - Enrollment           │
└────────────────────────┘

2. Dependency ระหว่าง Packages

text┌──────────────────┐        ┌──────────────────┐
│ com.example.ui   │───────>│ com.example.service
└──────────────────┘        └──────────────────┘
  (depends on)

ตัวอย่างที่ 8: University System Package Structure

text┌──────────────────────────────────────────────────────────┐
│                 com.university                           │
│                                                          │
│  ┌──────────────────┐      ┌──────────────────┐         │
│  │ presentation     │      │ business.logic   │         │
│  │ (UI Layer)       │─────>│ (Service Layer)  │         │
│  │                  │      │                  │         │
│  │ - StudentUI      │      │ - StudentService │         │
│  │ - CourseUI       │      │ - CourseService  │         │
│  │ - ReportUI       │      │ - EnrollmentMgr  │         │
│  └──────────────────┘      └──────────────────┘         │
│                                    │                     │
│                                    ↓                     │
│  ┌──────────────────┐      ┌──────────────────┐         │
│  │ data.models      │      │ data.repository  │         │
│  │ (Data Objects)   │<─────│ (Data Access)    │         │
│  │                  │      │                  │         │
│  │ - Student        │      │ - StudentDAO     │         │
│  │ - Course         │      │ - CourseDAO      │         │
│  │ - Enrollment     │      │ - DatabaseConn   │         │
│  └──────────────────┘      └──────────────────┘         │
│                                    │                     │
│                                    ↓                     │
│  ┌──────────────────────────────────────────┐           │
│  │ external.database                        │           │
│  │ (External Services)                      │           │
│  │                                          │           │
│  │ - PostgreSQL Database                    │           │
│  │ - MySQL Database                         │           │
│  └──────────────────────────────────────────┘           │
│                                                          │
└──────────────────────────────────────────────────────────┘

ตัวอย่างที่ 9: Detailed Package Dependencies

text┌────────────────────────────────────────────────────────────────┐
│           E-Commerce System Package Structure                   │
│                                                                │
│  ┌──────────────────────┐                                     │
│  │ com.shop.ui          │                                     │
│  │ (Web Presentation)   │                                     │
│  │                      │                                     │
│  │ - ProductUI          │                                     │
│  │ - CartUI             │                                     │
│  │ - CheckoutUI         │                                     │
│  │ - ReportUI           │                                     │
│  └──────────────────────┘                                     │
│           │                                                    │
│           │ depends                                            │
│           ↓                                                    │
│  ┌──────────────────────┐                                     │
│  │ com.shop.services    │                                     │
│  │ (Business Logic)     │                                     │
│  │                      │                                     │
│  │ - ProductService     │                                     │
│  │ - OrderService       │                                     │
│  │ - PaymentService     │                                     │
│  │ - UserService        │                                     │
│  └──────────────────────┘                                     │
│           │       │                                            │
│   depends │       │ depends                                    │
│           ↓       ↓                                            │
│  ┌─────────────────────────┐    ┌──────────────────────┐      │
│  │ com.shop.persistence    │    │ com.shop.util        │      │
│  │ (Data Access)           │    │ (Utilities)          │      │
│  │                         │    │                      │      │
│  │ - StudentDAO            │    │ - ValidationUtil     │      │
│  │ - OrderDAO              │    │ - DateUtil           │      │
│  │ - ProductDAO            │    │ - CurrencyUtil       │      │
│  │ - UserDAO               │    │ - EmailSender        │      │
│  └─────────────────────────┘    └──────────────────────┘      │
│           │                                                    │
│           │ depends                                            │
│           ↓                                                    │
│  ┌──────────────────────┐                                     │
│  │ com.shop.models      │                                     │
│  │ (Domain Objects)     │                                     │
│  │                      │                                     │
│  │ - Product            │                                     │
│  │ - Order              │                                     │
│  │ - OrderItem          │                                     │
│  │ - User               │                                     │
│  │ - Payment            │                                     │
│  └──────────────────────┘                                     │
│           │                                                    │
│           │ depends                                            │
│           ↓                                                    │
│  ┌──────────────────────┐                                     │
│  │ com.shop.exception   │                                     │
│  │ (Custom Exceptions)  │                                     │
│  │                      │                                     │
│  │ - ShopException      │                                     │
│  │ - ValidationException│                                     │
│  │ - PaymentException   │                                     │
│  └──────────────────────┘                                     │
│                                                                │
└────────────────────────────────────────────────────────────────┘

Advanced Class Diagram Features

1. Constraints (ข้อจำกัด)

java// {constraint} ← แสดงในวงเล็บปีกกา

public class Account {
    private double balance;  // {balance >= 0}
    private String pin;      // {pin.length == 4}
}

UML:

text┌──────────────────────┐
│     Account          │
├──────────────────────┤
│ - balance: double    │ {balance >= 0}
│ - pin: String        │ {pin.length == 4}
├──────────────────────┤
│ + withdraw(): void   │
└──────────────────────┘

2. Tagged Values (ข้อมูลเพิ่มเติม)

text┌──────────────────────┐
│     Student          │ {author=John, version=1.0}
├──────────────────────┤
│ - id: String         │
│ - name: String       │
├──────────────────────┤
│ + study(): void      │
└──────────────────────┘

3. Navigability (ทิศทางการใช้)

textไม่ระบุ: ใช้ได้ 2 ทาง
─────────

One-way: A → B (เฉพาะ A ใช้ B)
───────────────────────────────
┌────────────┐           ┌────────────┐
│   Order    │─────────→ │  Product   │
└────────────┘           └────────────┘
   Order รู้จัก Product
   แต่ Product ไม่รู้จัก Order

ตัวอย่างที่ 10: Advanced Class Diagram

text┌─────────────────────────────────────┐
│ <<abstract>>                        │
│ PaymentMethod {author=Team}         │
│ {version=1.0}                       │
├─────────────────────────────────────┤
│ - transactionID: String             │
│ - amount: double {amount > 0}       │
│ - status: String {frozen/active}    │
├─────────────────────────────────────┤
│ + process(): boolean {abstract}     │
│ + validate(): boolean               │
│ + cancel(): void                    │
└─────────────────────────────────────┘
              △
              │ extends
        ┌─────┴──────┐
        │            │
    ┌──────────┐  ┌──────────┐
    │CreditCard│  │BankTransfer
    ├──────────┤  ├──────────┤
    │- cvv     │  │- bankCode│
    │- expDate │  │- accountNo
    └──────────┘  └──────────┘


┌───────────────────────┐
│      Order            │
├───────────────────────┤
│ - orderID: String     │
│ - amount: double      │
├───────────────────────┤
│ + createOrder()       │
└───────────────────────┘
        │ 1
        │ uses
        │ *
        ↓ {navigable}
┌───────────────────────┐
│   PaymentMethod       │
└───────────────────────┘

Communication Protocol: สัญลักษณ์ในทั่ว UML

Relationships Quick Reference

textInheritance (สืบทอด):
────────────────────
     △
     │
  ◀──┤
    Class

Realization (ทำตาม Interface):
──────────────────────────────
     △
     │┈┈┈
  ◀──┤
  Class

Association (ใช้งาน):
──────────────────
ClassA ────────── ClassB
       1       *

Aggregation (รวม - หลวม):
────────────────────────
ClassA ◇───────── ClassB
       1       *

Composition (ประกอบ - แน่น):
───────────────────────────
ClassA ◆───────── ClassB
       1       *

Dependency (พึ่งพา):
───────────────────
ClassA ┈┈┈┈┈┈┈┈> ClassB

Complete Real-World Example: Hospital Management System

ข้อมูลระบบ

ระบบจัดการโรงพยาบาล ที่มี:

  • Patients, Doctors, Nurses
  • Departments
  • Appointments
  • Medical Records
  • Prescriptions

Use Case Diagram

text              ┌──────────────────────────────────────┐
              │   Hospital Management System         │
              │                                      │
  ┌────────┐  │  ┌──────────────┐                   │
  │Patient │──┼──│ Book         │                   │
  └────────┘  │  │ Appointment  │                   │
              │  └──────────────┘                   │
              │                                      │
  ┌────────┐  │  ┌──────────────┐                   │
  │ Doctor │──┼──│ View Patient │                   │
  └────────┘  │  │ Records      │                   │
              │  └──────────────┘                   │
              │       │                             │
              │       │<<include>>                  │
              │       ↓                             │
              │  ┌──────────────┐                   │
              │  │ Prescribe    │                   │
              │  │ Medication   │                   │
              │  └──────────────┘                   │
              │                                      │
  ┌────────┐  │  ┌──────────────┐                   │
  │Nurse   │──┼──│ Manage Care  │                   │
  └────────┘  │  │ Plan         │                   │
              │  └──────────────┘                   │
              │                                      │
  ┌────────┐  │  ┌──────────────┐                   │
  │ Admin  │──┼──│ Manage Staff │                   │
  └────────┘  │  └──────────────┘                   │
              │                                      │
              └──────────────────────────────────────┘

Class Diagram (Partial)

text┌──────────────────────────┐
│       Person             │
│ (abstract)               │
├──────────────────────────┤
│ # personID: String       │
│ # name: String           │
│ # email: String          │
│ # phone: String          │
├──────────────────────────┤
│ + getInfo(): String      │
└──────────────────────────┘
        △
        │
   ┌────┴─────────┐
   │              │
┌──────────┐  ┌──────────────┐
│  Doctor  │  │   Patient    │
├──────────┤  ├──────────────┤
│- license │  │- medicalHist │
│- specialty
├──────────┤  ├──────────────┤
│+ consult()
│+ prescribe()
└──────────┘  └──────────────┘
   △ 1            1 │
   │ treats        │ has
   │ many          │ many
   │ *             │ *
   └────┬──────────┘
        │
        ↓
┌────────────────────┐
│  Appointment       │
├────────────────────┤
│ - appointmentID    │
│ - dateTime         │
│ - reason: String   │
│ - status: String   │
├────────────────────┤
│ + schedule()       │
│ + cancel()         │
└────────────────────┘
        │
        │ 1
        │ has
        │ 1
        ↓
┌────────────────────┐
│  MedicalRecord     │
├────────────────────┤
│ - recordID         │
│ - diagnosis        │
│ - treatment        │
│ - date: Date       │
├────────────────────┤
│ + updateRecord()   │
└────────────────────┘
        │
        │ 1
        │ contains
        │ *
        ↓
┌────────────────────┐
│ Prescription       │
├────────────────────┤
│ - prescriptionID   │
│ - medication       │
│ - dosage           │
│ - duration         │
├────────────────────┤
│ + fillPrescription()
└────────────────────┘

Component Diagram

text┌──────────────────────────────────────┐
│    Hospital System                   │
│                                      │
│ ┌──────────────────────────────────┐│
│ │ Presentation Layer              ││
│ │ - PatientUI, DoctorUI, AdminUI  ││
│ └──────────────────────────────────┘│
│                  │                   │
│                  ↓                   │
│ ┌──────────────────────────────────┐│
│ │ Business Logic Layer             ││
│ │ - AppointmentService             ││
│ │ - PatientService                 ││
│ │ - PrescriptionService            ││
│ └──────────────────────────────────┘│
│                  │                   │
│                  ↓                   │
│ ┌──────────────────────────────────┐│
│ │ Data Access Layer                ││
│ │ - PatientDAO                     ││
│ │ - DoctorDAO                      ││
│ │ - AppointmentDAO                 ││
│ └──────────────────────────────────┘│
│                  │                   │
│                  ↓                   │
│ ┌──────────────────────────────────┐│
│ │ Database                         ││
│ │ - MySQL / PostgreSQL             ││
│ └──────────────────────────────────┘│
│                                      │
└──────────────────────────────────────┘

Sequence Diagram: Book Appointment

text┌──────────┐    ┌─────────┐    ┌──────────────┐    ┌──────────┐
│ Patient  │    │   UI    │    │AppointmentSvc
│          │    │         │    │              │    │Database  │
└──────────┘    └─────────┘    └──────────────┘    └──────────┘
       │              │              │                   │
       │ selectDate() │              │                   │
       │─────────────>│              │                   │
       │              │              │                   │
       │              │ checkAvail() │                   │
       │              │─────────────>│                   │
       │              │              │ query()           │
       │              │              │──────────────────>│
       │              │              │ available = true  │
       │              │              │<──────────────────│
       │              │              │                   │
       │              │ bookAppmt()  │                   │
       │              │─────────────>│                   │
       │              │              │ insert()          │
       │              │              │──────────────────>│
       │              │              │ success           │
       │              │              │<──────────────────│
       │              │<─────────────│                   │
       │              │ confirmed    │                   │
       │<─────────────│              │                   │
       │              │              │                   │

สรุปตอนที่ 3

เราได้เรียนรู้:

✅ Communication Diagram – Interaction แบบเน้น structure
✅ Component Diagram – โครงสร้างของ components
✅ Deployment Diagram – วาง hardware/network
✅ Package Diagram – จัดกลุ่ม classes
✅ Advanced Class Features – Constraints, Tagged Values
✅ Complete Real-World Example – Hospital Management System


🎯 ในตอนต่อไป เราจะเรียนรู้:

  • Object Diagram (Instance-level diagrams)
  • Profile Diagram (UML Extensions)
  • Timing Diagram (Real-time systems)
  • Interaction Overview Diagram
  • Design Patterns ใน UML
  • Best Practices for UML Modeling