<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);


    版權所有 羅明
    posted on 2006-01-05 21:01 羅明 閱讀(459) 評論(0)  編輯  收藏 所屬分類: Java
     
    主站蜘蛛池模板: 欧美a级成人网站免费| 亚洲AⅤ永久无码精品AA| 亚洲熟妇无码AV| 亚洲一区二区三区自拍公司| 美女视频黄的免费视频网页| 亚洲一区二区三区在线观看网站| 亚洲日本韩国在线| 国产免费不卡视频| 免费一级特黄特色大片| 亚洲综合激情视频| 亚洲福利中文字幕在线网址| 99在线热视频只有精品免费| 国产亚洲精品91| 亚洲制服丝袜在线播放| 相泽亚洲一区中文字幕| 免费av欧美国产在钱| 中文字幕久无码免费久久 | 国产人成亚洲第一网站在线播放| 亚洲一级片免费看| 成人毛片18女人毛片免费视频未 | 毛片免费全部免费观看| 在线观看人成视频免费无遮挡| 亚洲av午夜精品无码专区| 亚洲午夜久久久影院伊人| 成人一a毛片免费视频| 国产精成人品日日拍夜夜免费| 国产亚洲欧美在线观看| 亚洲xxxxxx| 亚洲日韩图片专区第1页| 亚洲伊人久久综合影院| 天天摸天天碰成人免费视频| 99热这里有免费国产精品| 精品久久久久久国产免费了| 亚洲av成人片在线观看| 国产亚洲精品bv在线观看| 亚洲美女在线观看播放| 亚洲国产成人一区二区精品区| 亚洲高清无码专区视频| 国产男女猛烈无遮挡免费网站| 男女免费观看在线爽爽爽视频| 久久国产乱子免费精品|