简体   繁体   English

封装结构的最佳做法

[英]Best practice for package-structure

I have following situation: 我有以下情况:

I have a package x with class A which uses classes B and C. The access level of classes B and C is package-private. 我有一个带有类A的包x,它使用类B和C。类B和C的访问级别是包私有的。 B and C implement the same public Interface I. B和C实现相同的公共接口I。

For a better organisation of code I would like to group classes B, C and I into another package y, but WITHOUT changing the access level to public. 为了更好地组织代码,我想将类B,C和I分组到另一个包y中,但无需将访问级别更改为public。

How can I do this? 我怎样才能做到这一点? Are there some other ways than to create a public proxy class D in package y with some static factories producing instances of I? 除了使用某些生成I实例的静态工厂在y包中创建公共代理类D之外,还有其他方法吗?

Kind regards, Rokko_11 此致,Rokko_11

Don't let "status quo" dictate your design decisions. 不要让“现状”支配您的设计决策。

You are already doing "refactoring"; 您已经在进行“重构”; then: if there are good reasons to make B and C visible; 然后:如果有充分的理由使B和C可见; then make the essential parts of those classes public. 然后将这些课程的基本部分公开。 If not, don't do it; 如果没有,那就不要做; and keeps things package-local. 并将内容打包在本地。

That really depends on your concrete application; 这确实取决于您的具体应用。 and how B and C are used today. 以及今天如何使用B和C。 This decision can't be made by other folks on stackoverflow. 其他人无法在stackoverflow上做出此决定。

Side note: just because you need a factory; 旁注:因为您需要工厂; that doesn't imply that things need to be static. 这并不意味着事物必须是静态的。 To the contrary - you want to avoid "static" stuff as much as possible; 相反-您想尽可能避免使用“静态”内容; as unwise usage of "static" very often breaks your ability to do reasonable unit testing. 因为不正确地使用“静态”经常会破坏您进行合理的单元测试的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM