UML Diagram (ตอนที่ 4)

📢 ประกาศ: ซีรี่นี้มีทั้งหมด 4 ตอน

  • ✅ ตอนที่ 1: Class Diagram พื้นฐาน + 6 Relationships
  • ✅ ตอนที่ 2: Behavior Diagrams (Use Case, Activity, Sequence, State Machine)
  • ✅ ตอนที่ 3: Structural Diagrams ขั้นสูง (Communication, Component, Deployment, Package)
  • 🎬 ตอนที่ 4 (ตอนนี้): Advanced Diagrams + Design Patterns + Best Practices

บทนำตอนสุดท้าย: รวบรวมทั้งหมด

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

  1. Object Diagram – ตัวอย่างข้อมูลในเวลาหนึ่ง
  2. Timing Diagram – ระบบ real-time
  3. Interaction Overview Diagram – ภาพรวม interactions
  4. Design Patterns ใน UML – แบบแผนที่พบบ่อย
  5. Best Practices – วิธีดีในการใช้ UML
ยาวไปอยากเลือกอ่าน
  1. 📢 ประกาศ: ซีรี่นี้มีทั้งหมด 4 ตอน
  2. บทนำตอนสุดท้าย: รวบรวมทั้งหมด
  3. Object Diagram: ตัวอย่างข้อมูล (Instance-Level)
  4. Object Diagram คืออะไร?
  5. โครงสร้าง Object Diagram
  6. 1. Object (Instance)
  7. ตัวอย่างที่ 1: University System Objects
  8. ตัวอย่างที่ 2: Bank System State
  9. Timing Diagram: ระบบ Real-Time
  10. Timing Diagram คืออะไร?
  11. โครงสร้าง Timing Diagram
  12. ตัวอย่างที่ 3: Traffic Light System
  13. ตัวอย่างที่ 4: Elevator System
  14. Interaction Overview Diagram: ภาพรวม
  15. Interaction Overview Diagram คืออะไร?
  16. ตัวอย่างที่ 5: Order Processing Flow
  17. Design Patterns ใน UML
  18. 1. Creational Patterns (สร้างอ็อบเจกต์)
  19. Singleton Pattern
  20. Factory Pattern
  21. 2. Structural Patterns (โครงสร้าง)
  22. Adapter Pattern
  23. Decorator Pattern
  24. 3. Behavioral Patterns (พฤติกรรม)
  25. Observer Pattern
  26. Strategy Pattern
  27. Best Practices: วิธีดีในการใช้ UML
  28. 1. ความชัดเจน (Clarity)
  29. 2. Consistency (ความสม่ำเสมอ)
  30. 3. Appropriate Level of Detail
  31. 4. Meaningful Relationships
  32. 5. Use Packages Effectively
  33. 6. Document Your Diagrams
  34. 7. Use Multiple Diagrams
  35. Complete Project Example: Online Voting System
  36. 1. Use Case Diagram
  37. 2. Class Diagram
  38. 3. Sequence Diagram: Voting Process
  39. 4. State Machine Diagram: Voter State
  40. 5. Component Diagram
  41. UML Tools ที่นิยม
  42. Cheat Sheet: UML ทั้งหมด 14 Diagrams
  43. สรุปทั้งชุด: ทำไมต้อง UML?
  44. ✅ ประโยชน์
  45. ตัวอย่างสุดท้าย: Social Media Platform
  46. คำแนะนำสุดท้าย: ดำเนินการจาก Today
  47. Resources ที่ปะหนักแนะนำ
  48. สรุปตอนที่ 4 (ตอนสุดท้าย)
  49. 🎉 ขอบคุณที่ติดตามซีรี่นี้!
  50. สิ่งที่คุณได้เรียนรู้:

Object Diagram: ตัวอย่างข้อมูล (Instance-Level)

Object Diagram คืออะไร?

Object Diagram คล้ายกับ Class Diagram แต่:

  • แสดง instances (objects จริง) แทน classes
  • แสดง ค่าข้อมูล ในเวลาหนึ่ง
  • ใช้สำหรับ ตัวอย่างข้อมูล ของระบบ

ใช้เมื่อ: อธิบาย object state ใน runtime


โครงสร้าง Object Diagram

1. Object (Instance)

text┌────────────────────┐
│ objectName : Class │
├────────────────────┤
│ attribute = value  │
│ attribute = value  │
└────────────────────┘

ตัวอย่าง:

text┌────────────────────┐
│ john : Student     │
├────────────────────┤
│ studentID = 6501   │
│ name = "John"      │
│ gpa = 3.75         │
└────────────────────┘

ตัวอย่างที่ 1: University System Objects

text┌──────────────────────┐    ┌──────────────────────┐
│ john : Student       │    │ cs101 : Course       │
├──────────────────────┤    ├──────────────────────┤
│ id = "6501001"       │    │ code = "CS101"       │
│ name = "John Doe"    │    │ title = "OOP"        │
│ gpa = 3.75           │    │ credits = 3          │
│ year = 2             │    │ capacity = 40        │
└──────────────────────┘    └──────────────────────┘
        │                             │
        │ enrolls                     │ has
        │ 1                        1 |
        │                            │
        ↓                            ↓
┌──────────────────────────────────────────────┐
│ enrollment1 : Enrollment                     │
├──────────────────────────────────────────────┤
│ enrollmentID = "E001"                        │
│ enrollDate = "2024-09-01"                    │
│ grade = null                                 │
│ status = "active"                            │
└──────────────────────────────────────────────┘

┌──────────────────────┐
│ dr_smith : Teacher   │
├──────────────────────┤
│ id = "T001"          │
│ name = "Dr. Smith"   │
│ department = "CS"    │
│ salary = 50000       │
└──────────────────────┘
        │
        │ teaches
        │ 1
        │
        └──> cs101

ตัวอย่างที่ 2: Bank System State

text┌──────────────────────────┐
│ account1 : BankAccount   │
├──────────────────────────┤
│ accountNumber = "ACC001" │
│ accountHolder = "Alice"  │
│ balance = 50000          │
│ status = "active"        │
└──────────────────────────┘
        │ 1
        │ contains
        │ *
        ↓
┌──────────────────────────┐
│ tx1 : Transaction        │
├──────────────────────────┤
│ transactionID = "TX001"  │
│ type = "deposit"         │
│ amount = 5000            │
│ date = "2024-10-31"      │
└──────────────────────────┘

┌──────────────────────────┐
│ tx2 : Transaction        │
├──────────────────────────┤
│ transactionID = "TX002"  │
│ type = "withdrawal"      │
│ amount = 2000            │
│ date = "2024-10-31"      │
└──────────────────────────┘

Timing Diagram: ระบบ Real-Time

Timing Diagram คืออะไร?

Timing Diagram แสดง:

  • ค่าของ attributes เปลี่ยนแปลงตามเวลา
  • State changes ของ objects
  • Signals และ events ตามเวลา

ใช้เมื่อ: วิเคราะห์ real-time systems, embedded systems


โครงสร้าง Timing Diagram

textObject/Signal Name
        │
        │ value
    ┌───┼─────┐
    │   │     │
┌───┴───┴─────┴────────────→ time
└─

ตัวอย่างที่ 3: Traffic Light System

text┌─────────────────────────────────────────────────────┐
│ Traffic Light Timing Diagram                        │
└─────────────────────────────────────────────────────┘

Light State
    │
    │ RED
    ├─────────────────┐
    │                 │
    │         GREEN   │
    │         ├───────┤
    │         │       │ YELLOW
    │         │       ├──┐
    │         │       │  │ RED
    │         │       │  ├───────────────────────
    │         │       │  │
    └─────────┴───────┴──┴─ time (seconds)
    0         5      8   10          15

Duration:
- RED: 0-5 seconds (5s)
- GREEN: 5-8 seconds (3s)
- YELLOW: 8-10 seconds (2s)
- RED: 10-15+ seconds (cycle repeats)

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

text┌──────────────────────────────────────────────┐
│ Elevator Timing (Request to Destination)     │
└──────────────────────────────────────────────┘

Floor Position
    │
    │ Floor 1
    ├────┐
    │    │ Moving Up
    │    ├──────┐
    │    │      │ Floor 2
    │    │      ├────┐
    │    │      │    │ Moving Down
    │    │      │    ├──────┐
    │    │      │    │      │ Floor 1
    │    │      │    │      ├────
    │    │      │    │      │
    └────┴──────┴────┴──────┴─ time (seconds)
    0    2      5    7      10

Events:
- 0s: Request from Floor 1 to Floor 2
- 2s: Doors close, start moving up
- 5s: Arrive at Floor 2
- 7s: Doors open, passenger exits
- 10s: Ready for next request

State:
┌──────────┬──────────┬──────────┬──────────┐
│ IDLE     │ MOVING   │ STOPPED  │ IDLE     │
│ 0-2s     │ 2-5s     │ 5-10s    │ 10+s     │
└──────────┴──────────┴──────────┴──────────┘

Interaction Overview Diagram: ภาพรวม

Interaction Overview Diagram คืออะไร?

Interaction Overview Diagram คือ:

  • High-level view ของหลาย sequence diagrams
  • รวม multiple interactions เป็นหนึ่ง diagram
  • มี control flow เหมือน activity diagram

ใช้เมื่อ: อธิบาย complex scenarios ที่มี branches


ตัวอย่างที่ 5: Order Processing Flow

text                    ┌────────────────────┐
                    │ Start Order        │
                    └────────────────────┘
                            │
                            ↓
                    ┌────────────────────┐
                    │ seq: Browse Items  │ ← Sequence 1
                    └────────────────────┘
                            │
                            ↓
                    ◇ Items Selected?
                   ╱ ╲
                yes   no
                ╱     ╲
               ↓       ↓
        ┌──────────┐ ┌──────────┐
        │seq: Add  │ │seq: Exit │
        │to Cart   │ │Order     │
        └──────────┘ └──────────┘
               │            │
               └──────┬─────┘
                      ↓
            ┌─────────────────────┐
            │ seq: Proceed to     │ ← Sequence 2
            │ Checkout            │
            └─────────────────────┘
                      │
                      ↓
            ◇ Confirm Order?
           ╱ ╲
        yes   no
        ╱     ╲
       ↓       ↓
    ┌───────┐ ┌─────────┐
    │seq:   │ │seq:     │
    │Payment│ │Edit     │
    │       │ │Order    │
    └───────┘ └─────────┘
       │            │
       └──────┬─────┘
              ↓
    ┌──────────────────────┐
    │ seq: Send            │ ← Sequence 3
    │ Confirmation         │
    └──────────────────────┘
              │
              ↓
    ┌──────────────────┐
    │ End              │
    └──────────────────┘

Design Patterns ใน UML

Design Pattern คือ แบบแผนสำหรับแก้ปัญหา ที่พบบ่อยในการออกแบบซอฟต์แวร์

มี 23 patterns คลาสสิก แบ่งเป็น 3 กลุ่ม


1. Creational Patterns (สร้างอ็อบเจกต์)

Singleton Pattern

ปัญหา: ต้องการให้มี instance เดียว

text┌──────────────────────────┐
│ Singleton                │
├──────────────────────────┤
│ - instance: Singleton    │
│ - Singleton() private    │
├──────────────────────────┤
│ + getInstance(): Singleton
└──────────────────────────┘

Code:

javapublic class Database {
    private static Database instance = null;
    
    private Database() {
        // ห้าม new
    }
    
    public static Database getInstance() {
        if (instance == null) {
            instance = new Database();
        }
        return instance;
    }
}

// ใช้งาน
Database db1 = Database.getInstance();
Database db2 = Database.getInstance();
// db1 == db2 (instance เดียวกัน)

Factory Pattern

ปัญหา: สร้าง objects แบบ polymorphic โดยไม่ต้องรู้ concrete class

text┌──────────────────┐
│ <<interface>>    │
│ Shape            │
├──────────────────┤
│ + draw(): void   │
└──────────────────┘
        △
        │
   ┌────┴────┐
   │         │
┌─────┐  ┌─────┐
│Circle│  │Square
└─────┘  └─────┘

┌────────────────────────┐
│ ShapeFactory           │
├────────────────────────┤
│ + createShape(type)    │
│   : Shape              │
└────────────────────────┘

Code:

