Showing posts with label design patterns. Show all posts
Showing posts with label design patterns. Show all posts

Sunday, December 18, 2011

Singleton

Singleton

Design Pattern စာအုပ္ေတြထဲမွာေရာ source code ေတြမွာပါ ေတြ႕ဖူးပါတယ္။ ဘာအတြက္သံုးလဲအနည္းငယ္ေလာက္ပဲသိပါတယ္။ Java မွာသိစရာနည္းနည္းမ်ားတယ္ထင္ပါတယ္။ နည္းနည္းသိတဲ႔အသိဟာအႏၲရာယ္မ်ားတယ္ဆိုေပေသာ္ျငားလည္း အင္တာဗ်ဴးေျဖဖို႕ၾကေတာ႔လည္း အသံုး၀င္သား။

အၾကမ္းဖ်င္းေလာက္ပဲေျပာမွာဆုိေတာ႔ wiki ရဲ႕ဖြင္႔ဆိုခ်က္ကိုပဲ ကိုးကားလုိက္ပါတယ္။ အိမ္ေရာက္ရင္ ထပ္ကိုးကားစရာမ်ား ကံေကာင္းေထာက္မျပီးေတြ႕ခဲ႔ရင္ ေနာက္တစ္ေခါက္ ထပ္ကိုးကားပါဦးမယ္။ singleton ဟာ class ေတြ instantiation လုပ္တာကို ကန္႕သတ္ဖုိ႕သံုးပါသတဲ႔။ system တစ္ခုလံုးမွာ ဒီ Object တစ္ခုပဲသံုးမယ္ဆုိျပီး ဒီဇုိင္းဆင္ရင္ သံုးတယ္ေပါ႔ခမ်ာ။ အထူးသျဖင္႔ သူ႕ကို Instance ဘယ္ႏွစ္ခုအထိပဲသံုးမလဲဆိုတာမ်ိဳးကန္႕သတ္ခ်င္ရင္ အဆင္ေျပတယ္ေပါ႔ဗ်ာ။

Anti-pattern လို႕လည္းဆိုၾကသဗ်။ Anti-pattern ဆိုတာက သံုးသာသံုးေနတယ္။ အက်ိဳးသိပ္မရိွဘူး။ သူ႕ဘာသာသူ ရြာလည္ေနတယ္လို႕ဆိုလုိတယ္။ အသံုးမတတ္တာေၾကာင္႔လည္းျဖစ္ႏိုင္သလို၊ မလိုအပ္ဘဲသိပ္သံုးလြန္းတာေၾကာင္႔လည္းျဖစ္မယ္။ အင္တာဗ်ဴးရင္ေတာ႔ လႊတ္သာေျပာေပး။ ဒီလုိကိစၥေတြသိတယ္ဆိုတာ ေတာ္ရံုမဟုတ္ဘူး ထင္လာတဲ႔အထိေပါ႔။

Java မွာေတာ႔ ပ်င္းပ်င္းရိရိ singleton နဲ႕ မပ်င္းတပ်င္းဆိုျပီးႏွစ္ခုရိွတယ္ဗ်။

ပ်င္းတယ္ဆိုတာကေတာ႔ ခိုင္းတုိင္းမလုပ္ဘူးေပါ႔ခမ်ာ။ တျခားလူလုပ္ေပးမလား ထၾကည္႕တာေပါ႔။ ရိွရင္ ဒီေကာင္႔ကိုခုိင္းလုိက္ပါ။ ကြ်န္ေတာ္ဆက္အိပ္ေနခ်င္လို႕ဆိုတာမ်ိဳးကို ပ်င္းတယ္ေခၚတာေပါ႔။
ဒါေလးေတြေတာ႔ၾကည္႕ထားေပါ႔ဗ်ာ။ ေလေတြခ်ည္း စြတ္ေပါေနေတာ႔လည္း တကယ္မေရးတတ္ဘူးထင္ျပီးေမးၾကည္႕ရင္ ခက္ေနမယ္။ ခ်ေရးျပႏိုင္ေတာ႔ တစ္ပန္းခြဲေလာက္သာတာေပါ႔။

