SOLID Principles: Writing Scalable Code

SOLID Principles: Writing Scalable Code

Learn the fundamental principles of object-oriented software design in TypeScript, covering SRP, OCP, LSP, ISP, and DIP with real-world refactoring examples.

SOLIDClean CodeTypeScriptArchitecture

The Foundation of Maintainable Code

Software Project တစ်ခု စတင်တည်ဆောက်တဲ့အချိန်မှာ Code တွေဟာ ရိုးရှင်းပြီး ရေးသားရလွယ်ကူနေတတ်ပါတယ်။ ဒါပေမဲ့ Feature အသစ်တွေ ထပ်တိုးလာတာနဲ့အမျှ Codebase ဟာ တဖြည်းဖြည်း ရှုပ်ထွေးလာပြီး ပြင်ဆင်ထိန်းသိမ်းရခက်ခဲလာနိုင်ပါတယ်။ ဖိုင်တစ်ခုမှာ ပြောင်းလဲမှုအနည်းငယ် လုပ်လိုက်တာနဲ့ မသက်ဆိုင်တဲ့ အခြားနေရာတွေမှာပါ Bugs တွေ ပေါ်လာတတ်တဲ့ Fragility ပြဿနာဟာ Software Design ကို စနစ်တကျ မတည်ဆောက်ထားခြင်းရဲ့ အကျိုးဆက်တစ်ခု ဖြစ်ပါတယ်။

ဒီလို ပြဿနာတွေကို လျှော့ချပြီး ရေရှည် Maintain လုပ်ရလွယ်ကူတဲ့ Code တွေ ရေးသားနိုင်ဖို့ SOLID Principles တွေကို နားလည်ထားဖို့ လိုအပ်ပါတယ်။ Robert C. Martin (Uncle Bob) က လူသိများအောင် စုစည်းတင်ပြခဲ့တဲ့ ဒီ Principles ၅ ခုဟာ Object-Oriented Software Design မှာ အရေးပါတဲ့ အခြေခံသဘောတရားတွေ ဖြစ်ပါတယ်။

ဒီဆောင်းပါးမှာတော့ Software Engineering Trilogy ရဲ့ ပထမပိုင်း (Part 1) အနေနဲ့ SOLID Principles တစ်ခုချင်းစီကို Modern TypeScript နဲ့ လက်တွေ့ဥပမာတွေ အသုံးပြုပြီး လေ့လာသွားပါမယ်။

Loading flowchart...

1. Single Responsibility Principle (SRP)

"A class should have one, and only one, reason to change."

Class သို့မဟုတ် Module တစ်ခုမှာ တာဝန်ယူရမယ့် အကြောင်းအရာတစ်ခုတည်း (Single Responsibility) ရှိသင့်ပါတယ်။ ဒီနေရာမှာ Responsibility ဆိုတာ လုပ်ဆောင်ချက်တစ်ခုတည်းကို ဆိုလိုတာမဟုတ်ဘဲ ပြောင်းလဲမှုဖြစ်ပေါ်လာစေတဲ့ အကြောင်းရင်း (Reason to Change) တစ်ခုတည်းကို ဆိုလိုတာ ဖြစ်ပါတယ်။

ဥပမာ UserService တစ်ခုတည်းမှာ User Registration, Database Query, Email ပို့ခြင်းနဲ့ Email Template ပြင်ဆင်ခြင်းတွေကို အားလုံး ရောထွေးရေးသားထားတယ်ဆိုပါစို့။ Email Template ပြောင်းလဲတဲ့အခါ UserService ကိုပါ ပြင်ဆင်နေရခြင်းဟာ တာဝန်တွေ ရောထွေးနေတဲ့ လက္ခဏာတစ်ခု ဖြစ်ပါတယ်။

အဲဒီအစား တာဝန်တစ်ခုချင်းစီကို သက်ဆိုင်ရာ Class တွေဆီ ခွဲဝေပေးနိုင်ပါတယ်။

export class UserService {
  constructor(
    private readonly userRepo: UserRepository,
    private readonly emailService: EmailService
  ) {}

  async register(data: CreateUserDTO): Promise<User> {
    const user = new User(data)
    await this.userRepo.save(user)
    await this.emailService.sendWelcomeEmail(user.email)
    return user
  }
}

ဒီလို ခွဲခြားထားခြင်းအားဖြင့် Database နဲ့ Email ပိုင်းကို သီးခြားပြင်ဆင်နိုင်ပြီး Class တစ်ခုချင်းစီကိုလည်း သီးခြား Test လုပ်ရ ပိုလွယ်ကူလာပါတယ်။

2. Open-Closed Principle (OCP)

"Software entities should be open for extension, but closed for modification."

Software Components တွေဟာ Feature အသစ်တွေ ထပ်တိုးဖို့ လွယ်ကူရမယ်။ ဒါပေမဲ့ ရှိပြီးသား အလုပ်လုပ်နေတဲ့ Code တွေကို ပြင်ဆင်စရာမလိုအောင် ဒီဇိုင်းဆွဲထားသင့်ပါတယ်။

ဥပမာ Payment Method အသစ်တစ်ခု ထပ်ထည့်တိုင်း ရှိပြီးသား switch (paymentMethod) ထဲမှာ case အသစ်တွေ လိုက်ထည့်နေရတယ်ဆိုရင် Payment Logic တစ်ခုလုံးကို မကြာခဏ ပြင်ဆင်နေရမှာ ဖြစ်ပါတယ်။

Interface နဲ့ Polymorphism ကို အသုံးပြုခြင်းအားဖြင့် Payment Method အသစ်တွေကို ရှိပြီးသား PaymentService ကို မပြင်ဘဲ ထပ်တိုးနိုင်ပါတယ်။

interface PaymentProcessor {
  process(amount: number): Promise<boolean>
}

class StripeProcessor implements PaymentProcessor {
  async process(amount: number) {
    return true
  }
}

class PaymentService {
  async checkout(processor: PaymentProcessor, amount: number) {
    return processor.process(amount)
  }
}

အခုဆိုရင် Payment Method အသစ်တစ်ခု ထပ်ထည့်ချင်တဲ့အခါ PaymentProcessor Interface ကို Implement လုပ်တဲ့ Class အသစ်တစ်ခု ဖန်တီးရုံနဲ့ ရပါပြီ။

3. Liskov Substitution Principle (LSP)

"Subtypes must be substitutable for their base types without altering program correctness."

Subclass တစ်ခုဟာ Parent Type ရဲ့ နေရာမှာ အစားထိုးအသုံးပြုလိုက်တဲ့အခါ Program ရဲ့ မူလအပြုအမူနဲ့ မျှော်လင့်ထားတဲ့ ရလဒ်တွေကို မပျက်စီးစေရပါဘူး။

တစ်နည်းအားဖြင့် Parent Class သို့မဟုတ် Interface တစ်ခုကို အသုံးပြုထားတဲ့ Code ဟာ အဲဒီ Type ရဲ့ Subtypes တွေကို အစားထိုးအသုံးပြုတဲ့အခါလည်း မှန်ကန်စွာ အလုပ်လုပ်နေရပါမယ်။

ဥပမာ Read-only Repository တစ်ခုကို အသုံးပြုတဲ့ Client ဟာ Write လုပ်နိုင်တဲ့ Repository ဖြစ်စေ၊ Read-only Repository ဖြစ်စေ ပြဿနာမရှိဘဲ အသုံးပြုနိုင်သင့်ပါတယ်။

// Separating read and write capabilities
interface ReadableRepo<T> {
  findById(id: string): Promise<T | null>
}

interface MutableRepo<T> extends ReadableRepo<T> {
  delete(id: string): Promise<void>
}

// Any ReadableRepo implementation can be used safely
async function getProfile(
  repo: ReadableRepo<User>,
  id: string
) {
  return repo.findById(id)
}

ဒီလို Capability အလိုက် Interface တွေ ခွဲခြားသတ်မှတ်ထားခြင်းက Subtype တစ်ခုဟာ Parent Type ရဲ့ မျှော်လင့်ချက်တွေကို မချိုးဖောက်စေဘဲ အစားထိုးအသုံးပြုနိုင်ဖို့ အထောက်အကူပြုပါတယ်။

4. Interface Segregation Principle (ISP)

"Clients should not be forced to depend upon interfaces that they do not use."

Client တစ်ခုဟာ သူအသုံးမပြုတဲ့ Methods တွေအတွက် Interface တစ်ခုလုံးကို မှီခိုနေရခြင်းမျိုး မဖြစ်သင့်ပါဘူး။

