根据predefined API限制进入类和方法 [英] Restrict access to classes and methods according to a predefined API

查看:152
本文介绍了根据predefined API限制进入类和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每个包多个包以及多种类型的Java项目。我希望这个项目可以作为一个库(最好说在这种情况下框架),提供某些功能。该框架将被其他应用程序(在不同的Eclipse项目)一起使用。

I have a Java project with multiple packages and multiple classes in each package. I want this project to be used as a library (better say framework in this case), offering certain functionalities. The framework will be used by other application (in a different Eclipse project).

我怎么可以指定我想成为可供其他应用程序的类和方法?我只想要一个几类/方法是API的一部分。在implementated code的其余部分应该被隐藏的用户。

How can I specify the classes and methods that I want to be available for other applications? I only want a few classes/methods to be part of the API. The rest of the implementated code should be hidden for the users.

PD:我不意思是如何设计的API,但是如何组织code或访问修饰符,我应该用

PD: I don´t mean how to design the API, but how to organize the code or which access modifiers should I use.

推荐答案

好了,除非你把所有的code在一个封装中,你将需要作出规定:在得到您的实现包之间共享的公共什么。

Well, unless you put all the code in one package, you're going to need to make public anything that gets shared between packages in your implementation.

这通常解决问题的方法是通过约​​定:只为API类提供文件,并指定任何其他类中的JAR不支持,可能会随时改变。这是Sun并用java - API的船下两个单独的封装结构,java.whatever和com.sun.whatever。的com.sun不支持或者记录,你需要您自担风险使用这些类。

Typically the way this problem is solved is by convention: provide documentation only for the API classes, and specify that any other classes in your JAR are not supported and may change at any time. This is what Sun did with java - the API ships under two separate package structures, java.whatever and com.sun.whatever. com.sun is not supported or documented, and you use those classes at your own risk.

一些API提供者也喜欢运送两个单独的JAR,其中之一是明确的API,而另一个是明确支持/实施。这使得客户端只需要上的两个API JAR编译时的依赖,但运行时依赖。

Some API providers also like to ship two separate JARs, one of which is explicitly API and the other is explicitly support/implementation. This allows clients to only have a compile-time dependency on the API JAR, but a runtime dependency on both.

您也应该认真考虑你的JAR(S)从把类在同一个包你使用和访问受保护或保护包(默认)成员密封到prevent客户。

You should also strongly consider sealing your JAR(s) to prevent clients from putting classes in the same packages you're using and access your protected or package protected (default) members.

这篇关于根据predefined API限制进入类和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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