Factory Pattern in React

Aug 15, 2023

Server side language တွေမှာ အသုံးများတဲ့ factory pattern ကို react မှာ real world စံစံ ဘယ်လိုသုံးကြမလဲ။ တစ်ခုတော့ ရှိတယ် typescript နဲ့တော့ ရေးရပါလိမ့်မယ်။

blogging

React ရေးတဲ့ ခါမှာ လူတိုင်း ternary operator ကို အမြဲ လိုလို သုံးကြမယ် ထင်ပါတယ်။ ဥပမာ - user က admin လား ၊ ရိုးရိုး user လား ၊ department အလိုက်လား ဆိုပြီး dashboard ကိုယ်စီ ရှိကြတတ်ပါတယ်။ များသောအားဖြင့် ဒီလို အခြေအနေ မှာ ternary operator နဲ့ user role ကို စစ်ပြီး dashboard view ကို ပြလိမ့်ရှိပါတယ်။ ပြသနာက dashboard အသစ်တစ်ခု ရှိတိုင်း ပထမ ဆုံး အဆင့် user မှာ role အသစ် ပြန်ထည့်ရမယ် ဒုတိယအဆင့် နဂိုက ရေးထားတဲ့ dashboard page မှာ user role အသစ်စစ်ပြီးတော့ dashboard component အသစ်ကို ternary operator မှာ ရှည်လမျေား ရေးရပါတယ်။ အဲ့ချိန်မှာ code က readability ကျသွားရော။

အဲ့ဒီအစား အစိမ်းရောင် အမှန်ခြစ်ပြထားသည့် code အတိုင်း dashboard variable တစ်ခုကို enum အနေဖြင့် role နဲ့ ဆက်စပ်တဲ့ dashboard component ကို key value pair တစ်ခု တည်ဆောက်လိုက်မယ်။ ပြီးလျှင် UserProps interface အတွင်း user object type ကို role နဲ့တကွ define လုပ်လိုက်မယ်။ role ၏ type ကတော့ keyof typeof ကို သုံး၍ dashboard variable ကနေ ‘normal’ | ‘admin’ ဆို ပြီး define လုပ်လိုက်ခြင်းဖြစ်တယ်။ ဆိုလိုတာ role ဆိုသည့် key သည် normal or admin နှစ်ခုဘဲ value ပေးလို့ရမယ်ပေါ့။ ယခု အဆင့်ထိက factory တည်ဆောက်လိုက်တာပါဘဲ။

အဲ့ဒီ factory ကို အောက်က Dashboard ဆိုသည့် component မှာ UserDashboard က dashboard ဆိုသည့် object ကို ခေါ်ပြီးတော့ user ဆိုသည့် prop ကနေ role ကိုယူ၍ key နဲ့ကိုက်တဲ့ component ကို ခေါ်ယူတယ်။ ternary operator အရှည်ကြီး ရေးစရာမလိုတော့ ဘဲ Dashboard component မှာ UserDashboard တစ်ခုသာ တွေ့ရတော့မှာပါ။ sale dashboard အသစ် တစ်ခု ထပ်တည့်မယ် ဆိုပါဆို့ dashboard enum variable မှာsale : SaleDashboard လို့ ထပ်ရေးလိုက်ရုံနဲ့ ပြီး သွားပါပြီ။ ကျန်တာ ပြောင်းစရာမလိုတော့ဘူး။

ထိုနည်းဖြင့် react code base ကို readable နဲ့ maintainable ဖြစ်ဖြစ် ရေးနိုင်ပါတယ်။

Related Articles

Database Sharding

Apr 15, 2026

ကျွန်တော် တို့ project စလုပ်တဲ့ချိန်မှာ database server instance တစ်ခုတည်းနဲ့တင် လုံလောက်ပါတယ် (ဥပမာ - ကိုယ့် company အတွက် internal tools တွေ ၊ hobby project တွေပေါ့) ။ ကြာလာတာနဲ့ data size က ကြီးလာတော့ performance ကျလို့ cache layer (e.g Redis) ထည့်ပြီး fix လုပ်နိုင်တယ်။ ဒါပင်မဲ့ database server instance တစ်ခုကိုဘဲသုံးနေရင် (vertical scaling လုပ်မယ်ဆိုရင်) အရင် blog က ပြောတဲ့ အတိုင်း hardware limit ကိုထိလာတယ်။ ဥပမာအနေနဲ့ data intensive ဖြစ်တဲ့ application တွေပြောရရင် bus ticket application တွေ၊ ordering system တွေပါ။ ...

