August 3, 2012

Generics

Generics ဆိုသည်မှာ


Generics ဆိုသည်မှာ Generics Programming ဟုအမည်ရသော ပရိုဂရမ်ရေးသားနည်း၏ အမည်မှ ရယူထားသော အမည် တစ်ခုဖြစ်ပါသည်။ ဆိုရမည် ဆိုလျှင် Generic Programming သည် Object Oriented Programming နည်းနှင့် ပတ်သက်ခြင်း မရှိသော သီးခြားနည်းပညာ တစ်ခု ဖြစ်ပါသည်။ Generics Programming တွင် ပုံစံ (Type) သတ်မှတ်ချက်များနှင့် ချုပ်နှောင်ထားခြင်း မရှိပဲ၊ Type များအား Parameter အနေဖြင့် အသုံးပြုနိုင်ပါသည်။

Java ဘာသာရပ်တွင် Generics အား အသုံးများသည်မှာ Collections Framework တွင် ဖြစ်၏။ Java 5 (Tiger) အရောက်တွင် Javaတွင်လည်း Generics ကိုထည့်သွင်း အသုံးပြုခဲ့ပြီး၊ Java 7 အား အသုံးပြုနေသည့် ယနေ့ အချိန်ဆိုပါက Java အားလေ့လာသူများသည် Generics အကြောင်းအား လေ့လာပြီး၊ သိရှိပြီး ဖြစ်နေပါလိမ့်မည်။ ဘာကြောင့် Generics အား အသုံးပြုရန် လိုအပ်ခဲ့သနည်း၊ Generics အား အသုံးပြုခြင်း၏ အကျိုးကျေးဇူးမှာ အဘယ်နည်း အစရှိသည်များအား စကားလုံးဖြင့် ရှင်းပြနေသည်ထက် နမှုနာ ကုဒ်များအား ရေးသားကြည့်သည်က ပို၍ မြန်ပါလိမ့်မည်။

NonGenericsSample.java
package com.mmju.java.se.generics;

import java.util.LinkedList;
import java.util.List;

public class NonGenricsSample {

 public static void main(String[] args) {
     List list = new LinkedList();
     
     for(String s : args) {
      list.add(s);
     }
     
     for(int i=0; i<list.size(); i++) {
      String s = (String) list.get(i);
      System.out.println(s);
     }
      
    }
}
List အော့ဘဂျက်သည် မည်သည့် အော့ဘဂျက်ကိုမဆို ကော်လက်ရှင် အနေဖြင့် အသုံးပြုနိုင်သောကြောင့်၊ ရုတ်တရက်ကြည့်လျှင် အစဉ်ပြေသလို ဖြစ်သော်လည်း၊ မည်သည့် အော့ဘဂျက်ကိုသာ အသုံးပြုမည်ဟု သတ်မှတ်၍ မရနိုင်ခြင်း၊ တဖန် List#get ဖြင့် သိမ်းဆည်းထားသော အော့ဘဂျက်အား ထုတ်ယူရာတွင် Cast လုပ်ရန် လိုအပ်ခြင်း အစရှိသည့် အခက်အခဲများအား ဖြစ်ပွားစေနိုင်ပါသည်။

GenericsSample.java
package com.mmju.java.se.generics;

import java.util.LinkedList;
import java.util.List;

public class GenricsSample {

 public static void main(String[] args) {
     List<String> list = new LinkedList<>();
     
     for(String s : args) {
      list.add(s);
     }
     
     for(int i=0; i<list.size(); i++) {
      String s = list.get(i);
      System.out.println(s);
     }
      
    }
}
Generics အား အသုံးပြုသည့်အခါတွင် <> အတွင်းတွင်၊ ကော်လက်ရှင် အတွင်းတွင် အသုံးပြုနိုင်သည့် ပုံစံအား String ဟု သတ်မှတ်နိုင်ပါသည်။ ဤနည်းအားဖြစ် ထို List အတွင်းတွင် String မှ အပါး ထည့်သွင်း အသုံးပြု၍ မရနိုင်ပေ။ တဖန် List#get ဖြင့်ကော်လက်ရှင် အတွင်းမှ အော့ဘဂျက်များအား ထုတ်ယူရာတွင်လည်း Cast လုပ်စရာ မလိုအပ်တော့ပေ။