Interface တစ်ခုထဲမှာ မတူညီတဲ့ တာဝန်တွေကို အများကြီး စုပေါင်းထည့်ထားတာကို Fat Interface လို့ ခေါ်ပါတယ်။ ဒီလို Interface ကြီးတွေကို အသုံးပြုတဲ့အခါ မလိုအပ်တဲ့ Methods တွေကိုပါ Implement လုပ်ပေးနေရနိုင်ပါတယ်။

အဲဒီအစား လုပ်ဆောင်ချက်အလိုက် သေးငယ်ပြီး သီးခြားအသုံးပြုနိုင်တဲ့ Interfaces တွေ ခွဲထုတ်သင့်ပါတယ်။

interface Workable {
  work(): void
}

interface Feedable {
  eat(): void
}

class HumanWorker implements Workable, Feedable {
  work() {
    // Working logic
  }
  eat() {
    // Eating logic
  }
}

class RobotWorker implements Workable {
  work() {
    // Working without needing eat()
  }
}

ဒီဥပမာမှာ RobotWorker က eat() Method ကို အသုံးပြုစရာမလိုတာကြောင့် Feedable Interface ကို Implement လုပ်စရာမလိုတော့ပါဘူး။ ဒါကြောင့် Class တစ်ခုချင်းစီဟာ လိုအပ်တဲ့ Contract တွေကိုသာ လိုက်နာရမှာ ဖြစ်ပါတယ်။

5. Dependency Inversion Principle (DIP)

"High-level modules should not depend on low-level modules. Both should depend on abstractions."

High-level Business Logic တွေဟာ Database, Cache, Mailer စတဲ့ Concrete Implementations တွေအပေါ် တိုက်ရိုက် မှီခိုမနေသင့်ပါဘူး။ အဲဒီအစား Interface လိုမျိုး Abstractions တွေအပေါ်မှာသာ မှီခိုသင့်ပါတယ်။

ဥပမာ OrderService တစ်ခုက PostgreSQL Database ကို တိုက်ရိုက်အသုံးပြုနေမယ်ဆိုရင် Database နည်းပညာ ပြောင်းလဲတဲ့အခါ OrderService ကိုပါ ပြင်ဆင်ရနိုင်ပါတယ်။

Interface တစ်ခုကို ကြားခံအသုံးပြုခြင်းအားဖြင့် Business Logic နဲ့ Database Implementation ကို သီးခြားခွဲထားနိုင်ပါတယ်။

interface IDatabase {
  query(sql: string): Promise<unknown[]>
}

class PostgresDatabase implements IDatabase {
  async query(sql: string) {
    return []
  }
}

export class OrderService {
  constructor(private readonly db: IDatabase) {}

  async listOrders() {
    return this.db.query('SELECT * FROM orders')
  }
}

ဒီပုံစံမှာ OrderService က PostgreSQL ကို တိုက်ရိုက်သိရှိစရာမလိုဘဲ IDatabase Interface ကိုသာ သိရှိထားဖို့ လိုပါတယ်။ နောက်ပိုင်းမှာ Database Implementation အသစ်တစ်ခု ပြောင်းသုံးချင်ရင်လည်း Business Logic ကို မထိခိုက်စေဘဲ အစားထိုးနိုင်ပါတယ်။

ဒါ့အပြင် Dependency Inversion Principle ဟာ Unit Testing ရေးသားတဲ့အခါမှာလည်း ကြီးမားတဲ့ အားသာချက်တစ်ခု ဖြစ်စေပါတယ်။ Production Database ကို တိုက်ရိုက်ချိတ်ဆက်စရာမလိုဘဲ In-Memory Mock Database ဒါမှမဟုတ် Stub Implementation တစ်ခုကို OrderService ထဲ အလွယ်တကူ Inject လုပ်ပြီး Test Cases တွေကို မြန်မြန်ဆန်ဆန် စမ်းသပ်နိုင်ပါတယ်။ Dependency Injection (DI) Container တွေဖြစ်တဲ့ NestJS, Awilix စတဲ့ Modern Tools တွေဟာ ဒီသဘောတရားအပေါ် အခြေခံပြီး တည်ဆောက်ထားတာ ဖြစ်ပါတယ်။

Pragmatic Engineering Trade-offs

