Reputation:
I have a question with the Adapter Pattern. I am implementing this pattern because I have a third-party library and don’t want to depend upon it.
However, I dont’t get why creating the IAdapter and the Adapter in the same proyect will remove a dependency.
Because if the third-party library changes, the package need to be recompiled, so also any class that uses the IAdapter also needs to be recompiled.
Does the IAdapter and the Adapter must be in different packages?
Upvotes: 0
Views: 243
Reputation: 2998
Measure of whether 2 components/classes are decoupled or not isn't merely based on the need to recompile the package. There are multiple dimensions to it.
IAdapter
?IAdapter
?Depending on your needs, here is how you can achieve above goals.
IAdapter
implemented by ThirdParty1Adapter
. If you wish to replace it, you can implement ThirdParty2Adapter
that fulfills all contracts of IAdapter
. Your IAdapter
clients will be unaffected.IAdapter
in a dedicated jar. Client application would expect one of the implementation of IAdapter
to be provided by DI container or application server. There are options here, but just being technology agnostic here.Upvotes: 1
Reputation: 131356
The adapter is not magical. It will adapt a specific version of a class/library to make it usable with the API that you wish to use.
You have to go further than different packages since indeed, in your case the adapter classes (Adapter interface and Adapter implementation) should not even be included in the third party library source code.
You want to decouple them to make the adapter to rely on a specific and released version of the adapted class to make its integration stable.
Upvotes: 0