简体   繁体   中英

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. B and C implement the same public Interface 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.

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?

Kind regards, 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; 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. This decision can't be made by other folks on 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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