public class MySingleton { 
 private static MySingleton _instance = 
  new MySingleton(); 

 private MySingleton() { 
  // construct object . . . 
 } 

 public static MySingleton getInstance() { 
  return _instance; 
 } 

 // Remainder of class definition . . .

အဲဒီမွာၾကည္႕ရင္ MySingleton ၾကီးကို ေခၚလိုက္တာနဲ႕ instantiate လုပ္ျပီး memory ေပၚတင္ထားလုိက္တယ္။ static ပါတဲ႔အတြက္ သူက အလားတူေကာင္ေတြနဲ႕အတူရိွေနမရိွေန memory တစ္ခြင္မွာသူတစ္ေကာင္သာရိွေနဖုိ႕အတြက္ လုပ္ထားတယ္ေပါ႔ဗ်ာ။ ေခၚလိုက္တုိင္း တစ္ေကာင္ထြက္လာမယ္။ရိွေနတဲ႔ေကာင္ေသမယ္။ ျခေသၤ႔မင္းဆိုတာ တစ္ေကာင္တည္းပဲ မန္မိုရီတစ္ဂူတည္း ႏွစ္ေကာင္မေအာင္းခ်င္ဘူးေပါ႔ဗ်ာ။
ငပ်င္း singleton
public class MySingleton { 
 private static MySingleton _instance; 

 private MySingleton() { 
  // construct object . . . 
 } 

 // For lazy initialization 
 public static synchronized MySingleton getInstance() { 
  if (_instance==null) { 
   _instance = new MySingleton(); 
  } 
  return _instance; 
 } 
  // Remainder of class definition . . . 
} 

Java ကို မခ်စ္ၾကတာ ဒီအေၾကာင္းေတြေၾကာင္႔ထင္တယ္။ ဘာေတြေလွ်ာက္ေရးထားမွန္းမသိတာမ်ားလုိ႕ျဖစ္မယ္။ synchronized ဆိုတာကေတာ႔ ဒီ method ကို တစ္ေယာက္သံုးေနရင္ေနာက္တစ္ေယာက္ကေစာင္႔ေနေပေတာ႔တဲ႔။ lock လုပ္တဲ႔သေဘာပါ။ အင္မတန္အႏၲရာယ္မ်ားပါတယ္။ ပထမတစ္ေယာက္က သူပဲ ထာ၀ရသံုးေနမယ္ဆိုရင္ ေနာက္တစ္ေယာက္က ေနာက္ဘ၀မွပဲသံုးရပါမယ္။ token ဆိုတဲ႔ network topology လို သူကပဲ အပိုင္စီးထားလိုက္ရင္က်န္တဲ႔သူေတြ ငုတ္တုတ္ေမ႔သြားသလိုေပါ႔ခမ်ာ။ အခုန static method ဆိုတာကလည္း နည္းနည္းပြားျပီးျပီဆိုေတာ႔ထပ္မပြားေတာ႔ပါဘူး။ ဒီ တစ္ေခါက္ေတာ႔ စစခ်င္း new နဲ႕ instantiate မလုပ္တာကိုေတြ႕ပါလိမ္႔မယ္။ if(instance==null) ဆိုတာကေတာ႔ ပ်င္းလို႕ အရင္အေကာင္ရိွေသးရင္ အဲဒီအေကာင္ကို ဆက္သံုးၾကပါ။ ငါ႔ကို မေခၚပါနဲ႕ဦးဆိုတဲ႔သေဘာပါပဲ။ တကယ္လုိ႕ ေရွ႕ကေကာင္ၾကြျပီဆိုမွ သူ႕ကို အသစ္ဖန္တီးျပီး သံုးလုိ႕ရမယ္ဆိုတဲ႔သေဘာပါပဲ။

Bill Pugh ဆိုတဲ႔ researcher ကလည္း ကဲတယ္ခင္ဗ်။ အခုနက ကြ်န္ေတာ္ေျပာတဲ႔ synchronized ၾကီးက ဒုကၡေပးတယ္ဆိုတာသူလည္း ေပါက္တယ္တဲ႔ခင္ဗ်။ သူကေတာ႔ researcher ပီပီ အခ်ိန္ေတြပိုေနေတာ႔ေလွ်ာက္ေရးပံုရပါတယ္။


public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }
 
        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 

                public static final Singleton instance = new Singleton();
        }
 
        public static Singleton getInstance() {

                return SingletonHolder.instance;
        }
}


