vendredi 28 août 2015

Avoid dependencies between modules

For the sake of the question let's say I have a cryptographic module opensslCryptographicModule that have the following methods:

public String encrypt(String content, String key);
public String decrypt(String encrypted, String key);

And an other module, let's call it MainModule that needs a cryptographic module to work.

Since the cryptographic module used in MainModule may change I created an interface as follow:

public interface CryptographicModule {
    public String encrypt(String content, String key);
    public String decrypt(String encrypted, String key);
}

And the MainModule takes a CryptographicModule in its constructor.

My question is how do I keep the cryptographic module totally independant ?

If I put the interface in the MainModule then the opensslCryptographicModule will need to implements an interface declared in an other project and therefore will become dependant of that project.

But if I put the interface in the opensslCryptographicModule then the other cryptographic modules will need opensslCryptographicModule to get the interface.


The only solution that I can see is a third module that will make the link between both of the modules.

It will implements the interface (that will be in the MainModule) using the adapter pattern to provides the functionalities of the cryptographic module:

public class OpenSSLCryptographicModuleAdapter implements CryptographicModule  {

    private OpensslCryptographicModule module;    

    public OpenSSLCryptographicModuleAdapter(OpensslCryptographicModule module) {
        this.module = module 
    }

    @Override
    public String encrypt(String content, String key) {
        //...
    }

    @Override
    public String decrypt(String encrypted, String key) {
        //...
    }
}

And the MainModule will rely on this third module to work.

But I find it a bit overkill especially when we have control of all the modules. This design pattern is great when using a third party library or if we want to works with some old code, but not when the whole project is been written.

Aucun commentaire:

Enregistrer un commentaire