javapublic interface Shape {
    void draw();
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing circle");
    }
}

public class ShapeFactory {
    public static Shape createShape(String type) {
        if (type.equals("circle")) {
            return new Circle();
        } else if (type.equals("square")) {
            return new Square();
        }
        return null;
    }
}

// ใช้งาน
Shape shape = ShapeFactory.createShape("circle");
shape.draw();

2. Structural Patterns (โครงสร้าง)

Adapter Pattern

ปัญหา: ทำให้ incompatible interfaces ทำงานกันได้

text┌──────────────┐       ┌──────────────┐
│LegacySystem  │       │TargetInterface
└──────────────┘       └──────────────┘
       │                        △
       │ adapted by             │
       │                        │
       └─→ ┌─────────────────────┐
           │ Adapter             │
           └─────────────────────┘

Code:

java// Legacy system
public class LegacyPaymentSystem {
    public void processOldPayment(double amount) {
        System.out.println("Processing: " + amount);
    }
}

// Target interface
public interface PaymentGateway {
    void pay(double amount);
}

// Adapter
public class PaymentAdapter implements PaymentGateway {
    private LegacyPaymentSystem legacy;
    
    public PaymentAdapter() {
        this.legacy = new LegacyPaymentSystem();
    }
    
    @Override
    public void pay(double amount) {
        legacy.processOldPayment(amount);
    }
}

Decorator Pattern

ปัญหา: เพิ่ม functionality ให้ object โดยไม่แก้ original class

text┌──────────────────┐
│<<interface>>     │
│Coffee            │
├──────────────────┤
│+ cost(): double  │
└──────────────────┘
        △
        │
   ┌────┴────┐
   │         │
┌────────┐  ┌──────────────────┐
│SimpleCof│  │CoffeeDecorator   │
└────────┘  ├──────────────────┤
            │- coffee: Coffee  │
            ├──────────────────┤
            │+ cost(): double  │
            └──────────────────┘
                    △
                    │
            ┌───────┴────────┐
            │                │
        ┌────────────┐  ┌──────────┐
        │MilkDecorator
        │WithMilk    │  │WithSugar │
        └────────────┘  └──────────┘

Code:

javapublic interface Coffee {
    double cost();
}

public class SimpleCoffee implements Coffee {
    @Override
    public double cost() {
        return 50;
    }
}

public abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee;
    
    public CoffeeDecorator(Coffee coffee) {
        this.coffee = coffee;
    }
}

public class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }
    
    @Override
    public double cost() {
        return coffee.cost() + 10;  // เพิ่ม 10 บาท
    }
}

// ใช้งาน
Coffee coffee = new SimpleCoffee();
coffee = new MilkDecorator(coffee);
coffee = new MilkDecorator(coffee);  // เพิ่มนม 2 ครั้ง
System.out.println(coffee.cost());  // 50 + 10 + 10 = 70

3. Behavioral Patterns (พฤติกรรม)

Observer Pattern

ปัญหา: แจ้งเตือน multiple objects เมื่อ state เปลี่ยน

text┌──────────────────────┐
│ Subject              │
│ (Observable)         │
├──────────────────────┤
│ + attach(obs)        │
│ + detach(obs)        │
│ + notify()           │
└──────────────────────┘
           │
           │ notifies
           │ many
           ↓
┌──────────────────────┐
│ Observer             │
│ (Listener)           │
├──────────────────────┤
│ + update()           │
└──────────────────────┘
           △
           │
      ┌────┴────┐
      │         │
    ┌────────┐ ┌────────┐
    │ObserverA
    │        │ │ObserverB
    └────────┘ └────────┘

Code:

javapublic interface Observer {
    void update(String news);
}

public class NewsPublisher {
    private List<Observer> observers = new ArrayList<>();
    
    public void attach(Observer obs) {
        observers.add(obs);
    }
    
    public void publishNews(String news) {
        for (Observer obs : observers) {
            obs.update(news);  // แจ้งทุกคน
        }
    }
}

public class NewsReader implements Observer {
    private String name;
    
    public NewsReader(String name) {
        this.name = name;
    }
    
    @Override
    public void update(String news) {
        System.out.println(name + " received: " + news);
    }
}