static final Singleton ပါခဗ်။ final keyword ကလည္း အင္မတန္က်ယ္တဲ႔ေခါင္းစဥ္တစ္ခုျဖစ္ပါတယ္။ အဲဒီလိုေရးလုိက္တာ သရက္ေစ႔ျဖစ္တယ္တဲ႔ခင္ဗ်။ thread-safe ဆုိတာ အခုန synchronized သံုးတာမ်ိဳး၊ တစ္ေယာက္သံုးေနတုန္း ေနာက္တစ္ေယာက္က ၀င္မရွဳပ္ေအာင္လုပ္တာမ်ိဳးကိုေခၚတယ္လို႕ အၾကမ္းမွတ္ထားလုိက္ရင္ရေလာက္ပါျပီ။

Regards,
Zero

အခုအခ်ိန္ကစျပီး ref: မ်ားကို လူေလးစားေလာက္ေအာင္ထည္႕ေတာ္မူသြားပါေတာ႔မယ္။ ေနာက္တစ္ခါ ျပန္ဖတ္ရင္ Bookmarks ကေန မရွာခ်င္ေတာ႔ဘူး။

ref:
http://java.sun.com/developer/technicalArticles/Programming/singletons/
http://en.wikipedia.org/wiki/Singleton_pattern
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
ဒီၤေအာက္ဆံုးကလင္႔ခ္ကေတာ႔ ဂန္းေအာ႔ဖ္ဖုိးကို ေနာက္ေျပာင္တယ္ခင္ဗ်။
http://www.infoq.com/news/2007/07/GoFCriticism

Wednesday, October 12, 2011

Design Patterns

Design Patterns

Today, I brush up on design patterns.
Here is the link for you to read and to do so :

Sometimes, we knew it from the source code though we didn't see this is which design pattern. There will be a B=I=G questionmark about design patterns, do we need to know Design patterns? The question is as simple as that. But the answer will be not so straightforward. (Somebody may think the opposite of straightforward will be wavybackward.) Indeed, there is no such thing design patterns are the musts, all developer should know design patterns as his sweetheart's birthday. In the real world solution, we may use a lot of design patterns according to the framework's support or according to the structure of the architect/tech lead, who take most of the share from the projects. Anyway, the answer will absolutely depends on you. Do you want to know clearly and vividly about the design pattern though you may not use (except in your interviews)? If you give a mere yes or a small nod or shrug your shoulders, you should learn about these design patterns.

Unfortunately, there is no shortcut in reading a book. If you are equally lazy as me, try to read regular hour(/s) everyday instead of reading a tech-book per day as geeks do. There are some patterns you should know as a good developer. These patterns are very useful if you really want to write quality codes.

For design patterns, I am reading Head First Design Patterns - O'Reilly Media.
One of my friends who suggests to read this book says it is easier to understand than the book written by Gang of 4. Of course all the head first series are fun to read.

We should be professional at least. Though we may not use a lot of Design Pattern in current environments, we should know proper ways of coding and basic patterns.

Most of us learnt design patterns @ school. After we had got some experiences from work, we should now digest our knowledge from school and fill the gap in our understanding of the software development.

There are two ways to prevent the reinventing the wheel; the first is not to do anything, sit tight and do nothing special, the second is to learn the proven solutions that conquer the same old barriers before.

Sometimes, working 2 hard can lead you 2 B mediocre. Read in advance something you may need to know in your life/work before you encounter the big problems.

Thanks for reading my post.

I will try to post some design patterns summaries soon.

Regards,
Zero/Zero