<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 羅明 閱讀(466) 評論(0)  編輯  收藏 所屬分類: Java
     
    主站蜘蛛池模板: 国产精品亚洲自在线播放页码| 亚洲一区二区三区成人网站| 免费国产a国产片高清| 国产片免费在线观看| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 一个人在线观看视频免费| 日韩一级视频免费观看| 欧洲亚洲国产清在高| 亚洲精品亚洲人成在线播放| 麻豆视频免费播放| 国产偷窥女洗浴在线观看亚洲| 亚洲精品视频观看| a级毛片无码免费真人久久| 亚欧免费视频一区二区三区 | 亚洲偷自拍拍综合网| 国产福利电影一区二区三区,免费久久久久久久精 | 国产一级高清免费观看| 亚洲欧洲精品视频在线观看| 91福利视频免费观看| 亚洲AⅤ优女AV综合久久久| 久久亚洲精品成人777大小说| 亚洲AV无码一区二区乱子仑| 2021国内精品久久久久精免费| 自拍偷自拍亚洲精品情侣| 亚洲熟妇av午夜无码不卡| 日韩在线免费看网站| 国产高潮久久免费观看| 亚洲AV无码一区二区三区DV| 一级做a爰片久久免费| 成全视频免费高清| 亚洲一区二区三区电影| 91福利免费网站在线观看| 久久久久亚洲AV无码永不| 无码日韩人妻av一区免费| 免费中文字幕视频| 热99re久久精品精品免费| 亚洲av永久无码嘿嘿嘿| 久草视频在线免费| 国产精品亚洲天堂| 亚洲精品tv久久久久| 国产99久久亚洲综合精品|