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?
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.