Generics Class နှင့် Interface များအား ရေးသားခြင်း

ရှင်းပါသည်။ Generics အားအသုံးပြုထားသော ကလပ်စ်များအား Generics Class ဟု၎င်း၊ အင်တာဖေစ်များအား Generics Interface ဟု၎င်း ခေါ်ဆိုပါသည်။ ဦးစွာ ကျွှန်တော်တို့ Generics Interface ၏ ရေးသားပုံကို လေ့လာကြည့်ပါမည်။ ပြီးခဲ့သော နမှုနာတွင် အသုံးပြုခဲ့သည့် Listအင်တာဖေစ် အား လေ့လာကြည့်ပါမည်။

List.java
public interface List<E> extends Collection<E> {
 
 public boolean add(E object);
 
 public E get(int index);

အထပ်ပါ ကုဒ်များသည် java.util.List ၏ တစ်စိတ်တစ်ပိုင်အား ဖော်ပြထားခြင်း ဖြစ်ပါသည်။ အထက်ဖော်ပြပါ ကုဒ်များတွင် List<E> ဟု ရေးသားထားသည်ကို တွေ့ရပါမည်။ ဤနေရာသင် Java 5 အရောက်တွင် ဖြည့်စွက်ခဲ့သော Generics ၏ ရေးသားပုံ ဖြစ်ပါသည်။ <> ဤ အစိတ်အပိုင်းအား ပုံစံပါရာမီတာဟု ခေါ်ဆိုပြီး၊ ကလပ်စ် အတွင်းတွင် အသုံးပြုမည့် ကိန်းရှင်များအား ပုံစံအား ပါရာမီတာ အဖြစ်ရယူနိုင်ပါသည်။ <E> ဟု ရေးသားထားရာတွင် E သည် ပုံစံ ကိန်းရှင် ဖြစ်ပြီး၊ E နေရာတွင် အစားထိုးသော ပုံစံ အား အခြေခံ ပုံစံ အဖြစ်အသုံးပြုနိုင်မည် ဖြစ်ပါသည်။ List#add ၏ နေရာတွင် E နေရာတွင် ရေးသားထားသော ပုံစံအား အသုံးပြုမည် ဖြစ်ပြီး၊ List#get ၏ ရလဒ်သည်လည်း E နေရာတွင် အစားထိုးမည့် အော့ဘဂျက်အား ပြန်လည်ရရှိမည် ဖြစ်ပါသည်။

အကယ်၍ List<String> ဟု ရေးသားထားပါက String Object များအား အသုံးပြုသော List အား အသုံးပြုနိုင်မည် ဖြစ်သည်။ ထို့ကြောင့် List#add ၏ ပါရာမီတာသည် String Object ကို အသုံးပြုရန် လိုအပ်ပြီး၊ List#get ၏ ရလဒ်သည်လည်း String Object ဖြစ်မည် ဆိုသည်ကို သိရှိနိုင်ပါသည်။

တဖန် List အင်တာဖေစ်အား ဖြည့်စွက် ရေးသားထားသော LinkedList ကလပ်စ်၏ ရေးသားပုံမှာ အောက်ပါအတိုင်း ဖြစ်၏။

LinkedList.java
public class LinkedList<E>
       extends AbstractSequentialList<E>
       implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{

    @Override
    public boolean add(E object) {
     // အသေးစိတ်အား အထက်ဖော်ပြပါ လင့်ခ်တွင် လေ့လာပါ
     return false;
    }

    @Override
    public E get(int index) {
     // အသေးစိတ်အား အထက်ဖော်ပြပါ လင့်ခ်တွင် လေ့လာပါ
     return null;
    }

အင်တာဖေစ် List အား ပံ့ပိုးထားသော ကလပ်စ် LinkedList အား ရေးသားရာတွင်၊ အင်တာဖေစ် ကဲ့သို့ပင် Generics အား အသုံးပြုရန် လိုအပ်ပါသည်။ ရေးသားရာတွင်လည်း အင်တာဖေစ်နှင့် ထပ်တူ <E> ဟု ရေးသားထားပါသည်။

ကျွှန်တော်တို့ နမှုနာတွင် List<String> list = new LinkedList<>() ဟု အင်တာဖေစ် List<String> အော့ဘဂျက်အား LinkedList ကလပ်စ်အား အင်းစတန့်စ် လုပ်ခြင်းအားဖြင့် ရေးသားပါသည်။ <> ရေးသားပုံမှာ Java 7 တွင် စတင် အသုံးပြုလာသော ပုံစံဖြစ်ပြီး  Diamond ပုံစံဟု ခေါ်ဆိုပါသည်။ ဘယ်ဘက်တွင် List<String> ဟုပုံစံကိန်းရှင်အား သတ်မှတ် ရေးသားထားပါက၊ ညာဘက်ချမ်း၊ အင်းစတန့်စ်အား ရေးသားသည့်ဘက်တွင် <> ဟု ရေးသားသည်နှင့် အလိုအလျှောက် ဘယ်ဘက်တွင် ရေးသားထားသော ပုံစံ ကိန်းရှင်အား အသုံးပြုနိုင်မည် ဖြစ်ပါသည်။ ထို့ကြောင့် String အား အသုံးပြုပါသဖြင့် LinkedList ၏ ပုံစံကိန်းရှင်သည်လည်း String ဖြစ်မည် ဖြစ်ပါသည်။


ပုံစံပါရာမီတာ

ကျွှန်တော်တို့ <> ဖြင့် ရေးသားသော ပုံစံပါရာမီတာ အကြောင်းကို ဆက်လက် လေ့လာကြည့်ပါဦးမည်။ ပုံစံပါရာမီတာသည် ကလပ်နှင့် အင်တာဖေစ်များအား Declare လုပ်ရာတွင် အမည်၏ နောက်တွင်၎င်း၊ လုပ်ဆောင်ချက်များတွင်၎င်း Generics Method အနေဖြင့် ရေးသားလေ့ရှိပါသည်။ <> အထဲတွင် ပုံစံကိန်းရှင်များကို တစ်ခုထက်မက အသုံးပြု ရေးသားနိုင်ပါသည်။ ကိန်းရှင်များအား ကော်မာခံ၍ ရေးသားနိုင်ပါသည်။
public interface Map<K, V> {
    ...
အသုံးပြုသော ပုံစံကိန်းရှင်များ၏ အမည်မှာလည်း သာမန်ကိန်းရှင်များကဲ့သို့ ရေးသားနိုင်သော်လည်း၊ အင်္ဂလိပ်စာလုံးကြီး တစ်လုံးကို အသုံးပြုသည်က များပါသည်။

သက်မှတ်ထားသော ကိန်းရှင်များအားလည်း ယခုတိုင်ဖော်ပြခဲ့သော Extends ဝါကျ၊ Implements ဝါကျ များသာမက၊ လုပ်ဆောင်ချက်၏ ပါရာမီတာများနှင့် ရလဒ်များ၊ အင်းစတန့်စ် ကိန်းရှင်များနှင့် လုပ်ဆောင်ချက် အတွင်းရှိ ကိန်းရှင်များတွင်ပါ အသုံးပြုနိုင်သည်။

တနည်းဆိုရသော် ပုံစံကိန်းရှင်အား အသုံးပြုခြင်း အားဖြင့်၊ အော့ဘဂျက်အတွင်း မည်သည့် နေရာမှ မဆို ဆက်သွယ် အသုံးပြုနိုင်မည် ဖြစ်သည်။လက်တွေ့ ရေးသားကြည့်ပါဦးမည်။

MyData.java
public class MyData<S> {
 
 private S myData = null;
 
 public MyData (S data) {
  this.myData = data;
 }

 public String getMyData() {
  return myData.toString();
 }

