April 18, 2014

Interface ၏ ပြောင်းလဲခြင်းများ

Project Lambda ဖြင့်ပြောင်းလဲမှု့များပြုလုပ်ခဲ့ရာတွင် Lambda Expression ကြီးသာမဟုတ်။ အခြားသော Java Programming အပိုင်းတွင်လည်း ပြောင်းလဲမှု့များကို ပြုလုပ်ခဲ့၏။ ပြီးခဲ့သော အခေါက်တွင်လည်း Effective final, Method Reference တို့ကို ဖော်ပြခဲ့၏။ ယခုတစ်ခေါက်တွင် Interface များ၏ ပြောင်းလဲမှု့ကို ဆက်လက်ဖော်ပြသွားပါဦးမည်။

Interface ၏ ပြောင်းလဲမှု့ဆိုပါက Functional Interface, Default Method on Interface နှင့် Static Method of Interface တို့ ဖြစ်ကြ၏။ Interface ၏ Default Method နှင့် Static Method တို့သည် Interface ၏ အခြေခံ အယူအဆအား ပြောင်းလဲခဲ့သောကြောင့် Java ဘာသာရပ်အတွက် ကြီးမားသော ပြောင်းလဲမှု့  တစ်ခုဖြစ်ပါသည်။

Lambda Expression ဖြင့် Interface Object များအား implement ပြုလုပ်ရန် Method တစ်ခုတည်းသာပါသော Interface ကို အသုံးပြုခဲ့ကြ၏။ အဆိုပါ Interface အား Functional Interface ဟု ခေါ်ပါသည်။ Functional Interface မှန်း ကွန်ပိုင်းလာအား အသိပေးနိုင်ရန် @FunctionalInterface annotation အား အသုံးပြုခဲ့ကြ၏။ Functional Interface နှင့် ပတ်သက်၍ Lambda Expression တွင် ဖော်ပြပြီး ဖြစ်သောကြောင့် ဤတစ်ခေါက်တွင် ရေးသားဖော်ပြတော့မည် မဟုတ်ပါ။


Default Method


Collection Framework ၏ Collection များကိုလည်း Lambda Expression နှင့် တွဲဖက်အသုံးပြုနိုင်ရန် Interior Iterator အနေနှင့် အသုံးပြုနိုင်ရန် အတွက် Iterable Inteface တွင် forEach Method အား ဖြည့်စွက်ရန် လိုအပ်ခဲ့၏။ ထို့အတွက် Basic Class များတွင် Method များအား ဖြည့်စွက်မည်ဆိုပါက၊ သိပ်ပြီးမခြားနားသော Method များအား Class အတော်များများအား ဖြည့်စွက်ရမည်ဖြစ်သည်။ ဤအချက်အား ရှောင်ရှားရန်အတွက် Interface များတွင် Default Method များအား ရေးသားလာနိုင်အောင် ပြုပြင်ခဲ့ပါသည်။ ဤကဲ့သို့ Interface တွင် Default Method အား ရေးသားလာနိုင်ခြင်း အားဖြင့် ၎င်းအား Implement လုပ်သော Class များတွင် အဆိုပါ Method အား ရေးသားရန် မလိုအပ်တော့ပါ။

ဤသို့ဆိုလျှင် Default Method ပါသော Interface နှင့် Abstract Class တို့သည် ခြားနားမှု့ ရှိတော့မည်မဟုတ်ဟု ထင်စရာရှိပါသည်။ မဟုတ်ပါ။ Abstract Class သည် State အား ပိုင်ဆိုင်နိုင်၍၊ Interface သည် လက်ရှိအနေအထားအရ State အား ပိုင်ဆိုင်နိုင်ခြင်း မရှိပါ။

ထို့အတွက် Instance Variable အား အသုံးပြုလိုပါက Abstract Class အား အသုံးပြု၍၊ Instance Variable အား အသုံးပြုရန် မလိုအပ်ပါက Interface နှင့် Default Method တို့အား အသုံးပြုနိုင်ပါသည်။


Default Method အား ရေးသားပုံ



interface [interface name] {
    default [return type] [method name] ([arguments]) {
        // statements
    }
}
Default Method အနေနှင့် အသုံးပြုနိုင်ရန် Interface အတွင်းရေသားသော Method အား default keyword အား အသုံးပြု၍ ရေးသားနိုင်ပါသည်။ Interface အား Implement လုပ်ထားသော Class အတွင်း Default Method အား ထိုအတိုင်းအသုံးပြုနိုင်သလို၊ လိုအပ်ပါက Override လုပ်၍လည်း ရေးသားနိုင်ပါသည်။

