我的内部 API 类是否应该全部放在一个包中? [英] Should my internal API classes be all in one package?

查看:33
本文介绍了我的内部 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.

问题是我有很多内部代码需要访问这些受限制的方法而不将这些方法公开.这会产生两个问题:

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:

  • 我无法创建接口像这样在类之间进行通信将使这些成为我的内部方法公开.
  • 我无法访问 protected 或 default方法,除非我把大部分我的内部课程在同一个包.

所以,我有大约 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.

第一种是使用 (实用 API 设计,Tulach 2008).

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

第二种是使用OSGi.有一篇文章 此处 解释了 OSGi 如何实现这一点.

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

相关问题:1234.

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

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

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