 public void setMyData(S myData) {
  this.myData = myData;
 }
}
အထက်ပါ နမှုနာထဲတွင် အင်းစတန့်စ် ကိန်းရှင် myData ၏ ပုံစံအား ပုံစံကိန်းရှင် S အား အသုံးပြုကာ၊ တဖန် ပါရာမီတာအဖြစ်ရယူသော S data ဖြင့်အစားထိုးပါသည်။ အတွင်းပိုင်းရှိ S ၏ နေရာတွင် အဆိုပါ ကလပ်စ်အား Instance လုပ်သည့်အခါတွင် ပုံစံပါရာမီတာ အတွင်းတွင် ရေးသားထားသော ပုံစံအား အစားထိုး အသုံးပြုသွားမည် ဖြစ်ပါသည်။ အတွင်းပိုင်းရှိ S များအားလုံးအား အစားထိုး အသုံးပြုသွားနိုင်မည် ဖြစ်ပါသည်။

တဖန် စာကြောင်း ၉ အားကြည့်ပါ။ ဤ ကလပ်စ်အား ရေးသားစဉ်က S သည် မည်သည့်ပုံစံ ဖြစ်သည်ကို မသိရပေ။ သို့သော်လည်း getMyData ၏ ရလဒ်အား String ဟု ရေးသားထားပါသည်။ အတွင်းပိုင်းမှာမူ data.toString() ဟု Object ကလပ်စ်၏ လုပ်ဆောင်ချက်ကို အသုံးပြုထားပါသည်။ အဘယ်ကြောင့်ဆိုသော် S အား အစားထိုး၍ အသုံးပြုမည့် ကလပ်စ်များသည် Object ကလပ်စ်၏ Sub Class များ ဖြစ်သောကြောင့် ဖြစ်ပါသည်။ ထို့ကြောင့် java.lang.Object ၏ လုပ်ဆောင်ချက် toString အား ခေါ်ယူ အသုံးပြုနိုင်ခြင်း ဖြစ်ပါသည်။

Object တင်မက အခြားသော ကလပ်စ်များကိုလည်း အသုံးပြုလိုပါက အောက်ပါအတိုင်း အသုံးပြု ရေးသားနိုင်ပါလိမ့်မည်။

MyDateData.java
import java.util.Date;

public class MyDateData<S extends Date, E extends Date> {

 private S startDate = null;
 private E endDate = null;

 public MyDateData(S s, E e) {
  this.startDate = s;
  this.endDate = e;
 }

 public long calSpan() {
  return this.endDate.getTime() - this.startDate.getTime();
 }

}

အထက်ပါ နမှုနာထဲတွင် S နှင့် E ပုံစံကိန်းရှင်များအား extends Keyword အား အသုံးပြု၍ java.util.Date ၏ အမွေဆက်ခံမှု့ကို ရယူစေပါသည်။ ထို့အတွက် S နှင့် E အား Date ၏ Sub Class များသာ အစားထိုးနိုင်ပြီး၊ အတွင်းပိုင်းရှိ အင်းစတန့်စ် ကိန်းရှင်များဖြစ်ကြသော startDate နှင့် endDate များသည် java.util.Date ၏ လုပ်ဆောင်ချက်များအား အသုံးပြုနိုင်မည် ဖြစ်ပါသည်။

ကျွှန်တော်တို့သည် Generics သုံး ကလပ်စ်များကိုသာ အသုံးပြုနေရုံမက၊ ကိုယ်တိုင်လည်း Generics များအား အသုံးပြု၍ ရေးသားနိုင်သည်ဟု သိရှိနိုင်မည် ဖြစ်ပါသည်။


ကိုးကား

http://www.techscore.com/tech/Java/JavaSE/JavaLanguage/1/

ဆက်ပါဦးမည်။ လေးစားစွာဖြင့်။
မင်းလွင်

26 comments:

  1. I have been browsing on-line greater than three hours these days, but I by no means found
    any fascinating article like yours. It is pretty price enough for me.
    In my view, if all site owners and bloggers made excellent content as you probably did, the internet shall be a lot more helpful than ever before.

    ReplyDelete
  2. My brother suggested I might like this website. He was entirely right.
    This post truly made my day. You can not imagine simply how much time I had spent for this information!
    Thanks!

    ReplyDelete
  3. Thanks for your marvelous posting! I genuinely enjoyed reading it, you may be a
    great author.I will make certain to bookmark your
    blog and definitely will come back in the foreseeable future.
    I want to encourage you to continue your great work,
    have a nice afternoon!

    ReplyDelete
  4. Greetings, I believe your blog might be having web browser compatibility issues.
    When I take a look at your site in Safari, it looks fine however, if opening in IE,
    it's got some overlapping issues. I merely wanted to provide you with a quick heads up!
    Aside from that, great site!