SOLID Principles တွေကို လက်တွေ့အသုံးချတဲ့အခါ အချိုးအစားညီညွတ်မှု (Pragmatic Balance) ရှိဖို့ အလွန်အရေးကြီးပါတယ်။ Simple CRUD Application ဒါမှမဟုတ် သေးငယ်တဲ့ Utility Script တစ်ခုအတွက် Interface တွေ၊ Classes တွေ အလွန်အကျွံ ခွဲထုတ်ခြင်း (Over-Engineering) ဟာ Codebase ကို မလိုအပ်ဘဲ ရှုပ်ထွေးသွားစေနိုင်ပါတယ်။

Senior Engineer တွေအနေနဲ့ စဉ်းစားရမယ့် အချက်ကတော့ Codebase ၏ လက်ရှိ Scale နဲ့ ပြောင်းလဲနိုင်ခြေ (Volatility) ဖြစ်ပါတယ်။ မကြာခဏ ပြောင်းလဲလေ့ရှိတဲ့ Core Domain Logic တွေ၊ Complex Payment Gateways တွေနဲ့ External Integrations တွေမှာ SOLID Principles ကို တိကျစွာ လိုက်နာသင့်ပြီး တစ်ကြိမ်သာ ရေးသားရမယ့် Simple Scripts တွေမှာတော့ သင့်တင့်တဲ့ ရိုးရှင်းမှုကို ထိန်းသိမ်းသင့်ပါတယ်။

SOLID Principles Reference Matrix

Principle တစ်ခုချင်းစီရဲ့ အဓိက ရည်ရွယ်ချက်၊ ရရှိနိုင်တဲ့ အကျိုးကျေးဇူးနဲ့ သတိထားသင့်တဲ့ Code Smells တွေကို အောက်ပါဇယားမှာ အကျဉ်းချုပ် ဖော်ပြထားပါတယ်။

PrincipleCore ConceptPrimary BenefitCode Smell to Watch
SRPSingle reason to changeHigh cohesion, simpler testingGod Objects, mixed responsibilities
OCPOpen for extension, closed for modificationFlexibility, safer changesExpanding switch-cases on types
LSPBehavior-safe substitutionReliable polymorphismSubclasses violating parent contracts
ISPFocused role-based interfacesLean client contractsDummy or empty method implementations
DIPDecoupling via abstractionsTest isolation, flexibilityHardcoded concrete dependencies

Final Thoughts

SOLID Principles တွေဟာ စာအုပ်ထဲမှာ ဖော်ပြထားတဲ့အတိုင်း အခြေအနေတိုင်းမှာ မျက်စိမှိတ် လိုက်နာရမယ့် စည်းမျဉ်းသေတွေ မဟုတ်ပါဘူး။ အဓိက ရည်ရွယ်ချက်ကတော့ Codebase ကြီးထွားလာတဲ့အခါ ပြင်ဆင်ရလွယ်ကူပြီး ပြောင်းလဲမှုတစ်ခုကြောင့် မသက်ဆိုင်တဲ့ အခြားအစိတ်အပိုင်းတွေကို ထိခိုက်မှုနည်းစေဖို့ ဖြစ်ပါတယ်။

တစ်ခါတလေ မလိုအပ်ဘဲ Abstraction တွေ၊ Interfaces တွေ အများကြီး ခွဲထုတ်လိုက်တာက Code ကို ပိုမိုရှုပ်ထွေးသွားစေနိုင်ပါတယ်။ ဒါကြောင့် Principle တစ်ခုချင်းစီကို အသုံးပြုတဲ့အခါ လက်ရှိ Codebase ရဲ့ ပြဿနာနဲ့ လိုအပ်ချက်ကို အရင်နားလည်ဖို့ အရေးကြီးပါတယ်။

SOLID Principles တွေကို သေချာနားလည်ထားခြင်းက Class နဲ့ Module အဆင့်မှာ Maintainable Code တွေ ရေးသားနိုင်ဖို့ အခြေခံကောင်းတစ်ခု ဖြစ်ပါတယ်။ ဒီအခြေခံပေါ်မှာပဲ နောက်အဆင့်ဖြစ်တဲ့ Design Patterns နဲ့ System Architecture တွေကို ပိုမိုထိရောက်စွာ နားလည်အသုံးချနိုင်မှာ ဖြစ်ပါတယ်။