// ใช้งาน
NewsPublisher publisher = new NewsPublisher();
Observer reader1 = new NewsReader("Alice");
Observer reader2 = new NewsReader("Bob");

publisher.attach(reader1);
publisher.attach(reader2);

publisher.publishNews("Breaking news!");
// Alice received: Breaking news!
// Bob received: Breaking news!

Strategy Pattern

ปัญหา: เปลี่ยนอัลกอริธึม runtime

text┌──────────────────┐
│Context           │
├──────────────────┤
│- strategy        │
├──────────────────┤
│+ execute()       │
└──────────────────┘
           │
           │ uses
           ↓
┌──────────────────┐
│<<interface>>     │
│Strategy          │
├──────────────────┤
│+ execute()       │
└──────────────────┘
           △
           │
      ┌────┴────┐
      │         │
  ┌────────┐ ┌────────┐
  │StrategyA
  │        │ │StrategyB
  └────────┘ └────────┘

Code:

javapublic interface PaymentStrategy {
    void pay(double amount);
}

public class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("Paying " + amount + " with credit card");
    }
}

public class PayPalPayment implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("Paying " + amount + " with PayPal");
    }
}

public class PaymentContext {
    private PaymentStrategy strategy;
    
    public void setStrategy(PaymentStrategy strategy) {
        this.strategy = strategy;
    }
    
    public void checkout(double amount) {
        strategy.pay(amount);
    }
}

// ใช้งาน
PaymentContext context = new PaymentContext();

context.setStrategy(new CreditCardPayment());
context.checkout(1000);  // Paying 1000 with credit card

context.setStrategy(new PayPalPayment());
context.checkout(1000);  // Paying 1000 with PayPal

Best Practices: วิธีดีในการใช้ UML

1. ความชัดเจน (Clarity)

text✓ ดี: ชื่อที่อธิบายได้
───────────────────────
Class: StudentEnrollmentManager
Method: enrollStudentInCourse()
Attribute: studentID

✗ ไม่ดี: ชื่อที่สั้นเกินไป
──────────────────────────
Class: Mgr
Method: enroll()
Attribute: id

2. Consistency (ความสม่ำเสมอ)

text✓ ดี: ตั้งชื่อสม่ำเสมอ
──────────────────────
getter: getStudentID()
getter: getStudentName()
getter: getStudentGPA()

setters: setStudentID()
setters: setStudentName()
setters: setStudentGPA()

✗ ไม่ดี: ชื่อไม่เสมอกัน
──────────────────────
getStudentID()
fetchStudentName()
gpa (direct access)

3. Appropriate Level of Detail

text✓ ดี: ประมาณเหมาะสม
─────────────────────
ระดับ high-level: แสดงเฉพาะ public interfaces
ระดับ design: แสดง classes, relationships, cardinality
ระดับ implementation: แสดงทุก attributes, methods, modifiers

✗ ไม่ดี: มากหรือน้อยเกินไป
──────────────────────────
ในไดอะแกรมเดียว มี classes 100+ ตัว
หรือแสดงเฉพาะบางส่วนไม่ครบ

4. Meaningful Relationships

text✓ ดี: ความสัมพันธ์ที่มีความหมาย
────────────────────────────────
Student ──> Course (enrolls in)
Teacher ──> Course (teaches)
Order ──> Product (contains)

✗ ไม่ดี: เชื่อมโยงทั้งหมด
─────────────────────────
Every class connected to every class
ทำให้ diagram เป็นสป็ด (spaghetti)

5. Use Packages Effectively

text✓ ดี: จัดกลุ่มอย่างมีเหตุผล
──────────────────────────
com.university.presentation
com.university.service
com.university.persistence
com.university.model

✗ ไม่ดี: จัดกลุ่มแบบสุ่ม
─────────────────────────
com.university.utils
com.university.other
com.university.stuff

6. Document Your Diagrams

text┌──────────────────────────────────┐
│ E-Commerce System                │
│ Version: 1.0                     │ ← Metadata
│ Date: 2024-10-31                 │
│ Author: Design Team              │
│ Notes: MVP Implementation         │
│                                  │
│ [Diagram Content]                │
│                                  │
│ Legend:                          │
│ ◆ = Composition                  │
│ ◇ = Aggregation                  │
│ → = Dependency                   │
└──────────────────────────────────┘

7. Use Multiple Diagrams

text✓ ดี: แยกตามมุมมองต่างๆ
───────────────────────
diagram 1: Class diagram (structure)
diagram 2: Use case diagram (functionality)
diagram 3: Sequence diagram (interaction)
diagram 4: Component diagram (architecture)

✗ ไม่ดี: ทั้งหมดในไดอะแกรมเดียว
─────────────────────────────
ทำให้ diagram ยุ่งและยากอ่าน

Complete Project Example: Online Voting System

1. Use Case Diagram

text              ┌──────────────────────┐
              │ Voting System        │
              │                      │
  ┌────────┐  │  ┌──────────────┐   │
  │ Voter  │──┼──│ Register     │   │
  └────────┘  │  └──────────────┘   │
              │         │            │
              │         │<<include>> │
              │         ↓            │
              │  ┌──────────────┐    │
              │  │ Verify Email │    │
              │  └──────────────┘    │
              │                      │
              │  ┌──────────────┐    │
              ├──│ Vote         │    │
              │  └──────────────┘    │
              │         │            │
              │         │<<extend>>  │
              │         │(if confirm)│
              │         ↓            │
              │  ┌──────────────┐    │
              │  │ Confirm Vote │    │
              │  └──────────────┘    │
              │                      │
  ┌────────┐  │  ┌──────────────┐    │
  │ Admin  │──┼──│ View Results │    │
  └────────┘  │  └──────────────┘    │
              │                      │
              │  ┌──────────────┐    │
              ├──│ Manage       │    │
              │  │ Elections    │    │
              │  └──────────────┘    │
              │                      │
              └──────────────────────┘

2. Class Diagram

text┌────────────────────────────────┐
│        Person                  │
│ (abstract)                     │
├────────────────────────────────┤
│ # personID: String             │
│ # name: String                 │
│ # email: String                │
├────────────────────────────────┤
│ + getInfo(): String            │
└────────────────────────────────┘
        △
        │
   ┌────┴──────┐
   │           │
┌────────┐  ┌──────────┐
│ Voter  │  │   Admin  │
├────────┤  ├──────────┤
│- status│  │- role    │
├────────┤  ├──────────┤
│+ vote()│  │+ createElection()
└────────┘  └──────────┘
   │ 1           1 │
   │ participates │ manages
   │ *            │ *
   ↓              ↓
┌─────────────────────────┐
│    Election             │
├─────────────────────────┤
│ - electionID: String    │
│ - title: String         │
│ - startDate: Date       │
│ - endDate: Date         │
│ - status: String        │
├─────────────────────────┤
│ + startVoting(): void   │
│ + endVoting(): void     │
└─────────────────────────┘
   │ 1
   │ contains
   │ *
   ↓
┌─────────────────────────┐
│  Candidate              │
├─────────────────────────┤
│ - candidateID: String   │
│ - name: String          │
│ - platform: String      │
│ - voteCount: int        │
├─────────────────────────┤
│ + getVoteCount()        │
└─────────────────────────┘
        △
        │ 1
        │ receives
        │ many
        │ *
   ┌────┴────────┐
   │             │
┌────────┐  ┌─────────┐
│ Vote   │  │ Voter   │
└────────┘  └─────────┘

3. Sequence Diagram: Voting Process

text┌────────┐    ┌──────────┐    ┌──────────┐    ┌──────────┐
│ Voter  │    │ VotingUI │    │ VoteService
│        │    │          │    │          │    │Database  │
└────────┘    └──────────┘    └──────────┘    └──────────┘
      │             │              │              │
      │ selectCand()│              │              │
      │────────────>│              │              │
      │             │              │              │
      │             │ submitVote() │              │
      │             │─────────────>│              │
      │             │              │              │
      │             │              │ checkVoter() │
      │             │              │─────────────>│
      │             │              │              │
      │             │              │ voterValid   │
      │             │              │<─────────────│
      │             │              │              │
      │             │              │ recordVote() │
      │             │              │─────────────>│
      │             │              │              │
      │             │              │ voteID       │
      │             │              │<─────────────│
      │             │ success      │              │
      │             │<─────────────│              │
      │ confirmation│              │              │
      │<────────────│              │              │
      │             │              │              │

