Software Architecture Patterns

Back to Software Development Index

Clean Architecture

Read detailed Clean Architecture guide →

วิธีการจัดการโปรเจ็คเพื่อให้สามารถ maintain และ scale ได้ง่าย

Core Principles

  1. มีการแยก layer การทำงานของ application ให้มีส่วนการทำงานแต่ละชั้นออกจากกันอย่างชัดเจน โดยแต่ละ layer จะมี dependency ได้แค่ layer ที่อยู่ชั้นในที่ติดกับตัวมันเอง
  2. Layer ที่อยู่ภายนอกจะเรียกใช้ layer ที่อยู่ภายใน โดย layer ภายนอกสุดจะขึ้นอยู่กับ platform ที่ทำงาน ในขณะที่ layer ที่อยู่ชั้นในสุดจะเป็น pure business logic ที่ไม่ขึ้นกับ platform ใด ๆ
  3. แต่ละ component จะมีการจัดการงานแค่ 1 เรื่อง (Single Responsibility)

Layers

โดยแต่ละ layer ประกอบไปด้วย:

1. Entities

  • จุดที่รวม business logic
  • ประกอบไปด้วย POJOs, value object ที่ใช้ในการเก็บข้อมูล
  • ไม่ทำอะไรอย่างอื่น
  • Examples: Bank, Interest, User

2. Use Cases

  • Action ต่างๆ ที่เปิดให้เรียกใช้ business logic
  • เป็น pure code ไม่มี platform dependency ใด ๆ อยู่
  • กำหนดกฎเกณฑ์ทางธุรกิจ

3. Controllers, Presenters, Adapters

  • ทำหน้าที่นำข้อมูลที่ได้จาก Use cases มาแสดงผลในรูปแบบที่ต้องการ เช่น JSON, XML
  • ทำหน้าที่จัดหาข้อมูลส่งให้ entities layer จาก datasources หลายๆ แหล่ง
  • Implement จาก interface ที่จัดไว้ให้จาก layer ที่อยู่ชั้นในกว่า

4. Infrastructure (DB, UI, etc.)

  • เป็น layer ที่เปลี่ยนแปลงบ่อยที่สุด
  • มีหน้าที่แสดงผล และรับ event จาก user หรือรับ input จาก datasources ต่างๆ
  • ประกอบไปด้วยการเรียกใช้งาน library หรือ framework ของ platform ต่าง เช่น Android framework

Dependency Rule

Source code dependencies can only point inwards

วงภายในจะต้องไม่รับรู้และไม่พึ่งพาอาศัยโค้ดจากวงภายนอก แต่วงนอกพึ่งพาอาศัยวงภายในตามลูกศร depend on

Hexagonal Architecture

Hexagonal architecture หรือ ports and adapters architecture

We can say Hexagonal architecture is a model of designing software applications around domain logic to isolate it from external factors.

Key Characteristics

  • Isolate domain logic from external concerns
  • Ports - Interfaces that define how the application communicates
  • Adapters - Implementations that connect to external systems
  • Flexibility - Easy to swap adapters without changing core logic

Benefits

  1. Testability - Core logic can be tested in isolation
  2. Flexibility - Easy to change infrastructure components
  3. Maintainability - Clear separation of concerns
  4. Technology independence - Core doesn’t depend on frameworks

Model View Controller (MVC)

การออกแบบโครงสร้างให้มีระบบ Core System ที่สามารถ Input / Output ข้อมูลไปยังหลายระบบได้

Components

  • Model - Business logic and data
  • View - Presentation layer
  • Controller - Handles user input and updates model

Purpose

  • ออกแบบให้มี Layer ที่จะช่วยเป็นตัวกลางแปลงข้อมูลให้ระหว่างระบบที่ต้องทำงานร่วมกันได้
  • รูปแบบ MVC จะเน้นเรื่องของการทำงานเชื่อมต่อกันทั้งในระบบภายในที่มีหลายส่วน

Architecture Comparison

PatternFocusComplexityBest For
Clean ArchitectureBusiness logic isolationHighLarge enterprise apps
HexagonalPort/adapter flexibilityMediumDomain-driven apps
MVCUI separationLowWeb applications

Related:

References: