API vs. Implementation
←Older revision | Revision as of 04:18, 15 February 2025 | ||
Line 4: | Line 4: | ||
The primary rule comes from [[Chapter 6]] of [[TheAPIBook]] called [[Code Against Interfaces, Not Implementations]]: make sure to '''distinguish''' between [[API]] and '''implementation'''! [[API]] is supposed to act as a ''facade'' hiding the gory implementation details. The fact that something is implemented by ten different implementation classes doesn't mean all of them have to be exposed to users of the [[API]]! | The primary rule comes from [[Chapter 6]] of [[TheAPIBook]] called [[Code Against Interfaces, Not Implementations]]: make sure to '''distinguish''' between [[API]] and '''implementation'''! [[API]] is supposed to act as a ''facade'' hiding the gory implementation details. The fact that something is implemented by ten different implementation classes doesn't mean all of them have to be exposed to users of the [[API]]! | ||
+ | |||
+ | <!-- show example from https://vmmeetup.github.io/2015/ | ||
+ | |||
+ | An API for a multi-threaded, multi-language, multi-tenant, multi-node VM | ||
+ | Jaroslav Tulach, Oracle Labs | ||
+ | |||
+ | Comparing the Interop API before and after cleanup | ||
+ | |||
+ | --> | ||
Don't mix [[API]] and implementation in one package or (if you do) make implementation '''private''' so it is not visible in the [[javadoc]]. When you split [[API]] and implementation into separate packages, consider using [[FriendPackages]] accessor so the [[API]] package has a '''priviledged''' access to the implementation when it needs it. Discourage people from using implementation by making it package private, or in case of [[Modular_Java_SE|Java Modular system]] by ''exporting only API'' package outside of your module. | Don't mix [[API]] and implementation in one package or (if you do) make implementation '''private''' so it is not visible in the [[javadoc]]. When you split [[API]] and implementation into separate packages, consider using [[FriendPackages]] accessor so the [[API]] package has a '''priviledged''' access to the implementation when it needs it. Discourage people from using implementation by making it package private, or in case of [[Modular_Java_SE|Java Modular system]] by ''exporting only API'' package outside of your module. |