    ReplyDelete
  5. I blog frequently and I seriously appreciate your information. Your article has truly peaked
    my interest. I will book mark your blog and keep checking for new details
    about once a week. I opted in for your Feed as well.

    ReplyDelete
  6. Hello There. I found your blog using msn. This is an extremely well written article.
    I'll make sure to bookmark it and come back to read more of your useful information. Thanks
    for the post. I'll certainly return.

    ReplyDelete
  7. This is a topic that is near to my heart... Many thanks!
    Exactly where are your contact details though?

    ReplyDelete
  8. Aw, this was a very good post. Taking a few minutes and actual effort to create a really good
    article… but what can I say… I put things off a lot and don't manage to get
    nearly anything done.

    ReplyDelete
  9. I am actually grateful to the holder of this website who has shared this enormous article at at this place.

    ReplyDelete
  10. This article will help the internet visitors for building up new weblog
    or even a blog from start to end.

    ReplyDelete
  11. Hi there, You have done an excellent job. I will certainly
    digg it and personally recommend to my friends.
    I am confident they will be benefited from this site.

    ReplyDelete
  12. I believe everything posted was actually very reasonable.
    But, think on this, what if you were to create a awesome title?
    I mean, I don't want to tell you how to run your blog, however
    what if you added a post title that grabbed a person's attention? I mean "Generics" is a little
    vanilla. You might glance at Yahoo's home page and see how they create article headlines to
    grab viewers interested. You might try adding a video or a related pic or two to
    grab people interested about what you've got to say.
    Just my opinion, it might make your posts a little livelier.

    ReplyDelete
  13. I enjoy what you guys are up too. This kind of clever work and reporting!
    Keep up the awesome works guys I've included you guys to my personal blogroll.

    ReplyDelete
  14. Does your website have a contact page? I'm having a tough time locating it
    but, I'd like to send you an e-mail. I've got some ideas for your blog you might be interested in hearing.

    Either way, great blog and I look forward
    to seeing it develop over time.

    ReplyDelete
  15. Howdy terrific blog! Does running a blog such as this require a massive
    amount work? I have no understanding of computer
    programming however I had been hoping to start my own blog
    soon. Anyhow, should you have any suggestions or techniques for new
    blog owners please share. I understand this is off topic but
    I just needed to ask. Cheers!

    ReplyDelete
  16. I'm not sure why but this web site is loading very slow for me.
    Is anyone else having this issue or is it a problem on my end?
    I'll check back later on and see if the problem still exists.

    ReplyDelete
  17. Having read this I believed it was extremely informative.
    I appreciate you spending some time and energy to put this content together.
    I once again find myself personally spending a significant amount of time both reading and posting comments.
    But so what, it was still worth it!

    ReplyDelete
  18. Hi mates, nice post and good arguments commented here, I am in fact enjoying by these.

    ReplyDelete
  19. It's perfect time to make a few plans for the long run and it is time to be happy.
    I've learn this put up and if I could I wish to recommend you some
    attention-grabbing things or tips. Perhaps you could write subsequent articles regarding this article.

    I desire to read more issues approximately it!

    ReplyDelete
  20. Terrific article! This is the kind of info
    that are supposed to be shared across the web. Disgrace on the search engines for now
    not positioning this publish higher! Come on over
    and talk over with my website . Thanks =)

    ReplyDelete
  21. Excellent goods from you, man. I have understand your stuff previous to and you are just too fantastic.
    I actually like what you have acquired here, really like what
    you are stating and the way in which you say it.
    You make it enjoyable and you still care for to keep it wise.
    I can't wait to read far more from you. This is really a wonderful site.

    ReplyDelete
  22. Wow, this article is pleasant, my sister is analyzing these things, therefore I am
    going to convey her.

    ReplyDelete
  23. Hello! Would you mind if I share your blog with my twitter group?
    There's a lot of folks that I think would really enjoy your content.
    Please let me know. Many thanks

    ReplyDelete
  24. Keep on working, great job!

    ReplyDelete
  25. Right away I am going to do my breakfast, afterward having my breakfast coming again to read
    other news.

    ReplyDelete
  26. Keep on working, great job!

    ReplyDelete