
Design Patterns: Solving Common Problems
Learn how to solve recurring software design problems in TypeScript using battle-tested creational, structural, and behavioral patterns with clean real-world code.
Bridging Micro-Design to System Solutions
SOLID Principles (Part 1) တွေက Classes နဲ့ Interfaces တွေရဲ့ အတွင်းပိုင်း ဖွဲ့စည်းပုံ (Micro-design) ကို ဘယ်လို ရှင်းလင်းပြီး စနစ်တကျ ဖြစ်အောင် တည်ဆောက်မလဲဆိုတာ လမ်းညွှန်ပေးတဲ့ အခြေခံစည်းမျဉ်းတွေ ဖြစ်ပါတယ်။ Design Patterns တွေကတော့ Objects အချင်းချင်း ဘယ်လို ပူးပေါင်းလုပ်ဆောင်ကြမလဲ (Collaboration) ဆိုတာကို ဖြေရှင်းပေးတဲ့ Reusable Architectural Solutions (Meso-design) တွေ ဖြစ်ပါတယ်။
ဒီအပိုင်းမှာတော့ Software Engineering Trilogy ရဲ့ ဒုတိယပိုင်း (Part 2) အနေနဲ့ Gang of Four (GoF) Design Patterns တွေထဲက အသုံးများတဲ့ Patterns တွေကို Modern TypeScript နဲ့ ဘယ်လို အသုံးချနိုင်မလဲဆိုတာ Creational, Structural နဲ့ Behavioral ဆိုပြီး အမျိုးအစား ၃ မျိုး ခွဲခြားလေ့လာသွားပါမယ်။
Design Patterns တွေဟာ သီးခြား Library တစ်ခု မဟုတ်သလို အလွတ်ကျက်မှတ်ရမယ့် Syntax တွေလည်း မဟုတ်ပါဘူး။ Software Engineer တွေ မျိုးဆက်တစ်ခုပြီးတစ်ခု ကြုံတွေ့ဖြေရှင်းခဲ့ရတဲ့ အတွေ့အကြုံတွေကနေ ထွက်ပေါ်လာတဲ့ မျှဝေခံယူထားတဲ့ နည်းလမ်းများ (Shared Vocabulary) သာ ဖြစ်ပါတယ်။
1. Creational Patterns: Decoupling Object Creation
Creational Patterns တွေရဲ့ အဓိက ရည်ရွယ်ချက်က Object တစ်ခုကို ဘယ်လို ဖန်တီးမလဲဆိုတဲ့ Logic ကို Client Code နဲ့ သီးခြားခွဲထုတ်ပေးဖို့ ဖြစ်ပါတယ်။ ဒါကြောင့် Client Code ထဲမှာ new ConcreteClass() လို့ Concrete Class တွေကို တိုက်ရိုက် သိထားစရာမလိုတော့ဘဲ Object Creation Process ကို သီးခြား ထိန်းချုပ်နိုင်ပါတယ်။
- Factory Pattern: Object ဖန်တီးတဲ့ တာဝန်ကို သီးခြား Method သို့မဟုတ် Class တစ်ခုဆီ လွှဲပြောင်းပေးပါတယ်။ ဥပမာ User ရွေးချယ်ထားတဲ့ Notification Channel အလိုက် Email, SMS သို့မဟုတ် Push Notifier တွေကို သင့်တော်သလို ဖန်တီးပေးခြင်း ဖြစ်ပါတယ်။
- Singleton Pattern: Database Connection Pool သို့မဟုတ် Global Configuration လိုမျိုး Application တစ်ခုအတွင်း Instance တစ်ခုတည်းသာ ရှိသင့်တဲ့ Shared Resources တွေကို ထိန်းချုပ်ပေးပါတယ်။
interface INotificationSender {
send(recipient: string, message: string): Promise<void>
}
export class NotificationFactory {
static getSender(type: 'email' | 'push'): INotificationSender {
if (type === 'email') {
return new EmailSender()
}
if (type === 'push') {
return new PushSender()
}
throw new Error(`Unsupported channel: ${type}`)
}
}
Singleton Pattern ကို အသုံးပြုတဲ့အခါ Global Mutable State အဖြစ် အလွဲသုံးစား မလုပ်မိစေရန် သတိထားရပါမယ်။ Modern Architecture တွင် Singleton ကို Static Variable ဖြင့် မဆောက်ဘဲ Dependency Injection Container (NestJS သို့မဟုတ် Awilix) မှတစ်ဆင့် Scoped Lifecycle ဖြင့် စီမံခန့်ခွဲခြင်းက ပိုမိုစိတ်ချရပါတယ်။
2. Structural Patterns: Composing Flexible Relationships
Structural Patterns တွေက မတူညီတဲ့ Classes နဲ့ Objects တွေကို ပေါင်းစပ်ပြီး ပိုမိုကြီးမားတဲ့ System တစ်ခုအဖြစ် ဖွဲ့စည်းတဲ့အခါ လိုအပ်တဲ့ ဆက်သွယ်မှုနဲ့ ဖွဲ့စည်းပုံတွေကို ရိုးရှင်းစေပါတယ်။ အဓိကအားဖြင့် Existing Components တွေကို ပြန်လည်အသုံးချနိုင်ပြီး System တစ်ခုလုံးရဲ့ ဖွဲ့စည်းပုံကို ပြောင်းလွယ်ပြင်လွယ် ရှိစေဖို့ ကူညီပေးပါတယ်။
- Adapter Pattern: မတူညီတဲ့ Interfaces နှစ်ခုကြားမှာ ကြားခံ Wrapper အဖြစ် လုပ်ဆောင်ပေးပါတယ်။ ဥပမာ AWS S3, Cloudflare R2 သို့မဟုတ် Local File Storage SDK တွေကို Internal Domain Interface တစ်ခုတည်းကနေ တပြေးညီ အသုံးပြုနိုင်စေပါတယ်။
- Decorator Pattern: Existing Object ရဲ့ မူလ Code ကို ပြင်ဆင်စရာမလိုဘဲ လုပ်ဆောင်ချက်အသစ်တွေ (Logging, Caching, Retry Logic) ကို ထပ်ဆောင်းပေးပါတယ်။ Subclass တွေ အများကြီး ဖန်တီးစရာမလိုဘဲ လိုအပ်တဲ့ Features တွေကို အလွယ်တကူ ပေါင်းစပ်အသုံးပြုနိုင်ပါတယ်။
// Adapter: Normalizing external third-party SDK to internal domain interface
interface StorageService {
save(filename: string, data: Buffer): Promise<string>
}
class ThirdPartyCloudClient {
uploadBlob(key: string, buffer: Buffer) {
return `https://cdn.example.com/${key}`
}
}
export class CloudStorageAdapter implements StorageService {
constructor(private readonly client: ThirdPartyCloudClient) {}
async save(filename: string, data: Buffer): Promise<string> {
return this.client.uploadBlob(filename, data)
}
}
Adapter Pattern ဟာ Third-party Library အပြောင်းအလဲများ ဖြစ်ပေါ်လာတဲ့အခါ Application ၏ Core Business Logic ကို ထိခိုက်မှုမရှိစေဘဲ Adapter Class ကိုသာ ပြင်ဆင်ရုံဖြင့် လုံလောက်စေပါတယ်။
3. Behavioral Patterns: Managing Object Interactions
Behavioral Patterns တွေက Objects အချင်းချင်း ဘယ်လို ဆက်သွယ်ကြမလဲ၊ Data နဲ့ Events တွေကို ဘယ်လို ပေးပို့ကြမလဲ၊ လုပ်ဆောင်မှုတွေကို ဘယ်လို ပူးပေါင်းဆောင်ရွက်ကြမလဲဆိုတဲ့ ပြဿနာတွေကို ဖြေရှင်းပေးပါတယ်။ ဒါ့အပြင် Object တစ်ခုရဲ့ တာဝန်နဲ့ အခြား Object တွေရဲ့ တာဝန်တွေကို သီးခြားခွဲထားနိုင်ဖို့လည်း ကူညီပေးပါတယ်။
- Strategy Pattern: တူညီတဲ့ ရည်ရွယ်ချက်ကို အကောင်အထည်ဖော်နိုင်တဲ့ လုပ်ဆောင်မှုနည်းလမ်းတွေကို သီးခြားစီ ခွဲထုတ်ထားပြီး Runtime မှာ လိုအပ်သလို အပြန်အလှန် ပြောင်းလဲအသုံးပြုနိုင်စေပါတယ်။ ဥပမာ Payment Gateways (Stripe, PayPal, Crypto) သို့မဟုတ် Shipping Fee Calculators တွေမှာ အသုံးပြုနိုင်ပါတယ်။
- Observer Pattern: Object တစ်ခုရဲ့ State ပြောင်းလဲသွားတဲ့အခါ အဲဒီ Object ကို Subscribe လုပ်ထားတဲ့ Listeners တွေဆီ အလိုအလျောက် Event ပေးပို့အသိပေးပါတယ်။ Event Emitters, In-app Notifications နဲ့ UI State Management တွေမှာ အသုံးများပါတယ်။
interface PaymentStrategy {
pay(amount: number): Promise<boolean>
}
export class PaymentProcessor {
constructor(private strategy: PaymentStrategy) {}
setStrategy(strategy: PaymentStrategy) {
this.strategy = strategy
}
async execute(amount: number) {
return this.strategy.pay(amount)
}
}
Strategy Pattern မရှိလျှင် Developer များသည် Controller အတွင်း switch-case သို့မဟုတ် if-else အစုအပြုံလိုက် ရေးသားရတတ်ပြီး Feature အသစ်တစ်ခု ထပ်ထည့်တိုင်း မူလ Code ကို ပြင်ဆင်နေရခြင်း (OCP ချိုးဖောက်ခြင်း) ဖြစ်ပေါ်စေပါတယ်။ Strategy ခွဲထုတ်လိုက်ခြင်းဖြင့် Payment Provider အသစ်ထည့်သွင်းရန် Strategy Class တစ်ခု တိုးချဲ့ရုံသာ လိုအပ်ပါတော့တယ်။
Design Patterns Decision Matrix
Pattern တစ်ခုချင်းစီက ဖြေရှင်းပေးတဲ့ ပြဿနာနဲ့ TypeScript မှာ အသုံးပြုနိုင်တဲ့ နည်းလမ်းတွေကို အောက်ပါဇယားမှာ နှိုင်းယှဉ်ကြည့်နိုင်ပါတယ်။
| Pattern | Category | Primary Problem Solved | TypeScript Implementation | Common Anti-pattern |
|---|---|---|---|---|
| Singleton | Creational | Shared instance management | Private constructor, DI container | Global mutable state leak |
| Factory | Creational | Dynamic object creation | Factory method, interface polymorphism | Over-engineering simple objects |
| Adapter | Structural | Interface mismatch / SDK integration | Wrapper class implementing domain interface | Wrapping already compatible types |
| Decorator | Structural | Dynamic feature extension | Wrapper classes / Higher-order functions | Deeply nested decorator stacks |
| Strategy | Behavioral | Interchangeable business logic | Strategy interface, dependency injection | Premature abstraction for static logic |
| Observer | Behavioral | Decoupled event notification | Set-based listeners, EventEmitter | Memory leaks from unhandled cleanup |
Pragmatic Pattern Selection
Senior Engineer တစ်ယောက်အနေဖြင့် Design Patterns ကို အသုံးချရာတွင် အမြဲသတိပြုရမည့် အချက်မှာ Premature Abstraction ဖြစ်ပါတယ်။ ရိုးရှင်းသော ပြဿနာတစ်ခုကို ဖြေရှင်းရန် အလွှာများစွာ ခွဲထုတ်ခြင်းသည် Codebase ကို နားလည်ရခက်ခဲစေပါတယ်။
ဒါ့အပြင် Modern TypeScript ၏ First-Class Functions, Closures နှင့် Structural Typing စနစ်များသည် Classic Object-Oriented Patterns အချို့ကို ပိုမိုရိုးရှင်းစွာ ရေးသားနိုင်စေပါတယ်။ ဥပမာ Method တစ်ခုတည်းသာ ပါဝင်သော Strategy Pattern အတွက် သီးခြား Class မဆောက်ဘဲ Function Type တစ်ခုဖြင့် တိုက်ရိုက် အစားထိုးနိုင်သလို Decorator Pattern ကိုလည်း Higher-Order Functions (HOFs) များဖြင့် ရိုးရှင်းစွာ တည်ဆောက်နိုင်ပါတယ်။
Pattern တစ်ခုကို အသုံးပြုသင့်သည့် အချိန်မှာ Codebase ကြီးထွားလာ၍ တူညီသော Logic များကို နေရာအနှံ့ ထပ်ခါတလဲလဲ ရေးသားနေရခြင်း၊ Third-party SDKs များ မကြာခဏ ပြောင်းလဲနေခြင်း၊ သို့မဟုတ် Runtime တွင် Algorithm များကို လွတ်လပ်စွာ အစားထိုးရန် လိုအပ်လာသည့် အခါမှသာ ဖြစ်ပါတယ်။
Final Thoughts
Design Patterns တွေဟာ Project တစ်ခု စတင်ကတည်းက ကြိုတင်ထည့်သွင်းအသုံးပြုရမယ့် အရာတွေ မဟုတ်ပါဘူး။ လက်ရှိ Codebase မှာ ရှုပ်ထွေးမှုတွေ များလာတာ၊ Feature အသစ်တွေ ထပ်ထည့်ဖို့ ခက်ခဲလာတာ၊ Components တွေကြား Coupling များလာတာ စတဲ့ ပြဿနာတွေ ကြုံလာတဲ့အခါမှ သင့်တော်တဲ့ Pattern ကို ရွေးချယ်အသုံးပြုသင့်ပါတယ်။
တစ်ခါတလေ Design Pattern တစ်ခုကို အသုံးပြုလိုက်တာက Code ကို ပိုရှုပ်ထွေးသွားစေနိုင်ပါတယ်။ ဒါကြောင့် Pattern တစ်ခုကို အသုံးပြုဖို့ထက် အဲဒီ Pattern က ဖြေရှင်းပေးမယ့် ပြဿနာ တကယ်ရှိမရှိကို အရင်စဉ်းစားဖို့ အရေးကြီးပါတယ်။
SOLID Principles နဲ့ Design Patterns တွေကို ပေါင်းစပ်နားလည်ထားခြင်းက Class တစ်ခုချင်းစီရဲ့ တာဝန်တွေကို ရှင်းလင်းစေရုံသာမက Objects တွေ အချင်းချင်း ပူးပေါင်းလုပ်ဆောင်ပုံကိုလည်း ပိုမိုကောင်းမွန်စေပါတယ်။ ဒီအခြေခံတွေက Maintainable, Flexible နဲ့ Scalable ဖြစ်တဲ့ Software Systems တွေ တည်ဆောက်နိုင်ဖို့ အရေးပါတဲ့ အထောက်အကူတွေ ဖြစ်ပါတယ်။