4. State Machine Diagram: Voter State

text         ┌─────────┐
         │ Initial │
         └─────────┘
              │
              │ register
              ↓
         ┌──────────┐
         │ Registered
         │          │ {awaiting email verification}
         └──────────┘
              │
              │ verify_email
              ↓
         ┌──────────┐
         │ Verified │ {can vote}
         └──────────┘
              │
              │ election_open
              ↓
         ┌──────────┐
         │ Voting   │ {casting ballot}
         └──────────┘
              │
              │ vote_submitted
              ↓
         ┌──────────┐
         │ Voted    │ {vote counted}
         └──────────┘
              │
              │ election_closed
              ↓
         ┌──────────┐
         │ Final    │ {voting complete}
         └──────────┘

Exception paths:
- Verified ──→ resend_verification ──→ Verified
- Voting ──→ cancel_vote ──→ Verified

5. Component Diagram

text┌──────────────────────────────────────┐
│     Voting System Architecture       │
│                                      │
│ ┌───────────────────────────────┐   │
│ │ Presentation Layer            │   │
│ │ - Web UI, Admin Dashboard     │   │
│ └───────────────────────────────┘   │
│                 │                    │
│                 ↓                    │
│ ┌───────────────────────────────┐   │
│ │ Business Logic Layer          │   │
│ │ - VoteService                 │   │
│ │ - ElectionService             │   │
│ │ - AuthService                 │   │
│ └───────────────────────────────┘   │
│                 │                    │
│                 ↓                    │
│ ┌───────────────────────────────┐   │
│ │ Data Access Layer             │   │
│ │ - VoteDAO                     │   │
│ │ - ElectionDAO                 │   │
│ │ - VoterDAO                    │   │
│ └───────────────────────────────┘   │
│                 │                    │
│                 ↓                    │
│ ┌───────────────────────────────┐   │
│ │ Database & Cache              │   │
│ │ - PostgreSQL Database         │   │
│ │ - Redis Cache                 │   │
│ └───────────────────────────────┘   │
│                                      │
└──────────────────────────────────────┘

UML Tools ที่นิยม

text┌─────────────────────────────────────┐
│ Online Tools (ฟรี)                  │
├─────────────────────────────────────┤
│ - draw.io / diagrams.net            │
│ - Lucidchart                        │
│ - PlantUML                          │
│ - Gliffy                            │
│ - Creately                          │
└─────────────────────────────────────┘

┌─────────────────────────────────────┐
│ Desktop Tools                       │
├─────────────────────────────────────┤
│ - Enterprise Architect              │
│ - Visual Paradigm                   │
│ - Rational Rose / IBM Rhapsody      │
│ - StarUML                           │
│ - Astah                             │
└─────────────────────────────────────┘

┌─────────────────────────────────────┐
│ IDE Integration                     │
├─────────────────────────────────────┤
│ - IntelliJ IDEA (built-in)          │
│ - NetBeans                          │
│ - Eclipse                           │
│ - VS Code Extensions                │
└─────────────────────────────────────┘

Cheat Sheet: UML ทั้งหมด 14 Diagrams

text┌─────────────────────────────────────────────────┐
│          UML Diagrams (14 ประเภท)               │
├─────────────────────────────────────────────────┤
│ STRUCTURE DIAGRAMS (โครงสร้าง)                 │
├─────────────────────────────────────────────────┤
│ 1. Class Diagram           → Classes & Relationships
│ 2. Object Diagram          → Instances at runtime
│ 3. Component Diagram       → Components & Dependencies
│ 4. Deployment Diagram      → Hardware & Network
│ 5. Package Diagram         → Organize Classes
│ 6. Composite Structure     → Internal Structure
│ 7. Profile Diagram         → Extend UML
│                                                 │
├─────────────────────────────────────────────────┤
│ BEHAVIOR DIAGRAMS (พฤติกรรม)                   │
├─────────────────────────────────────────────────┤
│ 8. Use Case Diagram        → System Functions
│ 9. Activity Diagram        → Workflows (Flowchart)
│10. Sequence Diagram        → Interactions Over Time
│11. Communication Diagram   → Interactions (Structure)
│12. State Machine Diagram   → Object States
│13. Timing Diagram          → Time-Based Changes
│14. Interaction Overview    → High-level Interactions
│                                                 │
└─────────────────────────────────────────────────┘

