<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Source: http://openide.netbeans.org/tutorial/api-design.html#design.less.friend
    Allow access only from a friend code
    Another useful technique to not expose too much in API is to give access to certain functionality (e. g. ability to instantiate a class or to call a certain method) just to a friend code.

    Java by default restricts the friends of a class to those classes that are in the same package. If there is a functionality that you want share just among classes in the same package, use package-private modifier in definition of a constructor, a field or a method and then it will remain accessible only to friends.

    Sometimes however it is more useful to extend the set of friends to a wider range of classes - for example one wants to define a pure API package and put the implementation into separate one. In such cases following trick can be found useful. Imagine there is a class item:

    public final class api.Item {
        /** Friend only constructor */
        Item(int value) {
            this.value = value;
        }
    
        /** API method(s) */
        public int getValue() {
            return value;
        }
            
        /** Friend only method */
        final void addListener(Listener l) {
            // some impl
        }
    }
    
    that is part of the API, but cannot be instanitated nor listened on outside of the friend classes (but these classes are not only in api package). Then one can define an Accessor in the non-API package:
    public abstract class impl.Accessor {
        public static Accessor DEFAULT;
    
        static {
            // invokes static initializer of Item.class
            // that will assign value to the DEFAULT field above
            Class c = api.Item.class;
            try {
                Class.forName(c.getName(), true, c.getClassLoader());
            } catch (ClassNotFoundException ex) {
                assert false : ex;
            }
            assert DEFAULT != null : "The DEFAULT field must be initialized";
        }
    
        /** Accessor to constructor */
        public abstract Item newItem(int value);
        /** Accessor to listener */
        public abstract void addListener(Item item, Listener l);
    }
    
    with abstract methods to access all friend functionality of the Item class and with a static field to get the accessor's instance. The main trick is to implement the Accessor by a (non-public) class in the api package:
    final class api.AccessorImpl extends impl.Accessor {
        public Item newItem(int value) {
            return new Item(value);
        }
        public void addListener(Item item, Listener l) {
            return item.addListener(l);
        }
    }
    
    and register it as the default instance first time somebody touches api.Item by adding a static initializer to the Item class:
    public final class Item {
        static {
            impl.Accessor.DEFAULT = new api.AccessorImpl();
        }
    
        // the rest of the Item class as shown above
    }
    
    Then the friend code can use the accessor to invoke the hidden functionality from any package:
    api.Item item = impl.Accessor.DEFAULT.newItem(10);
    impl.Accessor.DEFAULT.addListener(item, this);


    版權(quán)所有 羅明
    posted on 2006-01-05 21:01 羅明 閱讀(460) 評論(0)  編輯  收藏 所屬分類: Java
     
    主站蜘蛛池模板: 日韩免费视频播播| 最近中文字幕完整免费视频ww | 女人18毛片水真多免费播放 | 一区二区视频免费观看| 免费很黄无遮挡的视频毛片| 噜噜综合亚洲AV中文无码| 亚洲精品无码国产片| 久久亚洲精品成人无码| 精品韩国亚洲av无码不卡区| 在线观看亚洲网站| 免费看美女午夜大片| 日韩精品视频在线观看免费| 一级免费黄色大片| 国色精品va在线观看免费视频| 国产三级在线免费| 18女人毛片水真多免费| 久久受www免费人成_看片中文| 亚洲午夜精品在线| 67194在线午夜亚洲| 亚洲色大成网站www尤物| 亚洲一区二区成人| 亚洲欧洲日本天天堂在线观看| 亚洲欧洲日产国码无码网站| 国产亚洲综合网曝门系列| 久久久久亚洲精品无码蜜桃 | 免费人妻无码不卡中文字幕18禁| 亚洲国产婷婷综合在线精品| 亚洲乱码精品久久久久..| 久久精品国产精品亚洲艾草网 | 亚洲午夜福利717| 亚洲精品线在线观看| 久久狠狠爱亚洲综合影院| 亚洲成AV人片在WWW| 亚洲人成www在线播放| 直接进入免费看黄的网站| 国产亚洲精品免费视频播放| 一级毛片免费观看不收费| 免费人成网站在线观看不卡| 免费A级毛片无码无遮挡内射| 国产成人免费一区二区三区| 中文字幕第13亚洲另类|