ထို့ကြောင့် data size ကြီးလာပြီး request တွေများလာတာနဲ့ အမြဲ horizontal scaling strategies တွေကို သုံးလာရတယ်။ ...

အသုံးများတဲ့ Database Horizontal scaling strategies နှစ်မျိုးရှိပါတယ်။ ...

၁။ Sharding ...

Write (data ကို အပြောင်းအလဲလုပ်တာ) နဲ့ read (data ဖတ်တာ) ကို servers တွေက တစ်ခုနဲ့တစ်ခုပေါ်မမှီခိုဘဲ request traffic တွေကို ဖြန့်ဝေတာ (distribute) ...

၂။ Read replicas ...

Write ကို primary server တစ်ခုကနေ handle လုပ်ပြီး read operation ကို replica လို့ခေါ်တဲ့primary database ရဲ့ copies အများကြီးတွေစီကို ဖြန့်ဝေပေးတာ။ ...

ဒီမှာတော့ database Sharding အကြောင်းရှင်းပြသွားပါမယ်။ ...

ခုနက scaling ပြသနာနဲ့ ကြည့်ရအောင်။ bus ticket application ကို သုံးလာတဲ့သူများလာတယ်ဆိုပါစို့။ data တွေက database တစ်ခုမှာဘဲ စုပြီးရှိလာရင်း traffic များလာတာနဲ့အမျှ query time တွေကြာလာတယ်။ data storage တွေပြည့်လာတယ်။ vertical scaling approach နဲ့ ဖြေရှင်းနိုင်ပင်မဲ့ အပေါ်ကပြောတဲ့ ပြသနာတွေရှိတယ်။ အဲ့ဒီကြီးမားနေတဲ့ database တစ်ခုလုံးကို ခွဲခြမ်းစိတ်ဖြာပြီး ထိန်းသိမ်းရလွယ်တဲ့ အစိတ်အပိုင်းတွေကို shard လို့ခေါ်တယ်။ shard တစ်ခုချင်းစီက database server instance အသီးသီးမှာရှိတယ်။ တစ်နေရာမှာဘဲ မစုနေလို့ workload တွေကို ကောင်းကောင်းဖြန့်ဝေနိုင်တယ်။ ...

ဘယ်လိုခွဲခြမ်းစိတ်ဖြာ (sharding) တာလဲ? ...

Data တွေဟာ shard key အလိုက် group လုပ်ပြီးခွဲတယ်။ shard key တွေက ခင်ဗျားရဲ့ database ထဲက column တစ်ခု ဒါမှ မဟုတ် တစ်ထက်ပိုပြီး ပေါ်မူတည်နေတယ်။ ဥပမာ - ticket id , user id , order number တွေ။ ဒါမှ မဟုတ် approach အရ ဘယ် column မှ မမူတည်ဘဲ unique generated key တွေနဲ့လည်း သုံးလို့ရတယ်။ အဲ့ဒီ shard key တွေကို database တစ်ခုထဲမှာ သိမ်းပြီး data request လာတဲ့ချိန် routing လုပ်တဲ့နေရာမှာသုံးတယ်။ routing ကို load balancer ဒါမှမဟုတ် proxy တစ်ခုကနေ shard key အရသက်ဆိုင်တဲ့ shard (database server) စီကို direct လုပ်ပေးတယ်။ ဒါမဲ့ မဟုတ် GCP documentation အရ multi-player game တို့ high performance real time application (e.g trading system) တွေမှာဆိုရင် application ရဲ့ code မှာတင် shard key ကိုဆုံးဖြတ်တယ်။ ...

လူသိများပြီး အသုံးများတဲ့ sharding နည်းလမ်းတစ်ခုစီကိုပုံနှင့်တကွဖတ်နိုင်ပါတယ်။ ...

Immutable vs Mutable Infrastructure

Mar 29, 2026

Immutable infrastructure ကို ဘယ်အချိန်မှာလိုအပ်လဲ။ ...

Vertical vs Horizontal Scaling

Apr 5, 2026

အရင် blog အဆက် ...

Applications မှာ သုံးလာတဲ့သူများလာတာနဲ့အမြဲ scale လုပ်ဖို့ လိုအပ်လာတယ်။ Scaling လုပ်ရင်ဘာကောင်းမှုတွေရှိလဲ။ ၁။ system ရဲ့ reliability နဲ့ availability ကိုမြင့်စေတယ်။ ၂။ response လုပ်တဲ့ခါ performance ကောင်းစေတယ်။ ၃။ storage နဲ့ data များပြားလာမှုကို အထောက်အကူလုပ်ပေးတယ်။ ...