如何向世界其他地方隐藏 Java API 的内部结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5872124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:13:12  来源:igfitidea点击:

How to hide the internal structure of a Java API to the rest of the world

javaapidesign-patterns

提问by CoffeJunky

i am developing a Java Api to do things (secret, uhhhh ;).

我正在开发一个 Java Api 来做事(秘密,嗯;)。

Is there a way to hide classes, and the internal structure of my API?

有没有办法隐藏类,以及我的 API 的内部结构?

What i found until now:

到目前为止我发现了什么:

  • Using inner classes (ugly way, i do not want to put all in on class file)
  • All classes in one package so that i can use the "package"-visibilty (also ugly, i need more packages)
  • 使用内部类(丑陋的方式,我不想把所有的都放在类文件中)
  • 一个包中的所有类,以便我可以使用“包”-可见性(也很丑,我需要更多包)

Example:

例子:

---
package net.my.app;
//this is the Public Access
class MyPublicClass{
    public void somePublicFunction(){ 
        //access to not visibil classes
    }
}

---
package net.my.app.notvisible:
//this is what i want to hide
class MyNOTPublicClass{
    ...
}
---

Any ideas? Thank you!

有任何想法吗?谢谢!

采纳答案by Sean Patrick Floyd

  • Use interfaces to define what your app does
  • Create a main entry point to accesses services, returning interfaces only
  • I wouldn't bother about actually hiding the implementation classes. You can never really hide them in Java, and those who are technically interested might just start your app with a debugger. Just provide no public constructors, for example
  • 使用接口来定义你的应用程序做什么
  • 创建访问服务的主入口点,仅返回接口
  • 我不会为真正隐藏实现类而烦恼。您永远无法真正将它们隐藏在 Java 中,那些对技术感兴趣的人可能只是使用调试器启动您的应用程序。只是不提供公共构造函数,例如

Regarding this comment:

关于此评论:

Sean, would you elaborate a little more on your answer? ...

肖恩,你能详细说明一下你的答案吗?...

One way to implement my second bullet point I mean using a Service Lookup class, e.g.

实现我的第二个要点的一种方法是使用服务查找类,例如

public class Lookup {
    private static final Foo foo = new FooImpl();
    public static Foo getFoo() { 
        return foo; 
    }
}

Foois an interface, FooImplan implementation class (which can be package private if you want to enforce that it can't be instantiated by clients)

Foo是一个接口,FooImpl一个实现类(如果你想强制它不能被客户端实例化,它可以是包私有的)

回答by Jeff Axelrod

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

您的问题有两种解决方案,它们不涉及将所有类保留在同一个包中。

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

第一种是使用(Practical API Design, Tulach 2008)中描述的朋友访问器/朋友包模式。

The second is to use OSGi.

二是使用OSGi。

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

相关问题:1234

回答by spot35

What do you mean by 'hide'?

“隐藏”是什么意思?

You can use the final modifier to stop people from extending methods and classes you don't want them to extend. If you want to stop people from decompiling your code, you can use code obfuscation and if you want to take it even further, you can use anonymous inner classes that implement interfaces.

您可以使用 final 修饰符来阻止人们扩展您不希望他们扩展的方法和类。如果你想阻止人们反编译你的代码,你可以使用代码混淆,如果你想更进一步,你可以使用实现接口的匿名内部类。

回答by Eric

  • You can try and make only your interfaces public. Have a look at the Factory Pattern.
  • Alternatively, you can implement you're application in OSGI.
  • 您可以尝试只公开您的接口。看看工厂模式。
  • 或者,您可以在 OSGI 中实现您的应用程序。

Neither of these methods would allow you to hide the implementation completely to someone who really wanted to see it. Someone could still use a decompiler to examine you .class files, or even examine the code in memory.

这些方法都不允许您将实现完全隐藏给真正想看到它的人。有人仍然可以使用反编译器来检查您的 .class 文件,甚至检查内存中的代码。

If you really need to protect your implementation in this way, then a good approach would be to only allow access to your application as a remote service and host it on a secure machine.

如果您真的需要以这种方式保护您的实现,那么一个好的方法是只允许将您的应用程序作为远程服务访问并将其托管在安全机器上。