如果我的内部API类是在一个包? [英] Should my internal API classes be all in one package?

查看:155
本文介绍了如果我的内部API类是在一个包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力包装起来用于公共消费的API。因此我试图限制只暴露于那些我想是公开的和可支持的方法。当然,在此下方有有限的接入方式众多。

I'm hard at work packaging up an API for public consumption. As such I'm trying to limit the methods that are exposed to only those that I wish to be public and supportable. Underneath this of course there are a multitude of limited access methods.

麻烦的是,我有很多的内部code的需要访问这些限制的方法,而无需对这些方法公开。这就造成了两个问题:

The trouble is that I have a lot of internal code that needs to access these restricted methods without making those methods public. This creates two issues:


  • 我不能创建界面
    类,因为这之间的沟通
    将使得这些我的内部方法
    大众。

  • 我无法访问受保护或默认
    方法,除非我把大部分
    我的内部类在同一个
    包。

所以,我有干净隔离包,但与过度授权的访问修饰符大约70或80的内部类。你会说一个封装为两害取其轻,或是否有更好的方法能够掩盖我内心的方法,同时保持更精细的包?

So, I have around 70 or 80 internal classes in cleanly segregated packages BUT with overly permissive access modifiers. Would you say that a single package is the lesser of two evils or is there a better way to be able to mask my internal methods whilst keeping more granular packages?

我很想在这里找到了最好的做法。

I'd be interested to find out the best practice here.

我已经知道

推荐答案

有两种办法可以解决你的问题,不涉及保留所有的类在同一个包。

There are two solutions to your question that don't involve keeping all classes in the same package.

第一种方法是使用Friend存取/ 朋友包图案(实用描述API设计,2008年Tulach)。

The first is to use the Friend Accessor/Friend Package pattern described in (Practical API Design, Tulach 2008).

二是使用OSGi的。有一篇文章<一个href=\"https://web.archive.org/web/20091108165509/http://www.1biznow.com/2009/02/hide-private-parts-of-your-bundle.html\"相对=nofollow>这里解释的OSGi是如何实现这一点。

The second is to use OSGi. There is an article here explaining how OSGi accomplishes this.

相关问题:<一href=\"http://stackoverflow.com/questions/5872124/how-to-hide-the-internal-structure-of-a-java-api-to-the-rest-of-the-world/6154321#6154321\">1, 2 ,的 3 和<一个href=\"http://stackoverflow.com/questions/182278/is-there-a-way-to-simulate-the-c-friend-concept-in-java\">4.

Related Questions: 1, 2, 3, and 4.

这篇关于如果我的内部API类是在一个包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