<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
     
    主站蜘蛛池模板: 亚洲综合色7777情网站777| 免费的一级片网站| 国产∨亚洲V天堂无码久久久| 亚洲人片在线观看天堂无码| 国产精品1024永久免费视频 | 国产va免费观看| 亚洲国产专区一区| 一级毛片试看60分钟免费播放| 免费一级国产生活片| 美女裸体无遮挡免费视频网站| 免费人成视频在线观看不卡| 性生大片视频免费观看一级| 亚洲午夜日韩高清一区| 美女无遮挡免费视频网站| 国产精品视频免费一区二区三区| 无码色偷偷亚洲国内自拍| 亚洲高清偷拍一区二区三区| 免费人成网站永久| 伊人久久大香线蕉亚洲五月天| 最近中文字幕大全免费版在线| 无码久久精品国产亚洲Av影片 | 日韩精品免费一线在线观看 | 免费在线观看黄色毛片| 国产美女视频免费观看的网站| 日本亚洲欧洲免费天堂午夜看片女人员| 一级毛片免费观看不卡的| 亚洲一级黄色大片| 免费在线精品视频| 国精产品一区一区三区免费视频| 久久精品国产亚洲AV无码麻豆| 成年丰满熟妇午夜免费视频| 国产一区二区三区亚洲综合| 精品国产综合成人亚洲区| 免费人成网站在线观看10分钟| 国产精品亚洲一区二区在线观看 | 亚洲av无码专区首页| 77777亚洲午夜久久多人| 91精品免费久久久久久久久| 国产精品亚洲а∨无码播放不卡 | 亚洲日韩aⅴ在线视频| 欧美最猛性xxxxx免费|