Default Class အား ရေးသားထားသော Iterable Interface ၏ forEach method အား နမှုနာအနေနှင့် ကြည့်ပါမည်။
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }
forEach Default method အတွင်းတွင် for statement အသစ်အား အသုံးပြုထားပြီး၊ ၎င်းအတွင်း Consumer Object ၏ accept method အား Reference လုပ်နေပါသည်။ တဖန် default method အတွင်းတွင် this keyword အား အသုံးပြုနိုင်ပါသည်။



Default Method အား ရေးသားချင်း


package com.dtc.lambda;

public class DefaultMethodSample {
    
    interface Greeting {
        default void greet(String name) {
            System.out.println("Hello! " + name);
        }
    }
    
    @FunctionalInterface
    interface AnimalGreeting extends Greeting {
        @Override
        public void greet(String name);
    }
    
    public DefaultMethodSample() {
        Greeting human = new Greeting() {};
        human.greet("Mg Mg");
        
        AnimalGreeting dog = x -> System.out.println("Wote Wote! " + x);
        dog.greet("Mg Mg");
    }

    public static void main(String[] args) {
        new DefaultMethodSample();
    }
}
အထက်ပါနမှုနာတွင် Greeting Interface တွင် greet default method အား ရေးသားထားပါသည်။ ထို့နောက် Greeting Interface ၏ Object human မှ default method greet အား ခေါ်ယူအသုံးပြုပါသည်။

တဖန် AnimalGreeting Interface ဖြင့် Greeting Interface အား Extends လုပ်၍ ၎င်းအတွင်းတွင် greet default method အား Override လုပ်ထားပါသည်။ ထို့ကြောင့် AnimalGreeting အား Implement လုပ်သည့် Class အတွင်း greet method အား Implement လုပ်၍ ရေးသားရန် လိုအပ်ပါသည်။ AnimalGreeting Interface ၏ Object အား Lambda Expression ဖြင့် ရေးသားထားပါသည်။ ထို့ကြောင့် AnimalGreeting Object dog တွင် Lambda Expression ဖြင့် ဖော်ပြထားသည့်အတိုင်း လုပ်ဆောင်သွားမည် ဖြစ်ပါသည်။

ဤနည်းအားဖြင့် Interface အတွင်းပါဝင်သော default method အား ဤအတိုင်း အသုံးပြုလိုပါက အသုံးပြုနိုင်ပြီး၊ ပြုပြင်ပြောင်းလည်းပြီး အသုံးပြုလိုပါက Sub Interface တွင်၎င်း၊ Implement လုပ်သည့် Class ဘက်တွင်၎င်း Override လုပ်၍ ရေးသားနိုင်ပါသည်။


သတိပြုရန်အချက်များ


  • Java ဘာသာရပ်၌ Class များသည် တစ်ခုတည်းမှသာ Extends လုပ်နိုင်သော်လည်း၊ Interface များသည် တစ်ခုထက်မက Extends လုပ်နိုင်၏။ သို့ရာတွင် တူညီသော Default Method များပါသည့် Interface များအား တစ်ခုထက်ပို၍ Extends လုပ်၍ မရနိုင်ပါ။
  • Class တစ်ခုသည် Default Method တူညီသော Interface နှစ်ခုအား Implement လုပ်လိုပါက၊ Class အတွင်းတွင် Override လုပ်၍ ရေးသားနိုင်ပါသည်။ 
  • Class ဘက်တွင် Interface ၏ Default Method အား Override လုပ်ထား၍၊ ၎င်း Default Method အား ခေါ်ယူအသုံးပြုလိုသည့်အခါ InterfaceName.super.methodName ဟု ခေါ်ယူ အသုံးပြုနိုင်ပါသည်။
  • တဖန် Class တစ်ခု၏ Extends လုပ်ထားသော Super Class မှာကော၊ Implement လုပ်ထားသော Interface မှာပါ အမည်တူ Default Method ပါဝင်ပါက၊ Extends လုပ်ထားသော Super Class ၏ method အား အသုံးပြုသွားမည် ဖြစ်သည်။


Static Method of Interface

Java SE 8 အရောက်တွင် Interface တွင် Default Method အား အသုံးပြုလာနိုင်သကဲ့သို့၊ Static Method ကိုလည်း ရေးသားလာနိုင်ပါသည်။ ရေးသားပုံမှာ Class များတွင် ရေးသားသကဲ့သို့သာပင် ဖြစ်၏။

JDK ၏ Comparator Interface တွင် Static Method များစွာ အသစ် ဖြည့်စွက်ထားပါသည်။
    public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
        return Collections.reverseOrder();
    }
ဤနည်းအားဖြင့် Interface တွင် Utility Method များအား ဖြည့်စွက်ရေးသားလာနိုင်ပါသည်။

ကိုးကား
http://itpro.nikkeibp.co.jp/article/COLUMN/20140324/545385/?ST=develop&P=1

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

No comments:

Post a Comment