ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဗိသုကာပညာရှင်တွေက အိမ်တိုင်းအတွက် လှေကားကို အစကနေ ပြန်တီထွင်မနေပါဘူး။ "ဝိုင်းလှေကား"၊ "L ပုံလှေကား" လို နာမည်ပေးထားတဲ့ ပုံစံတွေ ရှိပြီး နေရာနဲ့ လိုအပ်ချက်အလိုက် ရွေးသုံးကြပါတယ်။ Design pattern ဆိုတာ programmer တွေရဲ့ အဲဒီလို ပုံစံတွေပါ — ပြဿနာ အမျိုးအစားတစ်ခုကို ဖြေရှင်းဖို့ စမ်းသပ်ပြီးသား၊ နာမည်ပေးထားပြီးသား နည်းလမ်းတွေပါ။
Pattern ရဲ့ နာမည်ကို သိထားရင် team ထဲမှာ ပြောဆိုရတာ မြန်ပါတယ်။ "ဒီမှာ Strategy သုံးလိုက်ရအောင်" လို့ ပြောလိုက်တာနဲ့ code တစ်ကြောင်းမှ မရေးရသေးခင် အားလုံး ပုံကို မြင်ကြပါတယ်။ ဒါပေမယ့် pattern က ကိရိယာပါ၊ ပန်းတိုင် မဟုတ်ပါဘူး။
Problem → Why → Solution
ပြဿနာ: shipping နည်းလမ်း အသစ်တစ်ခု ထည့်တိုင်း checkout ကို ပြင်ရတယ်
Online shop တစ်ခုရဲ့ checkout function မှာ shipping ခ တွက်တာကို if/else ဆက်တိုက်နဲ့ ရေးထားပါတယ်။ "same-day" delivery အသစ် ထည့်ချင်တဲ့အခါ အလုပ်လုပ်နေတဲ့ checkout() ကို ဖွင့်ပြင်ရပြီး ဈေးနှုန်းတွက်တာ၊ discount ပေးတာ တွေနဲ့ ရောနေတဲ့ code ထဲမှာ branch အသစ် ထည့်ရပါတယ်။ နည်းလမ်း တစ်ခုချင်းစီကိုလည်း သီးခြား test လုပ်လို့ မရပါဘူး။
function checkout(subtotal, weightKg, method) {
let shipping;
if (method === "standard") {
shipping = 5 + weightKg * 1.5;
} else if (method === "express") {
shipping = 15 + weightKg * 2.5;
} else if (method === "pickup") {
shipping = 0;
} // every new method = another edit to this function
// ...discounts, tax and receipts also live here...
return subtotal + shipping;
}ဘာကြောင့် ဒါက ပြဿနာလဲ
checkout() က "shipping ခ ဘယ်လို တွက်လဲ" ဆိုတဲ့ ပြောင်းလဲနေမယ့် အပိုင်းနဲ့ "order တစ်ခုကို ဘယ်လို ပြီးအောင် လုပ်လဲ" ဆိုတဲ့ တည်ငြိမ်တဲ့ အပိုင်းကို ရောထားပါတယ်။ Strategy pattern က shipping တွက်နည်း တစ်ခုစီကို သီးခြား function အဖြစ် ခွဲထုတ်ပြီး checkout က ရွေးထားတဲ့ function ကို ခေါ်ရုံပဲ ဖြစ်စေပါတယ်။ နည်းလမ်းအသစ်ဆိုတာ object ထဲ entry တစ်ခု ထပ်ထည့်တာပဲ ဖြစ်သွားပါတယ်။
Core concept: ပြောင်းလဲမယ့် အပိုင်းကို သီးခြား ခွဲထုတ်တာ
Design pattern ဆိုတာ ထပ်ခါထပ်ခါ ကြုံရတဲ့ design ပြဿနာအတွက် နာမည်ပေးထားတဲ့ ဖြေရှင်းပုံ ပုံသဏ္ဌာန် တစ်ခုပါ၊ copy-paste ကူးရမယ့် code အတိအကျ မဟုတ်ပါဘူး။ Pattern အများစုရဲ့ အနှစ်သာရက တူပါတယ် — မကြာခဏ ပြောင်းမယ့် အပိုင်းကို တည်ငြိမ်တဲ့ အပိုင်းကနေ ခွဲထုတ်ပြီး နှစ်ခုကြားမှာ ရှင်းလင်းတဲ့ interface ထားတာပါ။ Pattern တွေကို Java လို class-based language တွေအတွက် စတင် ရေးသားခဲ့ကြပေမယ့် JavaScript မှာ function တွေက value ဖြစ်ပြီး closure ရှိလို့ ပုံစံတော်တော်များများက ပိုတိုပြီး ပိုရိုးရှင်းပါတယ်။
| Pattern | ဖြေရှင်းတဲ့ ပြဿနာ | JavaScript မှာ ရေးပုံ |
|---|---|---|
| Module | State ကို ဝှက်ပြီး public API ပဲ ပြချင် | IIFE + closure, ဒါမှမဟုတ် ES module |
| Singleton | Instance တစ်ခုတည်းပဲ ရှိရမယ် | ပထမ ခေါ်မှ ဖန်တီးပြီး သိမ်းထား |
| Factory | Object ဖန်တီးပုံ ရှုပ်ထွေးနေ | createX(options) function |
| Strategy | Algorithm ကို runtime မှာ လဲချင် | Function တွေပါတဲ့ object / map |
| Observer | ဖြစ်ရပ်တစ်ခုကို အများကို အသိပေးချင် | on() / emit() နဲ့ listener list |
Module pattern က closure ကို အသုံးချပါတယ် — IIFE (ချက်ချင်း run တဲ့ function) ထဲမှာ variable တွေ ကြေညာပြီး အပြင်ကို ပြချင်တဲ့ method တွေပါတဲ့ object ကို return ပြန်ပါတယ်။ ယနေ့ ES module ဖိုင်တစ်ခုစီကိုယ်တိုင်က module scope ရှိပြီး တစ်ကြိမ်ပဲ evaluate လုပ်လို့ IIFE မလိုတော့ပေမယ့် code အဟောင်းနဲ့ library တွေမှာ IIFE ပုံစံကို အများကြီး တွေ့ရပါတယ်။ Singleton က instance ကို ပထမဆုံး လိုတဲ့အချိန်မှ ဖန်တီးပြီး နောက်ပိုင်း ခေါ်တိုင်း အဲဒီ instance ကိုပဲ ပြန်ပေးပါတယ်။ Observer ကတော့ ဖြစ်ရပ်တစ်ခု ဖြစ်တဲ့အခါ အသိပေးခံချင်သူတွေကို listener အဖြစ် စာရင်းသွင်းထားပြီး emitter က သူတို့ကို တိုက်ရိုက် မသိဘဲ ခေါ်ပေးပါတယ်။
Pattern တိုင်းမှာ abstraction ကုန်ကျစရိတ် ရှိပါတယ် — ဖိုင်၊ နာမည်၊ indirection အလွှာ ပိုများလာပါတယ်။ ဒါကြောင့် pattern မသုံးခင် "ဒီနေရာမှာ တကယ် ပြောင်းလဲမှု ရှိလား" လို့ မေးပါ။ Shipping နည်းလမ်း တစ်ခုတည်းပဲ ရှိရင် function ရိုးရိုးလေး တစ်ခုက Strategy ထက် ပိုကောင်းပါတယ်။
- ဘာက တကယ် ပြောင်းနေလဲ ရှာ
- အဲဒီ အပိုင်းကို function/object ထဲ ခွဲထုတ်
- တည်ငြိမ်တဲ့ code က interface ကိုပဲ ခေါ်
- Function ရိုးရိုးနဲ့ လုံလောက်လား ပြန်စစ်
ပြဿနာ အရင်လာပြီး pattern က နောက်မှ လိုက်လာရပါမယ်။
အထွေထွေ ပုံစံ
// Module: private state + public API
const Service = (function () {
let privateState = initialValue;
return { publicMethod() { /* uses privateState */ } };
})();
// Singleton: build once, reuse forever
let instance;
function getInstance() {
return instance ?? (instance = createInstance());
}Module pattern က IIFE ကို ချက်ချင်း run ပြီး return ပြန်တဲ့ object ကိုပဲ အပြင်ကို ပေးပါတယ်။ Singleton က instance ကို variable တစ်ခုထဲ cache လုပ်ထားပြီး မရှိသေးမှ ဖန်တီးပါတယ်။
Example: Calculator module နဲ့ Database singleton
Calculator က result ကို closure ထဲ ဝှက်ထားပြီး method တိုင်းက this ကို return ပြန်လို့ .add().add().subtract() လို ဆက်တိုက် ခေါ်လို့ရပါတယ်။ Database က getInstance() ကို နှစ်ကြိမ် ခေါ်ပေမယ့် "Creating the connection" က တစ်ကြိမ်ပဲ ထွက်တာကို သတိထားကြည့်ပါ။
// 1. Module pattern: an IIFE hides state and returns a public API
const Calculator = (function () {
let result = 0; // private
return {
add(n) {
result += n;
return this; // returning the object enables chaining
},
subtract(n) {
result -= n;
return this;
},
getResult() {
return result;
},
reset() {
result = 0;
return this;
},
};
})();
Calculator.add(10).add(5).subtract(3);
console.log(`Result: ${Calculator.getResult()}`);
console.log(`After reset: ${Calculator.reset().add(2).getResult()}`);
// 2. Singleton pattern: create the instance once, lazily
const Database = (function () {
let instance;
function createInstance() {
console.log("Creating the connection (runs once)");
return { connection: "MySQL Connection" };
}
return {
getInstance() {
if (!instance) {
instance = createInstance();
}
return instance;
},
};
})();
const db1 = Database.getInstance();
const db2 = Database.getInstance();
console.log(db1 === db2); // true (same instance)Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 2-3
let result = 0; // privateIIFE ထဲမှာ ကြေညာထားလို့ result ကို method လေးခုကပဲ မြင်ရပါတယ်။ Calculator.result လို့ ဖတ်ရင် undefined ရပါတယ်။
- Line 8
return this; // returning the object enables chainingCalculator.add(10) လို့ method အဖြစ် ခေါ်တဲ့အခါ this က Calculator object ပါ။ အဲဒါကို return ပြန်လို့ နောက်ထပ် .add(5) ကို ဆက်ခေါ်လို့ရပါတယ်။ Arrow function နဲ့ method ရေးရင် this မှားသွားပြီး chaining ပျက်ပါမယ်။
- Line 22
})();Function ကို ကြေညာပြီးတာနဲ့ () နဲ့ ချက်ချင်း run ပါတယ်။ Calculator ထဲ ရောက်သွားတာက function မဟုတ်ဘဲ return ပြန်လာတဲ့ object ပါ။
- Lines 39-41
if (!instance) {instance မရှိသေးမှ createInstance() ကို ခေါ်ပါတယ်။ ဒုတိယ ခေါ်တဲ့အခါ instance ရှိပြီးသားမို့ ဒီ block ကို ကျော်ပြီး သိမ်းထားတဲ့ object ကို ပြန်ပေးပါတယ်။ ဒါကို lazy initialization လို့ ခေါ်ပါတယ်။
- Line 49
console.log(db1 === db2); // true (same instance)=== က object နှစ်ခုကို reference နဲ့ နှိုင်းယှဉ်ပါတယ်။ true ရတာက db1 နဲ့ db2 က object တစ်ခုတည်းကို ညွှန်နေကြောင်း သက်သေပါ။
Result: 12
After reset: 2
Creating the connection (runs once)
trueStrategy pattern: shipping တွက်နည်းတွေကို လဲလှယ်ရတာ
Problem section က checkout ကို Strategy pattern နဲ့ ပြန်ရေးကြည့်ရအောင်။ Shipping တွက်နည်း တစ်ခုစီက object ထဲက function တစ်ခုစီ ဖြစ်ပြီး checkout က method နာမည်နဲ့ ရှာပြီး ခေါ်ရုံပါပဲ။ "same-day" အသစ်ထည့်ချင်ရင် checkout ကို မထိဘဲ shippingStrategies ထဲ line တစ်ကြောင်း ထပ်ထည့်ရုံပါ။ နည်းလမ်း တစ်ခုချင်းစီကိုလည်း shippingStrategies.express(2) လို့ သီးခြား test လုပ်လို့ ရပါတယ်။
const shippingStrategies = {
standard: (weightKg) => 5 + weightKg * 1.5,
express: (weightKg) => 15 + weightKg * 2.5,
pickup: () => 0,
};
function checkout(subtotal, weightKg, method) {
const strategy = shippingStrategies[method];
if (!strategy) throw new Error(`Unknown shipping method: ${method}`);
const total = subtotal + strategy(weightKg);
return `${method}: ${total.toFixed(2)}`;
}
console.log(checkout(40, 2, "standard")); // standard: 48.00
console.log(checkout(40, 2, "express")); // express: 60.00
console.log(checkout(40, 2, "pickup")); // pickup: 40.00Factory pattern: ဖန်တီးပုံ စည်းမျဉ်းတွေကို တစ်နေရာတည်းမှာ ထားတာ
User object တွေကို code နေရာ ဆယ်နေရာမှာ ကိုယ်တိုင် { name, role, permissions } လို့ ဆောက်နေရင် admin ရဲ့ permission ပြောင်းတဲ့အခါ နေရာ ဆယ်နေရာလုံး ပြင်ရပါတယ်။ Factory function တစ်ခုက ဖန်တီးပုံ စည်းမျဉ်းကို တစ်နေရာတည်းမှာ စုထားပြီး ခေါ်တဲ့သူက "ဘာလိုချင်လဲ" ကိုပဲ ပြောရပါတယ်၊ "ဘယ်လို ဆောက်ရမလဲ" ကို သိစရာ မလိုပါဘူး။
const PERMISSIONS = {
admin: ["read", "write", "delete"],
editor: ["read", "write"],
viewer: ["read"],
};
function createUser(name, role = "viewer") {
const permissions = PERMISSIONS[role];
if (!permissions) throw new Error(`Unknown role: ${role}`);
return { name, role, permissions: [...permissions] };
}
console.log(createUser("Su", "admin").permissions.join(",")); // read,write,delete
console.log(createUser("Ko Ko").role); // viewerလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
ဒီ pattern တွေကို နာမည်မတပ်ဘဲ နေရာတိုင်းမှာ တွေ့ရပါတယ်။ Browser ရဲ့ addEventListener နဲ့ Node.js ရဲ့ EventEmitter က Observer pattern ပါ၊ Redux လို state library တွေရဲ့ subscribe() ကလည်း ဒီပုံစံပါပဲ။ Passport.js လို authentication library တွေက login နည်းလမ်း (Google, GitHub, password) တစ်ခုစီကို "strategy" လို့ တိုက်ရိုက် နာမည်ပေးထားပြီး Array ရဲ့ sort() ကို comparison function ပို့တာကိုယ်တိုင်က Strategy ပါ။
Database connection pool၊ logger၊ app configuration တွေကို application တစ်ခုလုံးမှာ instance တစ်ခုတည်း မျှသုံးဖို့ ES module ကနေ export လုပ်ကြပါတယ် — module တစ်ခုကို တစ်ကြိမ်ပဲ evaluate လုပ်လို့ ဒါက တကယ်တော့ Singleton ပါ။ Test တွေမှာ အစစ်အမှန် database အစား fake တစ်ခု အစားထိုးဖို့ factory function တွေ (createDb(config)) ကို သုံးကြပါတယ်။ Pattern တွေကို သိထားရင် library documentation ကို ဖတ်တဲ့အခါ ဘာကြောင့် ဒီလို design လုပ်ထားလဲ ချက်ချင်း နားလည်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. နာမည်ရှိလို့ pattern ကို သုံးတာ
Variation မရှိတဲ့ နေရာမှာ Factory, Strategy အလွှာတွေ ထည့်ရင် code ပိုရှည်ပြီး ဖတ်ရခက်ရုံပါပဲ။ Pattern ထည့်ပြီးနောက် ပြောင်းလဲမှု ဒါမှမဟုတ် test ရေးရတာ တကယ် ပိုလွယ်လာလား စစ်ပါ၊ မလွယ်လာရင် function ရိုးရိုးကို ပြန်သုံးပါ။
2. Singleton ကို ဖုံးကွယ်ထားတဲ့ global state အဖြစ် သုံးတာ
Singleton ထဲမှာ ပြင်လို့ရတဲ့ data ထားရင် test တစ်ခုက ပြောင်းလိုက်တဲ့ value က နောက် test ဆီ ပါသွားပြီး test အစဉ်ပြောင်းရင် fail ဖြစ်ပါတယ်။ Singleton ကို connection လို stateless resource တွေအတွက်ပဲ သုံးပြီး state ကို parameter နဲ့ ပို့ပါ။
3. Chaining method ကို arrow function နဲ့ ရေးတာ
add: (n) => { result += n; return this; } လို့ ရေးရင် arrow function မှာ ကိုယ်ပိုင် this မရှိလို့ this က Calculator မဟုတ်တော့ပါဘူး (module ထဲမှာ undefined)။ Calculator.add(1).add(2) က TypeError ဖြစ်ပါတယ်။ Chain လုပ်မယ့် method တွေကို add(n) { ... } ပုံစံနဲ့ ရေးပါ။
4. Observer listener ကို ဘယ်တော့မှ မဖြုတ်တာ
Component တစ်ခု ပျက်သွားပေမယ့် listener က emitter ထဲမှာ ကျန်နေရင် memory မလွတ်ဘဲ မရှိတော့တဲ့ component ကို ဆက်ခေါ်နေပါတယ်။ on() က unsubscribe function ပြန်ပေးအောင် ရေးပြီး မလိုတော့ရင် ခေါ်ပါ။
Mini challenge
Mini challenge: Observer event emitter ရေးကြည့်ပါ
Order ဖြစ်ရပ်တွေကို အသိပေးဖို့ createEmitter() factory ကို ရေးပါ။ Listener တွေ စာရင်းသွင်းတာ၊ ဖြုတ်တာ၊ event ပို့တာ လုပ်နိုင်ရပါမယ်။
- on(event, handler) က listener ကို ထည့်ပြီး ဖြုတ်ဖို့ unsubscribe function ကို return ပြန်ရမယ်
- emit(event, data) က အဲဒီ event ရဲ့ listener အားလုံးကို data နဲ့ ခေါ်ပြီး ခေါ်ခဲ့တဲ့ အရေအတွက်ကို return ပြန်ရမယ်
- Listener မရှိတဲ့ event ကို emit လုပ်ရင် error မတက်ဘဲ 0 ပြန်ရမယ်
- Listener list ကို closure ထဲ ဝှက်ထားရမယ်
function createEmitter() {
// TODO: a private Map from event name to a Set of handlers
return {
on(event, handler) {
// TODO: add handler; return a function that removes it
},
emit(event, data) {
// TODO: call each handler with data; return how many ran (0 if none)
},
};
}
// TODO: subscribe an "email" and a "stock" listener to "placed"
// TODO: emit "placed", unsubscribe email, emit again, emit "cancelled"ရလဒ်: Unsubscribe လုပ်ပြီးနောက် email listener မခေါ်တော့ရပါဘူး: Email: order #101 confirmed Stock: reserve 2 item(s) notified 2 listeners Stock: reserve 1 item(s) notified 1 listeners notified 0 listeners
အရိပ်အမြွက် ကြည့်ရန်
listeners.get(event) က Set တစ်ခု ပြန်ပေးပြီး handler ကို ဖြုတ်ဖို့ set.delete(handler) ကို သုံးပါ။ on() ထဲမှာ () => listeners.get(event).delete(handler) ကို return ပြန်ရင် closure က event နဲ့ handler ကို မှတ်ထားပေးပါတယ်။
အဖြေ ကြည့်ရန်
function createEmitter() {
const listeners = new Map(); // event name -> Set of handlers
return {
on(event, handler) {
if (!listeners.has(event)) listeners.set(event, new Set());
listeners.get(event).add(handler);
return () => listeners.get(event).delete(handler);
},
emit(event, data) {
const handlers = listeners.get(event);
if (!handlers) return 0;
for (const handler of handlers) handler(data);
return handlers.size;
},
};
}
const orders = createEmitter();
const stopEmail = orders.on("placed", (o) => {
console.log(`Email: order #${o.id} confirmed`);
});
orders.on("placed", (o) => console.log(`Stock: reserve ${o.qty} item(s)`));
console.log(`notified ${orders.emit("placed", { id: 101, qty: 2 })} listeners`);
stopEmail();
console.log(`notified ${orders.emit("placed", { id: 102, qty: 1 })} listeners`);
console.log(`notified ${orders.emit("cancelled", { id: 101 })} listeners`);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Design pattern ဆိုတာ ထပ်ခါထပ်ခါ ကြုံရတဲ့ ပြဿနာအတွက် နာမည်ပေးထားတဲ့ ဖြေရှင်းပုံ ပုံသဏ္ဌာန်ဖြစ်ပြီး ပြောင်းလဲမယ့် အပိုင်းကို သီးခြား ခွဲထုတ်ပါတယ်။
- Module pattern က closure နဲ့ state ကို ဝှက်ပြီး Singleton က instance တစ်ခုတည်းကို lazy ဖန်တီးပါတယ်။
- Strategy က algorithm ကို လဲလှယ်ခွင့်ပေးပြီး Observer က emitter နဲ့ listener တွေကြား တိုက်ရိုက် ချိတ်ဆက်မှုကို လျှော့ပေးပါတယ်။
- Pattern တိုင်းမှာ abstraction ကုန်ကျစရိတ် ရှိလို့ variation တကယ်ရှိမှ သုံးပါ၊ မရှိရင် function ရိုးရိုးကို ရွေးပါ။
MDN — JavaScript Guide — MDN Web Docs