สรุปทั้งชุด: ทำไมต้อง UML?

✅ ประโยชน์

text1. 📊 Communication
   - ทีมเข้าใจตรงกัน
   - Client เข้าใจ requirements

2. 🏗️ Architecture Planning
   - เห็นภาพรวมของระบบ
   - จำแนก problems

3. 📝 Documentation
   - บันทึก design decisions
   - ติดตามการเปลี่ยนแปลง

4. 🔄 Maintenance
   - เข้าใจระบบเดิมได้ง่าย
   - ปรับแต่งง่ายกว่า

5. 🛡️ Quality Assurance
   - ตรวจสอบ design ก่อนเขียนโค้ด
   - ลดบัง mistake

6. 🎓 Learning
   - เรียนรู้ OOP concepts
   - design patterns

ตัวอย่างสุดท้าย: Social Media Platform

textFULL PROJECT OVERVIEW

Use Case:
- Users: register, login, post, like, comment, follow
- Admin: manage content, ban users

Class Diagram:
User ──1─┬──* Post
         └──* Comment
         
Post ──1──* Comment
      1──* Like

Activity Diagram:
[Create Post Flow]
User inputs → Validate → Upload image → Publish → Notify followers

Sequence Diagram:
[Comment Process]
User → UI → Service → Database → Cache

Component Diagram:
UI ──→ Service Layer ──→ Data Layer ──→ Database

Deployment:
Load Balancer ──→ [Servers] ──→ Database + Cache
         ↓
    CDN (Images)

คำแนะนำสุดท้าย: ดำเนินการจาก Today

text📌 Week 1: Learn the Basics
   ├─ Class Diagram
   ├─ Relationships (6 types)
   └─ Multiplicity

📌 Week 2: Understand Behavior
   ├─ Use Case Diagram
   ├─ Activity Diagram
   ├─ Sequence Diagram
   └─ State Machine

📌 Week 3: System Architecture
   ├─ Component Diagram
   ├─ Deployment Diagram
   ├─ Package Diagram
   └─ Communication Diagram

📌 Week 4: Advanced & Patterns
   ├─ Object Diagram
   ├─ Design Patterns
   ├─ Best Practices
   └─ Real-world Projects

📌 Ongoing: Practice
   ├─ Draw diagrams for your projects
   ├─ Use UML tools (draw.io, PlantUML)
   ├─ Review and refine
   └─ Share with team

Resources ที่ปะหนักแนะนำ

text📚 Books:
- "UML Distilled" by Martin Fowler
- "Design Patterns" by Gang of Four

🌐 Online:
- www.uml.org (Official)
- Pluralsight courses
- Udemy UML courses

🛠️ Tools:
- draw.io (ฟรี, ใช้ง่าย)
- PlantUML (code-based, version control friendly)

📺 Channels:
- Tutorials Point (YouTube)
- Coursera UML Specialization

สรุปตอนที่ 4 (ตอนสุดท้าย)

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

✅ Object Diagram – ตัวอย่างข้อมูลจริง
✅ Timing Diagram – Real-time systems
✅ Interaction Overview – ภาพรวม interactions
✅ Design Patterns – 6 patterns ที่สำคัญ
✅ Best Practices – วิธีดีในการใช้ UML
✅ Complete Project Example – Voting System
✅ All 14 UML Diagrams – Cheat Sheet


🎉 ขอบคุณที่ติดตามซีรี่นี้!

สิ่งที่คุณได้เรียนรู้:

✨ ตอนที่ 1: Class Diagram + 6 Relationships
✨ ตอนที่ 2: Behavior Diagrams (4 types)
✨ ตอนที่ 3: Structural Diagrams ขั้นสูง
✨ ตอนที่ 4: Advanced + Patterns + Best Practices