java隐藏API,如何隐藏的Java API的内部结构的世界的剩余部分

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

Is there a way to hide classes, and the internal structure of my 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!

解决方案

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

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;

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值