<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 羅明 閱讀(466) 評論(0)  編輯  收藏 所屬分類: Java
     
    主站蜘蛛池模板: 无码久久精品国产亚洲Av影片| 亚洲第一福利网站| 91亚洲导航深夜福利| 日韩亚洲国产综合高清| 久青草国产免费观看| 18禁美女黄网站色大片免费观看| 在线免费观看色片| 国产AV无码专区亚洲AV漫画| 亚洲国产日韩女人aaaaaa毛片在线| 另类小说亚洲色图| 三年片在线观看免费观看大全一| 亚洲大成色www永久网址| 人成午夜免费大片在线观看| 一级毛片完整版免费播放一区| 无码人妻久久一区二区三区免费 | 亚洲色偷偷综合亚洲AV伊人蜜桃| 九九免费精品视频在这里| 免费在线观看视频网站| 亚洲国产综合久久天堂| 亚洲一区二区视频在线观看| 国产亚洲精品a在线无码| 亚洲色偷偷综合亚洲AV伊人蜜桃| 日本亚洲欧洲免费天堂午夜看片女人员| 在线免费观看a级片| 亚洲AV日韩精品久久久久久| 国产亚洲精彩视频| AV无码免费永久在线观看| 中文字幕精品亚洲无线码一区| 亚洲色无码专区一区| 97在线视频免费公开观看| 亚洲人成网站观看在线播放| 456亚洲人成影院在线观| 永久免费不卡在线观看黄网站| 国产免费一区二区三区VR| 亚洲国产精品成人久久久| 中文在线观看国语高清免费| 国产精品视_精品国产免费| 亚洲妇女水蜜桃av网网站| 中国黄色免费网站| 免费观看午夜在线欧差毛片| 亚洲AV无码一区二区三区牛牛|